X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fnbi%2Fapis%2Fservicecatalog%2FToscaInfosProcessor.java;h=2153b84f3ddf78bd63c9dafca60a31ceeddf2462;hb=ca7fa9dce1d37491e648531998c1577446eff23b;hp=99a16578383227f25432b0b8fc6fe8da09358e33;hpb=09d6ce6112d61a7fc98c3038afce636b3e494f29;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 99a1657..2153b84 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java @@ -11,36 +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 com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.nio.file.Path; -import java.sql.Timestamp; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import org.apache.commons.io.FileUtils; -import org.onap.nbi.exceptions.TechnicalException; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; 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.parameters.Input; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; +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 { @@ -48,305 +44,151 @@ 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 buildResponseWithToscaInfos(Map toscaInfosTopologyTemplate, - Map serviceCatalogResponse) { - if (toscaInfosTopologyTemplate.get("inputs") != null) { - ArrayList serviceSpecCharacteristic = new ArrayList(); - LinkedHashMap toscaInfos = (LinkedHashMap) toscaInfosTopologyTemplate.get("inputs"); - Set> stringLinkedHashMapEntry = (Set>) toscaInfos - .entrySet(); - - for (Map.Entry key :stringLinkedHashMapEntry) { - String keyString = key.getKey(); - LinkedHashMap inputParameter = key.getValue(); - 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("@type", "ONAPserviceCharacteristic"); - mapParameter.put("required", inputParameter.get("required")); - mapParameter.put("status", inputParameter.get("status")); - List serviceSpecCharacteristicValues = - buildServiceSpecCharacteristicsValues(inputParameter, parameterType); - mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues); - serviceSpecCharacteristic.add(mapParameter); - } - - serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic); - } - LinkedHashMap nodeTemplate = (LinkedHashMap) toscaInfosTopologyTemplate.get("node_templates"); - - 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); - LinkedHashMap toscaInfosFromResourceId = getToscaInfosFromResourceUUID(nodeTemplate, id); - if (toscaInfosFromResourceId != null && toscaInfosFromResourceId.get("customizationUUID")!=null) { - resourceSpecification.put("modelCustomizationId", toscaInfosFromResourceId.get("customizationUUID")); - } - } - } - } - public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException { SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); - ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(),false); + ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false); List inputs = sdcCsarHelper.getServiceInputs(); - if(inputs != null && inputs.size() > 0) { + 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) - { + 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); + mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues); + } + 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){ + 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; - } + 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) { - - List serviceSpecCharacteristicValues = new ArrayList<>(); - LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap(); - - serviceSpecCharacteristicValue.put("isDefault", true); + + 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); - - return serviceSpecCharacteristicValues; - } - private List buildServiceSpecCharacteristicsValues(LinkedHashMap parameter, String parameterType) { - List 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 entrySchema = (ArrayList) parameter.get("entry_schema"); - if (!CollectionUtils.isEmpty(entrySchema)) { - buildCharacteristicValuesFormShema(parameterType, serviceSpecCharacteristicValues, aDefault, - entrySchema); - } - } - } return serviceSpecCharacteristicValues; } - private void buildCharacteristicValuesFormShema(String parameterType, - List serviceSpecCharacteristicValues, Object aDefault, ArrayList entrySchema) { - LinkedHashMap constraints = (LinkedHashMap) entrySchema.get(0); - if (constraints != null) { - ArrayList constraintsList = (ArrayList) constraints.get("constraints"); - if (!CollectionUtils.isEmpty(constraintsList)) { - LinkedHashMap valuesMap = (LinkedHashMap) constraintsList.get(0); - if (valuesMap != null) { - List values = (List) 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); - } - } - } - } - } + 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(); - private LinkedHashMap getToscaInfosFromResourceUUID(LinkedHashMap nodeTemplates, String name) { - if(nodeTemplates!=null) { - for (Object nodeTemplateObject : nodeTemplates.values()) { - LinkedHashMap nodeTemplate = (LinkedHashMap) nodeTemplateObject; - LinkedHashMap metadata = (LinkedHashMap) nodeTemplate.get("metadata"); - if(metadata.get("UUID")!=null && metadata.get("type")!=null) { - String metadataUUID = (String) metadata.get("UUID"); - String metadataType = (String) metadata.get("type"); - if ("VF".equalsIgnoreCase(metadataType) && name!=null && name.equalsIgnoreCase(metadataUUID)) { - return metadata; - } - } - } - } - return null; - } - - - public Map getToscaInfos(Map sdcResponse) { - - LinkedHashMap topologyTemplate = null; - - String toscaModelUrl = (String) sdcResponse.get("toscaModelURL"); - String serviceId = (String) sdcResponse.get("id"); - File toscaFile = sdcClient.callGetWithAttachment(toscaModelUrl); - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - String tempFolderName = serviceId + timestamp; - File folderTemp = 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"); - topologyTemplate = getToscaTopologyTemplateNode(tempFolderName, toscaMetaFileHashMap); - return topologyTemplate; - } catch (TechnicalException e) { - LOGGER.error("unable to parse tosca file for id : " + serviceId, e); - return topologyTemplate; - } - finally { - deleteTempFiles(serviceId, toscaFile, folderTemp); - } + Map definitions = new HashMap(); + Model model = new ModelImpl(); - } - - private LinkedHashMap getToscaTopologyTemplateNode(String tempFolderName,LinkedHashMap toscaMetaFileHashMap) { - LinkedHashMap topologyTemplate = null; - if (toscaMetaFileHashMap.get("Entry-Definitions") != null) { - String toscaFilePath = (String) toscaMetaFileHashMap.get("Entry-Definitions"); - LinkedHashMap toscaFileHashMap = parseToscaFile(tempFolderName + "/" + toscaFilePath); - if (toscaFileHashMap.get("topology_template") != null) { - topologyTemplate = (LinkedHashMap) toscaFileHashMap.get("topology_template"); - } else { - LOGGER.error("no Entry-Definitions node in TOSCA.meta"); - } - } else { - LOGGER.error("no topology_template node in tosca file"); - } - return topologyTemplate; - } + if (inputs != null && 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()); - private void deleteTempFiles(String serviceId, File toscaFile, File folderTemp) { - try { - if(folderTemp!=null){ - LOGGER.debug("deleting temp folder for tosca files : " + folderTemp.getName()); - FileUtils.deleteDirectory(folderTemp); + if (input.getDefault() != null) { + property.setDefault(input.getDefault().toString()); + } + ((ModelImpl) model).addProperty(input.getName(), property); } - LOGGER.debug("deleting tosca archive : " + toscaFile.getName()); - FileUtils.forceDelete(toscaFile); - } catch (IOException e) { - LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e); - } - } - - private LinkedHashMap parseToscaFile(String fileName) { - - File toscaFile = new File(fileName); - if (!toscaFile.exists()) { - throw new TechnicalException("unable to find file : " + fileName); - } - try { - return (LinkedHashMap) mapper.readValue(toscaFile, Object.class); - } catch (IOException e) { - LOGGER.warn("unable to parse tosca file : " + fileName, e); - throw new TechnicalException("Unable to parse tosca file : " + fileName); + definitions.put("ServiceCharacteristics", model); - } catch (NullPointerException e) { - LOGGER.warn("unable to find tosca file : " + fileName, e); - throw new TechnicalException("unable to find tosca file : " + fileName); } - } - - - /** - * 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(); + String svcCharacteristicsJson = Json.pretty(definitions); + serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse); - while (ze != null) { + Metadata serviceMetadata = sdcCsarHelper.getServiceMetadata(); + String instantationType = serviceMetadata.getValue("instantiationType"); + serviceCatalogResponse.put("instantiationType", instantationType); - String fileName = ze.getName(); - File newFile = new File(outputFolder + File.separator + fileName); + 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"); - LOGGER.debug("File to unzip : " + newFile.getAbsoluteFile()); + 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); - // create all non exists folders - // else you will hit FileNotFoundException for compressed folder - new File(newFile.getParent()).mkdirs(); + serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic); - try (FileOutputStream fos = new FileOutputStream(newFile)) { - - int len; - while ((len = zis.read(buffer)) > 0) { - fos.write(buffer, 0, len); - } + 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; } - ze = zis.getNextEntry(); } - zis.closeEntry(); + if (nodeTemplate == null) + continue; + resourceSpecification.put("modelCustomizationId", + sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate)); } - - LOGGER.debug("Done"); - - } catch (IOException ex) { - LOGGER.error("Error while unzipping ToscaModel archive from ONAP", ex); - throw new TechnicalException("Error while unzipping ToscaModel archive from ONAP"); } } - }