Fix issue with null Pointer for Inputs of type list/map
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.java
index fff4444..97bcd9c 100644 (file)
@@ -15,6 +15,7 @@ package org.onap.nbi.apis.servicecatalog;
 
 import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +30,11 @@ 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 {
@@ -36,12 +42,13 @@ 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 {
 
@@ -92,7 +99,6 @@ public class ToscaInfosProcessor {
     }
   }
 
-
   private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
 
     List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
@@ -106,4 +112,80 @@ public class ToscaInfosProcessor {
     return serviceSpecCharacteristicValues;
   }
 
+  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();
+
+    Map<String, Model> definitions = new HashMap<String, Model>();
+    Model model = new ModelImpl();
+
+    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());
+
+        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);
+
+    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));
+      }
+    }
+  }
+
 }