void handleVectorTypeDepth1(SimulatorResourceModel &resModel, const OC::OCRepresentation::AttributeItem &ocAttribute) { if (OC::AttributeType::Integer == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<int>>()); } else if (OC::AttributeType::Double == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<double>>()); } else if (OC::AttributeType::Boolean == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<bool>>()); } else if (OC::AttributeType::String == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::string>>()); } else if (OC::AttributeType::OCRepresentation == ocAttribute.base_type()) { std::vector<OC::OCRepresentation> ocSubRepArray = ocAttribute.getValue<std::vector<OC::OCRepresentation>>(); std::vector<SimulatorResourceModel> subResModelArray(ocSubRepArray.size()); for (size_t i = 0; i < ocSubRepArray.size(); i++) { handleRepresentationType(subResModelArray[i], ocSubRepArray[i]); } resModel.add<std::vector<SimulatorResourceModel>>(ocAttribute.attrname(), subResModelArray); } }
SimulatorResourceModel SimulatorResourceFactory::buildResourceModel( std::shared_ptr<RAML::Items> item) { SimulatorResourceModel itemModel; for ( auto &propElement : item->getProperties()) { if (!propElement.second) continue; std::string propName = propElement.second->getName(); if ("p" == propName || "n" == propName || "id" == propName) { continue; } if ("array" == propElement.second->getType()) { std::vector<SimulatorResourceModel> arrayResModel; for ( auto &propertyItem : propElement.second->getItems()) { arrayResModel.push_back(buildResourceModel(propertyItem)); } itemModel.add(propName, arrayResModel); } else { itemModel.add(buildAttribute(propElement.second)); } } return itemModel; }
void handleVectorTypeDepth3(SimulatorResourceModel &resModel, const OC::OCRepresentation::AttributeItem &ocAttribute) { if (OC::AttributeType::Integer == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<std::vector<int>>>>()); } else if (OC::AttributeType::Double == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<std::vector<double>>>>()); } else if (OC::AttributeType::Boolean == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<std::vector<bool>>>>()); } else if (OC::AttributeType::String == ocAttribute.base_type()) { resModel.add(ocAttribute.attrname(), ocAttribute.getValue<std::vector<std::vector<std::vector<std::string>>>>()); } else if (OC::AttributeType::OCRepresentation == ocAttribute.base_type()) { std::vector<std::vector<std::vector<OC::OCRepresentation>>> ocSubRepArray = ocAttribute.getValue<std::vector<std::vector<std::vector<OC::OCRepresentation>>>>(); std::vector<std::vector<std::vector<SimulatorResourceModel>>> subResModelArray( ocSubRepArray.size()); for (size_t i = 0; i < ocSubRepArray.size(); i++) { std::vector<std::vector<SimulatorResourceModel>> innerArray1(ocSubRepArray[i].size()); for (size_t j = 0; j < ocSubRepArray[i].size(); j++) { std::vector<SimulatorResourceModel> innerArray2(ocSubRepArray[i][j].size()); for (size_t k = 0; k < ocSubRepArray[i][j].size(); k++) { handleRepresentationType(innerArray2[k], ocSubRepArray[i][j][k]); } innerArray1[j] = innerArray2; } subResModelArray[i] = innerArray1; } resModel.add<std::vector<std::vector<std::vector<SimulatorResourceModel>>>>( ocAttribute.attrname(), subResModelArray); } }
SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody( RAML::RequestResponseBodyPtr responseBody, std::string &resourceType, std::vector<std::string> &interfaceType) { SimulatorResourceModel resModel; if (!responseBody) return resModel; // Iterate throgh all resource property and extract information needed for simulating resource. RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties(); for ( auto &propertyElement : resourceProperties->getProperties()) { if (!propertyElement.second) continue; std::string propName = propertyElement.second->getName(); // Resource type if ("rt" == propName || "resourceType" == propName) { resourceType = propertyElement.second->getValueString(); continue; } // Interface type if ("if" == propName) { if ("string" == propertyElement.second->getType()) { interfaceType.push_back(propertyElement.second->getValueString()); } else if ("array" == propertyElement.second->getType()) { for (auto &item : propertyElement.second->getItems()) { if ("string" == item->getType()) { interfaceType = item->getAllowedValuesString(); break; } } } continue; } // Other Standard properties which should not be part of resource model if ("p" == propName || "n" == propName || "id" == propName) { continue; } // Add the attribute to resource model if ("array" == propertyElement.second->getType()) { std::vector<SimulatorResourceModel> arrayResModel; for ( auto &propertyItem : propertyElement.second->getItems()) { arrayResModel.push_back(buildResourceModel(propertyItem)); } resModel.add(propName, arrayResModel); } else { resModel.add(buildAttribute(propertyElement.second)); } } if ("array" == resourceProperties->getType()) { std::vector<SimulatorResourceModel> arrayResModel; for ( auto &propertyItem : resourceProperties->getItems()) { arrayResModel.push_back(buildResourceModel(propertyItem)); } resModel.add("links", arrayResModel); } return resModel; }
bool simulatorResourceModelToCpp(JNIEnv *env, jobject jResModel, SimulatorResourceModel &resModel) { if (!jResModel) return false; static jfieldID valuesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls, "mValues", "Ljava/util/Map;"); static jfieldID propertiesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls, "mProperties", "Ljava/util/Map;"); static jmethodID entrySetMID = env->GetMethodID(gSimulatorClassRefs.mapCls, "entrySet", "()Ljava/util/Set;"); static jmethodID iteratorMID = env->GetMethodID(gSimulatorClassRefs.setCls, "iterator", "()Ljava/util/Iterator;"); static jmethodID hasNextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "hasNext", "()Z"); static jmethodID nextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "next", "()Ljava/lang/Object;"); static jmethodID getKeyMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getKey", "()Ljava/lang/Object;"); static jmethodID getValueMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getValue", "()Ljava/lang/Object;"); jobject jValues = env->GetObjectField(jResModel, valuesFID); jobject jProperties = env->GetObjectField(jResModel, propertiesFID); if (jValues) { jobject entrySet = env->CallObjectMethod(jValues, entrySetMID); jobject iterator = env->CallObjectMethod(entrySet, iteratorMID); if (entrySet && iterator) { while (env->CallBooleanMethod(iterator, hasNextMID)) { jobject entry = env->CallObjectMethod(iterator, nextMID); jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID); jobject value = env->CallObjectMethod(entry, getValueMID); resModel.add(JniString(env, key).get(), JniAttributeValue::toCpp(env, value)); env->DeleteLocalRef(entry); env->DeleteLocalRef(key); env->DeleteLocalRef(value); } } } if (jProperties) { jobject entrySet = env->CallObjectMethod(jProperties, entrySetMID); jobject iterator = env->CallObjectMethod(entrySet, iteratorMID); if (entrySet && iterator) { while (env->CallBooleanMethod(iterator, hasNextMID)) { jobject entry = env->CallObjectMethod(iterator, nextMID); jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID); jobject value = env->CallObjectMethod(entry, getValueMID); resModel.setAttributeProperty(JniString(env, key).get(), JniAttributeProperty::toCpp(env, value)); env->DeleteLocalRef(entry); env->DeleteLocalRef(key); env->DeleteLocalRef(value); } } } return true; }