X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fnbi%2Fapis%2Fservicecatalog%2FToscaInfosProcessor.java;h=2046ecfb54b006736392a2d545d5293c83061803;hb=HEAD;hp=fc0494fdbade708b9e14b13411e9c4eaebacd6cf;hpb=61bc7f0724de5df59d70ec32c8f11f62e7c42feb;p=externalapi%2Fnbi.git diff --git a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java index fc0494f..2046ecf 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java @@ -11,16 +11,32 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + package org.onap.nbi.apis.servicecatalog; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; -import io.swagger.util.Json; +import org.onap.nbi.apis.serviceorder.model.consumer.VFModelInfo; +import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.elements.queries.EntityQuery; +import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery; +import org.onap.sdc.tosca.parser.enums.SdcTypes; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.onap.sdc.toscaparser.api.functions.GetInput; import org.onap.sdc.toscaparser.api.parameters.Input; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,144 +48,270 @@ import io.swagger.models.Model; import io.swagger.models.ModelImpl; import io.swagger.models.properties.Property; import io.swagger.models.properties.PropertyBuilder; +import io.swagger.util.Json; @Service public class ToscaInfosProcessor { - @Autowired - SdcClient sdcClient; - - @Autowired - private ServiceSpecificationDBManager serviceSpecificationDBManager; - - final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class); - - public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) - throws SdcToscaParserException { - - SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); - ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false); - List inputs = sdcCsarHelper.getServiceInputs(); - if (inputs != null && inputs.size() > 0) { - ArrayList serviceSpecCharacteristic = new ArrayList(); - for (Input input : inputs) { - LinkedHashMap mapParameter = new LinkedHashMap(); - mapParameter.put("name", input.getName()); - mapParameter.put("description", input.getDescription()); - mapParameter.put("valueType", input.getType()); - mapParameter.put("@type", "ONAPserviceCharacteristic"); - mapParameter.put("required", input.isRequired()); - mapParameter.put("status", null); - mapParameter.put("serviceSpecCharacteristicValue", null); - // If this Input has a default value, then put it in serviceSpecCharacteristicValue - if (input.getDefault() != null) { - List serviceSpecCharacteristicValues = - buildServiceSpecCharacteristicsValuesFromSdc(input); - mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues); + @Autowired + SdcClient sdcClient; + + @Autowired + private ServiceSpecificationDBManager serviceSpecificationDBManager; + + private Set vnfInstanceParams = new HashSet(Arrays.asList("onap_private_net_id", + "onap_private_subnet_id", "pub_key", "sec_group", "install_script_version", "demo_artifacts_version", + "cloud_env", "public_net_id", "aic-cloud-region", "image_name", "flavor_name", "skip_post_instantiation_configuration")); + + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind + + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class); + + public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException { + + SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); + ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false); + List inputs = sdcCsarHelper.getServiceInputs(); + if (inputs != null && inputs.size() > 0) { + ArrayList serviceSpecCharacteristic = new ArrayList(); + for (Input input : inputs) { + LinkedHashMap mapParameter = new LinkedHashMap(); + mapParameter.put("name", input.getName()); + mapParameter.put("description", input.getDescription()); + mapParameter.put("valueType", input.getType()); + mapParameter.put("@type", "ONAPserviceCharacteristic"); + mapParameter.put("required", input.isRequired()); + mapParameter.put("status", null); + mapParameter.put("serviceSpecCharacteristicValue", null); + // If this Input has a default value, then put it in serviceSpecCharacteristicValue + if (input.getDefault() != null) { + List serviceSpecCharacteristicValues = + buildServiceSpecCharacteristicsValuesFromSdc(input); + mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues); + } + serviceSpecCharacteristic.add(mapParameter); + } + serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic); } - serviceSpecCharacteristic.add(mapParameter); - } - serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic); - } - List nodeTemplates = sdcCsarHelper.getServiceNodeTemplates(); - - List resourceSpecifications = - (List) serviceCatalogResponse.get("resourceSpecification"); - for (LinkedHashMap resourceSpecification : resourceSpecifications) { - if (resourceSpecification.get("id") != null) { - String id = (String) resourceSpecification.get("id"); - LOGGER.debug("get tosca infos for service id: {}", id); - NodeTemplate nodeTemplate = null; - for (NodeTemplate node : nodeTemplates) { - if (node.getMetaData().getValue("UUID").equals(id)) { - nodeTemplate = node; - break; - } + List nodeTemplates = sdcCsarHelper.getServiceNodeTemplates(); + + List resourceSpecifications = + (List) serviceCatalogResponse.get("resourceSpecification"); + for (LinkedHashMap resourceSpecification : resourceSpecifications) { + if (resourceSpecification.get("id") != null) { + String id = (String) resourceSpecification.get("id"); + LOGGER.debug("get tosca infos for service id: {}", id); + NodeTemplate nodeTemplate = null; + for (NodeTemplate node : nodeTemplates) { + if (node.getMetaData().getValue("UUID").equals(id)) { + nodeTemplate = node; + break; + } + } + if (nodeTemplate == null) + continue; + resourceSpecification.put("modelCustomizationId", + sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate)); + } } - if (nodeTemplate == null) - continue; - resourceSpecification.put("modelCustomizationId", - sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate)); - } } - } - private List buildServiceSpecCharacteristicsValuesFromSdc(Input input) { + private List buildServiceSpecCharacteristicsValuesFromSdc(Input input) { - List serviceSpecCharacteristicValues = new ArrayList<>(); - LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap(); + List serviceSpecCharacteristicValues = new ArrayList<>(); + LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap(); - serviceSpecCharacteristicValue.put("isDefault", true); - serviceSpecCharacteristicValue.put("value", input.getDefault()); - serviceSpecCharacteristicValue.put("valueType", input.getType()); - serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue); + serviceSpecCharacteristicValue.put("isDefault", true); + serviceSpecCharacteristicValue.put("value", input.getDefault()); + serviceSpecCharacteristicValue.put("valueType", input.getType()); + serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue); - return serviceSpecCharacteristicValues; - } + return serviceSpecCharacteristicValues; + } - public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException { + public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) + throws SdcToscaParserException { - SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); - ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false); - List inputs = sdcCsarHelper.getServiceInputs(); + SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); + ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false); + List inputs = sdcCsarHelper.getServiceInputs(); - Map definitions = new HashMap(); - Model model = new ModelImpl(); + List vfEntityList = sdcCsarHelper.getEntity(EntityQuery.newBuilder(SdcTypes.VF).build(), + TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false); - if (inputs != null && inputs.size() > 0) { - for (Input input : inputs) { - Property property = PropertyBuilder.build(input.getType(), null, null); - property.setDescription(input.getDescription()); - property.setRequired(input.isRequired()); + Map groupProperties = null; + Map listOfInstanceParameters = new HashMap<>(); + if (!vfEntityList.isEmpty()) { - if (input.getDefault() != null) { - property.setDefault(input.getDefault().toString()); - } - ((ModelImpl) model).addProperty(input.getName(), property); - } - definitions.put("ServiceCharacteristics", model); + IEntityDetails iEntityDetails = vfEntityList.get(0); + groupProperties = iEntityDetails.getProperties(); + + for (String key : groupProperties.keySet()) { + org.onap.sdc.toscaparser.api.Property property = groupProperties.get(key); + String paramName = property.getName(); + if (paramName != null) { + if (vnfInstanceParams.stream() + .filter(vnfInstanceParam -> vnfInstanceParam.equalsIgnoreCase(paramName)).findFirst() + .isPresent()) { + listOfInstanceParameters.put(paramName, property.getValue().toString()); + } + } + } + + } + + // it will build Entity as VfModules + List vfModuleEntityList = sdcCsarHelper.getEntity( + EntityQuery.newBuilder("org.openecomp.groups.VfModule").build(), + TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE) + .customizationUUID(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID).build(), + false); + List listOfVfModelInfo = new ArrayList<>(); + + if (!vfModuleEntityList.isEmpty()) { + // Fetching vfModule metadata in a loop + for (IEntityDetails vfModuleEntity : vfModuleEntityList) { + VFModelInfo vfModel = new VFModelInfo(); + Metadata vfMetadata = vfModuleEntity.getMetadata(); + // Preparing VFModel + vfModel.setModelInvariantUuid( + testNull(vfMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID))); + vfModel.setModelName(testNull(vfMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELNAME))); + vfModel.setModelUuid(testNull(vfMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID))); + vfModel.setModelVersion( + testNull(vfMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELVERSION))); + vfModel.setModelCustomizationUuid(testNull(vfMetadata.getValue("vfModuleModelCustomizationUUID"))); + + // Adding it to the list + listOfVfModelInfo.add(vfModel); + } + } + + Map definitions = new HashMap(); + Model model = new ModelImpl(); + + if (!inputs.isEmpty() && inputs.size() > 0) { + for (Input input : inputs) { + Property property = null; + if (input.getType().equals("list") || input.getType().equals("map")) + property = PropertyBuilder.build("array", null, null); + else + property = PropertyBuilder.build(input.getType(), null, null); + + property.setDescription(input.getDescription()); + property.setRequired(input.isRequired()); + + if (input.getDefault() != null) { + property.setDefault(input.getDefault().toString()); + } + ((ModelImpl) model).addProperty(input.getName(), property); + } + definitions.put("ServiceCharacteristics", model); + + } + + String svcCharacteristicsJson = Json.pretty(definitions); + serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse); + + Metadata serviceMetadata = sdcCsarHelper.getServiceMetadata(); + String instantationType = serviceMetadata.getValue("instantiationType"); + serviceCatalogResponse.put("instantiationType", instantationType); + + LinkedHashMap inputSchemaRef = new LinkedHashMap(); + // use object to match examples in Specifications + inputSchemaRef.put("valueType", "object"); + inputSchemaRef.put("@schemaLocation", + "/serviceSpecification/" + serviceCatalogResponse.get("id") + "/specificationInputSchema"); + inputSchemaRef.put("@type", serviceCatalogResponse.get("name") + "_ServiceCharacteristic"); + + LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap(); + serviceSpecCharacteristic.put("name", serviceCatalogResponse.get("name") + "_ServiceCharacteristics"); + serviceSpecCharacteristic.put("description", + "This object describes all the inputs needed from the client to interact with the " + + serviceCatalogResponse.get("name") + " Service Topology"); + serviceSpecCharacteristic.put("valueType", "object"); + serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic"); + serviceSpecCharacteristic.put("@schemaLocation", "null"); + serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", inputSchemaRef); + + serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic); + + List nodeTemplates = sdcCsarHelper.getServiceNodeTemplates(); + List resourceSpecifications = (List) serviceCatalogResponse + .get("resourceSpecification"); + for (LinkedHashMap resourceSpecification : resourceSpecifications) { + if (resourceSpecification.get("id") != null) { + String id = (String) resourceSpecification.get("id"); + LOGGER.debug("get tosca infos for service id: {}", id); + NodeTemplate nodeTemplate = null; + for (NodeTemplate node : nodeTemplates) { + if (node.getMetaData().getValue("UUID").equals(id)) { + nodeTemplate = node; + break; + } + } + if (nodeTemplate == null) + continue; + resourceSpecification.put("modelCustomizationId", + sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate)); + if (!vfModuleEntityList.isEmpty()) { + resourceSpecification.put("childResourceSpecification", listOfVfModelInfo); + } + HashMap serviceInstanceParams = getServiceLevelInstanceParams(inputs); + resourceSpecification.put("serviceInstanceParams", serviceInstanceParams); + HashMap vnfInstanceParams = getUserDefinedVFLevelInstanceParams(groupProperties, listOfInstanceParameters); + resourceSpecification.put("InstanceSpecification", vnfInstanceParams); + } + } + } + + // Get List of Service Level InputParams as Key Value + private HashMap getServiceLevelInstanceParams(List listOfServiceLevelInputs) { + + HashMap serviceLevelInstanceParams = new HashMap<>(); + + for (Input input : listOfServiceLevelInputs) { + serviceLevelInstanceParams.put(input.getName(), input.getDefault()); + } + + return serviceLevelInstanceParams; + } + + private HashMap getUserDefinedVFLevelInstanceParams( + Map groupProperties, Map listOfVFLevelInputs) { + + if(groupProperties != null && !groupProperties.isEmpty()) { + + for (Entry entry : groupProperties.entrySet()) { + + org.onap.sdc.toscaparser.api.Property property = entry.getValue(); + + if ((property.getValue().getClass() == GetInput.class)) { + GetInput getInput = (GetInput) property.getValue(); + listOfVFLevelInputs.put(getInput.getInputName(), getInput.result()); + listOfVFLevelInputs.remove(property.getName()); + } else if(property.getName().equals("skip_post_instantiation_configuration")) { + //Add skip_post_instantiation_configuration as vf Level Inputs + listOfVFLevelInputs.put(property.getName(), property.getValue()); + } + } + + } + + return (HashMap) listOfVFLevelInputs; + } - } - String svcCharacteristicsJson = Json.pretty(definitions); - serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson,serviceCatalogResponse); - - LinkedHashMap inputSchemaRef = new LinkedHashMap(); - inputSchemaRef.put("valueType","Object"); - inputSchemaRef.put("@schemaLocation","/serviceSpecification/"+serviceCatalogResponse.get("id")+"/specificationInputSchema"); - inputSchemaRef.put("@type",serviceCatalogResponse.get("name") + "_ServiceCharacteristic"); - - LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap(); - serviceSpecCharacteristic.put("name",serviceCatalogResponse.get("name") + "_ServiceCharacteristics"); - serviceSpecCharacteristic.put("description","This object describes all the inputs needed from the client to interact with the " + serviceCatalogResponse.get("name") + " Service Topology"); - serviceSpecCharacteristic.put("valueType", "Object"); - serviceSpecCharacteristic.put("@type","ONAPServiceCharacteristic"); - serviceSpecCharacteristic.put("@schemaLocation", "null"); - serviceSpecCharacteristic.put("serviceSpecCharacteristicValue",inputSchemaRef); - - serviceCatalogResponse.put("serviceSpecCharacteristic",serviceSpecCharacteristic); - - List nodeTemplates = sdcCsarHelper.getServiceNodeTemplates(); - List resourceSpecifications = - (List) serviceCatalogResponse.get("resourceSpecification"); - for (LinkedHashMap resourceSpecification : resourceSpecifications) { - if (resourceSpecification.get("id") != null) { - String id = (String) resourceSpecification.get("id"); - LOGGER.debug("get tosca infos for service id: {}", id); - NodeTemplate nodeTemplate = null; - for (NodeTemplate node : nodeTemplates) { - if (node.getMetaData().getValue("UUID").equals(id)) { - nodeTemplate = node; - break; - } + private static String testNull(Object object) { + if (object == null) { + return "NULL"; + } else if (object instanceof Integer) { + return object.toString(); + } else if (object instanceof String) { + return (String) object; + } else { + return "Type not recognized"; } - if (nodeTemplate == null) - continue; - resourceSpecification.put("modelCustomizationId", sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate)); - } } - } -} +} \ No newline at end of file