Service Catalog - json schema reference
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.java
index fff4444..fc0494f 100644 (file)
 package org.onap.nbi.apis.servicecatalog;
 
 import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+
+import io.swagger.util.Json;
 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
@@ -29,6 +28,10 @@ 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;
 
 @Service
 public class ToscaInfosProcessor {
@@ -36,14 +39,15 @@ 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 {
+          throws SdcToscaParserException {
 
     SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
     ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
@@ -62,7 +66,7 @@ public class ToscaInfosProcessor {
         // If this Input has a default value, then put it in serviceSpecCharacteristicValue
         if (input.getDefault() != null) {
           List<LinkedHashMap> serviceSpecCharacteristicValues =
-              buildServiceSpecCharacteristicsValuesFromSdc(input);
+                  buildServiceSpecCharacteristicsValuesFromSdc(input);
           mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
         }
         serviceSpecCharacteristic.add(mapParameter);
@@ -72,7 +76,7 @@ public class ToscaInfosProcessor {
     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
 
     List<LinkedHashMap> resourceSpecifications =
-        (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
+            (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
     for (LinkedHashMap resourceSpecification : resourceSpecifications) {
       if (resourceSpecification.get("id") != null) {
         String id = (String) resourceSpecification.get("id");
@@ -87,12 +91,11 @@ public class ToscaInfosProcessor {
         if (nodeTemplate == null)
           continue;
         resourceSpecification.put("modelCustomizationId",
-            sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
+                sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
       }
     }
   }
 
-
   private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
 
     List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
@@ -106,4 +109,67 @@ 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 = 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();
+    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));
+      }
+    }
+  }
+
 }