/**
- * Copyright (c) 2018 Orange
+ * Copyright (c) 2018 Orange
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
*/
+
package org.onap.nbi.apis.servicecatalog;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.sql.Timestamp;
+import java.nio.file.Path;
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.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.io.FileUtils;
-import org.onap.nbi.exceptions.TechnicalException;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+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;
+
+ private Set<String> vnfInstanceParams = new HashSet<String>(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 buildResponseWithToscaInfos(LinkedHashMap toscaInfosTopologyTemplate,
- LinkedHashMap serviceCatalogResponse) {
- if (toscaInfosTopologyTemplate.get("inputs") != null) {
+ public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
+
+ SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
+ ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
+ List<Input> inputs = sdcCsarHelper.getServiceInputs();
+ if (inputs != null && inputs.size() > 0) {
ArrayList serviceSpecCharacteristic = new ArrayList();
- LinkedHashMap toscaInfos = (LinkedHashMap) toscaInfosTopologyTemplate.get("inputs");
- for (Object key : toscaInfos.keySet()) {
- String keyString = (String) key;
- LinkedHashMap inputParameter = (LinkedHashMap) toscaInfos.get(key);
+ for (Input input : inputs) {
LinkedHashMap mapParameter = new LinkedHashMap();
- String parameterType = (String) inputParameter.get("type");
- mapParameter.put("name", keyString);
- mapParameter.put("description", inputParameter.get("description"));
- mapParameter.put("valueType", parameterType);
+ mapParameter.put("name", input.getName());
+ mapParameter.put("description", input.getDescription());
+ mapParameter.put("valueType", input.getType());
mapParameter.put("@type", "ONAPserviceCharacteristic");
- mapParameter.put("required", inputParameter.get("required"));
- mapParameter.put("status", inputParameter.get("status"));
- List<LinkedHashMap> serviceSpecCharacteristicValues =
- buildServiceSpecCharacteristicsValues(inputParameter, parameterType);
- mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
+ 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<LinkedHashMap> serviceSpecCharacteristicValues =
+ buildServiceSpecCharacteristicsValuesFromSdc(input);
+ mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
+ }
serviceSpecCharacteristic.add(mapParameter);
}
-
serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
}
- LinkedHashMap node_templates = (LinkedHashMap) toscaInfosTopologyTemplate.get("node_templates");
+ List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
List<LinkedHashMap> resourceSpecifications =
(List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
for (LinkedHashMap resourceSpecification : resourceSpecifications) {
- String id = (String) resourceSpecification.get("id");
- LOGGER.debug("get tosca infos for service id: " + id);
- LinkedHashMap toscaInfosFromResourceId = getToscaInfosFromResourceUUID(node_templates, id);
- resourceSpecification.put("modelCustomizationId", toscaInfosFromResourceId.get("customizationUUID"));
- resourceSpecification.put("modelCustomizationName", toscaInfosFromResourceId.get("name"));
-
- }
- }
-
- private List<LinkedHashMap> buildServiceSpecCharacteristicsValues(LinkedHashMap parameter, String parameterType) {
- List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
- if (!"map".equalsIgnoreCase(parameterType) && !"list".equalsIgnoreCase(parameterType)) {
- LOGGER.debug("get tosca infos for serviceSpecCharacteristicValues of type map or string : " + parameter);
- Object aDefault = parameter.get("default");
- if (parameter.get("entry_schema") != null) {
- ArrayList entry_schema = (ArrayList) parameter.get("entry_schema");
- if (CollectionUtils.isNotEmpty(entry_schema)) {
- buildCharacteristicValuesFormShema(parameterType, serviceSpecCharacteristicValues, aDefault,
- entry_schema);
- }
- }
- }
- return serviceSpecCharacteristicValues;
- }
-
- private void buildCharacteristicValuesFormShema(String parameterType,
- List<LinkedHashMap> serviceSpecCharacteristicValues, Object aDefault, ArrayList entry_schema) {
- LinkedHashMap constraints = (LinkedHashMap) entry_schema.get(0);
- if (constraints != null) {
- ArrayList constraintsList = (ArrayList) constraints.get("constraints");
- if (CollectionUtils.isNotEmpty(constraintsList)) {
- LinkedHashMap valuesMap = (LinkedHashMap) constraintsList.get(0);
- if (valuesMap != null) {
- List<Object> values = (List<Object>) valuesMap.get("valid_values");
- for (Object value : values) {
- String stringValue = value.toString();
- LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
- serviceSpecCharacteristicValue.put("isDefault",
- aDefault != null && aDefault.toString().equals(stringValue));
- serviceSpecCharacteristicValue.put("value", stringValue);
- serviceSpecCharacteristicValue.put("valueType", parameterType);
- serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
+ 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));
}
}
}
+ private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
- private LinkedHashMap getToscaInfosFromResourceUUID(LinkedHashMap node_templates, String name) {
- for (Object nodeTemplateObject : node_templates.values()) {
- LinkedHashMap nodeTemplate = (LinkedHashMap) nodeTemplateObject;
- LinkedHashMap metadata = (LinkedHashMap) nodeTemplate.get("metadata");
- String metadataUUID = (String) metadata.get("UUID");
- String metadataType = (String) metadata.get("type");
- if ("VF".equalsIgnoreCase(metadataType) && name.equalsIgnoreCase(metadataUUID)) {
- return metadata;
- }
- }
- return null;
- }
-
-
- public LinkedHashMap getToscaInfos(LinkedHashMap sdcResponse) {
- String toscaModelUrl = (String) sdcResponse.get("toscaModelURL");
- String serviceId = (String) sdcResponse.get("uuid");
- File toscaFile = sdcClient.callGetWithAttachment(toscaModelUrl);
- Timestamp timestamp = new Timestamp(System.currentTimeMillis());
- String tempFolderName = serviceId + timestamp;
- File folderTemp = null;
- LinkedHashMap topology_template = null;
- try {
- unZipArchive(toscaFile.getName(), tempFolderName);
- folderTemp = new File(tempFolderName);
- LOGGER.debug("temp folder for tosca files : " + folderTemp.getName());
-
- LinkedHashMap toscaMetaFileHashMap = parseToscaFile(tempFolderName + "/TOSCA-Metadata/TOSCA.meta");
- if (toscaMetaFileHashMap.get("Entry-Definitions") == null) {
- throw new NullPointerException("no Entry-Definitions node in TOSCA.meta");
- }
- String toscaFilePath = (String) toscaMetaFileHashMap.get("Entry-Definitions");
- LinkedHashMap toscaFileHashMap = parseToscaFile(tempFolderName + "/" + toscaFilePath);
-
- if (toscaFileHashMap.get("topology_template") == null) {
- throw new NullPointerException("no topology_template node in tosca file");
- }
- topology_template = (LinkedHashMap) toscaFileHashMap.get("topology_template");
-
- } catch (NullPointerException e) {
- LOGGER.error("unable to parse tosca file for id : " + serviceId + ", " + e.getMessage());
- return null;
- } finally {
- try {
- LOGGER.debug("deleting temp folder for tosca files : " + folderTemp.getName());
- FileUtils.deleteDirectory(folderTemp);
- LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
- FileUtils.forceDelete(toscaFile);
- return topology_template;
- } catch (IOException e) {
- LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId);
- return null;
-
- }
- }
- }
-
+ List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
+ LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
- private LinkedHashMap parseToscaFile(String fileName) {
+ serviceSpecCharacteristicValue.put("isDefault", true);
+ serviceSpecCharacteristicValue.put("value", input.getDefault());
+ serviceSpecCharacteristicValue.put("valueType", input.getType());
+ serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
- File toscaFile = new File(fileName);
- if (toscaFile == null) {
- throw new TechnicalException("unable to find file : " + fileName);
- }
- try {
- return (LinkedHashMap) mapper.readValue(toscaFile, Object.class);
- } catch (IOException e) {
- LOGGER.error("unable to parse tosca file : " + fileName);
- LOGGER.error(e.getMessage());
- throw new TechnicalException("Unable to parse tosca file : " + fileName);
-
- } catch (NullPointerException e) {
- LOGGER.error("unable to find tosca file : " + fileName);
- LOGGER.error(e.getMessage());
- throw new TechnicalException("unable to find tosca file : " + fileName);
- }
+ return serviceSpecCharacteristicValues;
}
-
- /**
- * Unzip it
- *
- * @param zipFile input zip file
- * @param outputFolder zip file output folder
- */
- private void unZipArchive(String zipFile, String outputFolder) {
-
- byte[] buffer = new byte[1024];
-
- try {
-
- // create output directory is not exists
- File folder = new File(outputFolder);
- if (!folder.exists()) {
- folder.mkdir();
- }
-
- // get the zip file content
- try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
- // get the zipped file list entry
- ZipEntry ze = zis.getNextEntry();
-
- while (ze != null) {
-
- String fileName = ze.getName();
- File newFile = new File(outputFolder + File.separator + fileName);
-
- LOGGER.debug("File to unzip : " + newFile.getAbsoluteFile());
-
- // create all non exists folders
- // else you will hit FileNotFoundException for compressed folder
- new File(newFile.getParent()).mkdirs();
-
- try (FileOutputStream fos = new FileOutputStream(newFile)) {
-
- int len;
- while ((len = zis.read(buffer)) > 0) {
- fos.write(buffer, 0, len);
- }
-
- fos.close();
- }
- ze = zis.getNextEntry();
- }
-
- zis.closeEntry();
- zis.close();
- }
-
- LOGGER.debug("Done");
-
- } catch (IOException ex) {
- LOGGER.error("Error while unzipping ToscaModel archive from ONAP : " + ex.getMessage());
- throw new TechnicalException("Error while unzipping ToscaModel archive from ONAP");
+ public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
+ throws SdcToscaParserException {
+
+ SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
+ ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
+ List<Input> inputs = sdcCsarHelper.getServiceInputs();
+
+ List<IEntityDetails> vfEntityList = sdcCsarHelper.getEntity(EntityQuery.newBuilder(SdcTypes.VF).build(),
+ TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false);
+
+ Map<String, org.onap.sdc.toscaparser.api.Property> groupProperties = null;
+ Map<String, String> listOfInstanceParameters = new HashMap<>();
+ if (!vfEntityList.isEmpty()) {
+
+ 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<IEntityDetails> vfModuleEntityList = sdcCsarHelper.getEntity(
+ EntityQuery.newBuilder("org.openecomp.groups.VfModule").build(),
+ TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
+ .customizationUUID(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID).build(),
+ false);
+ List<VFModelInfo> 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<String, Model> definitions = new HashMap<String, Model>();
+ 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<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
+ List<LinkedHashMap> resourceSpecifications = (List<LinkedHashMap>) 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<String, Object> serviceInstanceParams = getServiceLevelInstanceParams(inputs);
+ resourceSpecification.put("serviceInstanceParams", serviceInstanceParams);
+ HashMap<String, Object> vnfInstanceParams = getUserDefinedVFLevelInstanceParams(groupProperties, listOfInstanceParameters);
+ resourceSpecification.put("InstanceSpecification", vnfInstanceParams);
+ }
+ }
+ }
+
+ // Get List of Service Level InputParams as Key Value
+ private HashMap<String, Object> getServiceLevelInstanceParams(List<Input> listOfServiceLevelInputs) {
+
+ HashMap<String, Object> serviceLevelInstanceParams = new HashMap<>();
+
+ for (Input input : listOfServiceLevelInputs) {
+ serviceLevelInstanceParams.put(input.getName(), input.getDefault());
+ }
+
+ return serviceLevelInstanceParams;
+ }
+
+ private HashMap<String, Object> getUserDefinedVFLevelInstanceParams(
+ Map<String, org.onap.sdc.toscaparser.api.Property> groupProperties, Map listOfVFLevelInputs) {
+
+ if(groupProperties != null && !groupProperties.isEmpty()) {
+
+ for (Entry<String, org.onap.sdc.toscaparser.api.Property> 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<String, Object>) listOfVFLevelInputs;
+ }
+
+
+ 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";
}
}
-
-}
+}
\ No newline at end of file