Integrate sdc-tosca parser jar 14/73414/2
authoraosull01 <adrian.osullivan@huawei.com>
Fri, 23 Nov 2018 10:39:04 +0000 (10:39 +0000)
committeraosull01 <adrian.osullivan@huawei.com>
Wed, 5 Dec 2018 10:48:10 +0000 (10:48 +0000)
Change-Id: Ieef1c4fe9d71164c7696bdb0bd93579a87bc09dc
Issue-ID: EXTAPI-171
Signed-off-by: aosull01 <adrian.osullivan@huawei.com>
pom.xml
src/main/java/org/onap/nbi/apis/servicecatalog/SdcClient.java
src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java
src/test/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessorTest.java
src/test/resources/toscafile/service-Sdwanvpninfraservice-csar.csar [new file with mode: 0644]
src/test/resources/toscafile/service-Sotnvpninfraservice-csar.csar [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index df6a4f9..adb269d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <artifactId>jackson-dataformat-yaml</artifactId>
                        <version>2.9.6</version>
                </dependency>
+               
+               <!-- sdc tosca parser  -->
+               
+               <dependency>
+            <groupId>org.onap.sdc.sdc-tosca</groupId>
+            <artifactId>sdc-tosca</artifactId>
+            <version>1.4.6</version>
+        </dependency>
 
                <!-- jolt -->
 
index 1c4546b..e25ab4e 100644 (file)
@@ -16,7 +16,11 @@ package org.onap.nbi.apis.servicecatalog;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -129,6 +133,26 @@ public class SdcClient {
         return toscaFile;
 
     }
+    
+    public Path getServiceToscaModel(String uuid) throws IOException {
+        StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
+                .append("/").append(uuid).append(OnapComponentsUrlPaths.SDC_TOSCA_PATH);
+
+        UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
+
+        InputStream inputStream = (InputStream) callSdc(callURI.build().encode().toUri()).getBody();
+
+        return createTmpFile(inputStream);
+    }
+    
+    private Path createTmpFile(InputStream csarInputStream) throws IOException {
+        Path csarFile = Files.createTempFile("csar", ".zip");
+        Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
+
+        LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());
+
+        return csarFile;
+    }
 
     private HttpEntity<String> buildRequestHeader() {
         HttpHeaders httpHeaders = new HttpHeaders();
index 54b5486..99a1657 100644 (file)
@@ -19,6 +19,7 @@ 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.LinkedHashMap;
@@ -30,6 +31,11 @@ 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.parameters.Input;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -88,8 +94,71 @@ public class ToscaInfosProcessor {
             }
         }
     }
+    
+    public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
 
-    private List<LinkedHashMap> buildServiceSpecCharacteristicsValues(LinkedHashMap parameter, String parameterType) {
+        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();
+            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<LinkedHashMap> serviceSpecCharacteristicValues =
+                            buildServiceSpecCharacteristicsValuesFromSdc(input);
+                        mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
+                    }
+                    serviceSpecCharacteristic.add(mapParameter);
+            }
+            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));
+            }
+        }
+    }
+
+        
+    private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
+               
+       List<LinkedHashMap> 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<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);
index 6c116fb..b2ebe17 100644 (file)
@@ -19,11 +19,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
+
+import org.assertj.core.api.AbstractBooleanAssert;
 import org.junit.Test;
 import org.onap.nbi.exceptions.TechnicalException;
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
 
 
 public class ToscaInfosProcessorTest {
@@ -70,7 +74,106 @@ public class ToscaInfosProcessorTest {
 
     }
 
+    @Test
+    public void buildResponseWithSdcToscaParser() {
+
+        ClassLoader classLoader = getClass().getClassLoader();
+        Path path = new File(classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
+        List<LinkedHashMap> resources = new ArrayList<>();
+        LinkedHashMap resource1 = new LinkedHashMap();
+        resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
+        resources.add(resource1);
+        LinkedHashMap resource2 = new LinkedHashMap();
+        resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
+        resources.add(resource2);
+        LinkedHashMap response = new LinkedHashMap();
+        response.put("resourceSpecification", resources);
+       
+        try {
+                toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
+        }
+        catch(SdcToscaParserException e) {
+                throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
+        }
+        resources = (List<LinkedHashMap>) response.get("resourceSpecification");
+        List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
+        serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
+        assertThat(serviceSpecCharacteristic.get(0).get("name")).isEqualTo("sdwanconnectivity0_topology");
+        assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
+        assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
+        assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
+        assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
+        assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
+        assertThat(resources.get(0).get("modelCustomizationId")).isEqualTo("94ec574b-2306-4cbd-8214-09662b040f73");
+        assertThat(resources.get(1).get("modelCustomizationId")).isEqualTo("a7baba5d-6ac3-42b5-b47d-070841303ab1");
+        
+    }
+
+    @Test
+    public void buildResponseWithSdcToscaParserWithDefaultInputs() {
+
+        ClassLoader classLoader = getClass().getClassLoader();
+        Path path = new File(classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
+        List<LinkedHashMap> resources = new ArrayList<>();
+        LinkedHashMap resource1 = new LinkedHashMap();
+        resource1.put("id", "218df3c3-50dd-4c26-9e36-4771387bb771");
+        resources.add(resource1);
+        LinkedHashMap resource2 = new LinkedHashMap();
+        resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
+        resources.add(resource2);
+        LinkedHashMap response = new LinkedHashMap();
+        response.put("resourceSpecification", resources);
+       
+        try {
+                toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
+        }
+        catch(SdcToscaParserException e) {
+                throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
+        }
+        resources = (List<LinkedHashMap>) response.get("resourceSpecification");
+        List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
+        serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
+        assertThat(resources.get(0).get("modelCustomizationId")).isEqualTo("b44071c8-04fd-4d6b-b6af-772cbfaa1129");
+        assertThat(resources.get(1).get("modelCustomizationId")).isEqualTo("c3612284-6c67-4d8c-8b41-b699cc90e76d");
+        assertThat(serviceSpecCharacteristic.get(12).get("serviceSpecCharacteristicValue")).isNull();
+        assertThat(serviceSpecCharacteristic.get(13).get("serviceSpecCharacteristicValue")).isNotNull();
+    }
+    
+    @Test
+    public void buildResponseWithSdcToscaParserwithMetaDataMisMatch() {
 
+        ClassLoader classLoader = getClass().getClassLoader();
+        Path path = new File(classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
+        List<LinkedHashMap> resources = new ArrayList<>();
+        LinkedHashMap resource1 = new LinkedHashMap();
+        resource1.put("id", "some bad resource id no in TOSCA CSAR");
+        resources.add(resource1);
+        LinkedHashMap resource2 = new LinkedHashMap();
+        resource2.put("id", "some bad resource id no in TOSCA CSAR");
+        resources.add(resource2);
+        LinkedHashMap response = new LinkedHashMap();
+        response.put("resourceSpecification", resources);
+       
+        try {
+                toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
+        }
+        catch(SdcToscaParserException e) {
+                throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
+        }
+        resources = (List<LinkedHashMap>) response.get("resourceSpecification");
+        List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
+        serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
+        assertThat(serviceSpecCharacteristic.get(0).get("name")).isEqualTo("sdwanconnectivity0_topology");
+        assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
+        assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
+        assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
+        assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
+        assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
+        // Check that resources cannot be found in the TOSCA template
+        assertThat(resources.get(0).get("modelCustomizationId")).isNull();
+        assertThat(resources.get(1).get("modelCustomizationId")).isNull();
+        
+    }
     @Test
     public void buildResponseWithToscaInfosOk() {
 
diff --git a/src/test/resources/toscafile/service-Sdwanvpninfraservice-csar.csar b/src/test/resources/toscafile/service-Sdwanvpninfraservice-csar.csar
new file mode 100644 (file)
index 0000000..52bdfe2
Binary files /dev/null and b/src/test/resources/toscafile/service-Sdwanvpninfraservice-csar.csar differ
diff --git a/src/test/resources/toscafile/service-Sotnvpninfraservice-csar.csar b/src/test/resources/toscafile/service-Sotnvpninfraservice-csar.csar
new file mode 100644 (file)
index 0000000..b4f9693
Binary files /dev/null and b/src/test/resources/toscafile/service-Sotnvpninfraservice-csar.csar differ