fix sonar alert
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.java
index 48f433c..6b70a18 100644 (file)
@@ -1,14 +1,15 @@
 /**
  * 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
  *
- * 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;
 
@@ -20,6 +21,9 @@ import java.sql.Timestamp;
 import java.util.ArrayList;
 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.collections.CollectionUtils;
@@ -42,14 +46,17 @@ public class ToscaInfosProcessor {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
 
-    public void buildResponseWithToscaInfos(LinkedHashMap toscaInfosTopologyTemplate,
-        LinkedHashMap serviceCatalogResponse) {
+    public void buildResponseWithToscaInfos(Map toscaInfosTopologyTemplate,
+            Map serviceCatalogResponse) {
         if (toscaInfosTopologyTemplate.get("inputs") != null) {
             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);
+            Set<Entry<String, LinkedHashMap>> stringLinkedHashMapEntry = (Set<Entry<String, LinkedHashMap>>) toscaInfos
+                .entrySet();
+
+            for (Map.Entry<String,LinkedHashMap> key :stringLinkedHashMapEntry) {
+                String keyString = key.getKey();
+                LinkedHashMap inputParameter = key.getValue();
                 LinkedHashMap mapParameter = new LinkedHashMap();
                 String parameterType = (String) inputParameter.get("type");
                 mapParameter.put("name", keyString);
@@ -69,29 +76,29 @@ public class ToscaInfosProcessor {
         LinkedHashMap nodeTemplate = (LinkedHashMap) toscaInfosTopologyTemplate.get("node_templates");
 
         List<LinkedHashMap> resourceSpecifications =
-            (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
+                (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(nodeTemplate, id);
-            if (toscaInfosFromResourceId != null) {
-                resourceSpecification.put("modelCustomizationId", toscaInfosFromResourceId.get("customizationUUID"));
-                resourceSpecification.put("modelCustomizationName", toscaInfosFromResourceId.get("name"));
+            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"));
+                }
             }
-
         }
     }
 
     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);
+            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.isNotEmpty(entrySchema)) {
                     buildCharacteristicValuesFormShema(parameterType, serviceSpecCharacteristicValues, aDefault,
-                        entrySchema);
+                            entrySchema);
                 }
             }
         }
@@ -99,8 +106,8 @@ public class ToscaInfosProcessor {
     }
 
     private void buildCharacteristicValuesFormShema(String parameterType,
-        List<LinkedHashMap> serviceSpecCharacteristicValues, Object aDefault, ArrayList entry_schema) {
-        LinkedHashMap constraints = (LinkedHashMap) entry_schema.get(0);
+            List<LinkedHashMap> serviceSpecCharacteristicValues, Object aDefault, ArrayList entrySchema) {
+        LinkedHashMap constraints = (LinkedHashMap) entrySchema.get(0);
         if (constraints != null) {
             ArrayList constraintsList = (ArrayList) constraints.get("constraints");
             if (CollectionUtils.isNotEmpty(constraintsList)) {
@@ -111,7 +118,7 @@ public class ToscaInfosProcessor {
                         String stringValue = value.toString();
                         LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
                         serviceSpecCharacteristicValue.put("isDefault",
-                            aDefault != null && aDefault.toString().equals(stringValue));
+                                aDefault != null && aDefault.toString().equals(stringValue));
                         serviceSpecCharacteristicValue.put("value", stringValue);
                         serviceSpecCharacteristicValue.put("valueType", parameterType);
                         serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
@@ -122,29 +129,30 @@ public class ToscaInfosProcessor {
     }
 
 
-    private LinkedHashMap getToscaInfosFromResourceUUID(LinkedHashMap node_templates, String name) {
-        if (node_templates != null) {
-            for (Object nodeTemplateObject : node_templates.values()) {
+    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");
-                String metadataUUID = (String) metadata.get("UUID");
-                String metadataType = (String) metadata.get("type");
-                if ("VF".equalsIgnoreCase(metadataType) && name.equalsIgnoreCase(metadataUUID)) {
-                    return 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 LinkedHashMap getToscaInfos(LinkedHashMap sdcResponse) {
+    public Map getToscaInfos(Map sdcResponse) {
 
         LinkedHashMap topologyTemplate = null;
 
         String toscaModelUrl = (String) sdcResponse.get("toscaModelURL");
-        String serviceId = (String) sdcResponse.get("uuid");
+        String serviceId = (String) sdcResponse.get("id");
         File toscaFile = sdcClient.callGetWithAttachment(toscaModelUrl);
         Timestamp timestamp = new Timestamp(System.currentTimeMillis());
         String tempFolderName = serviceId + timestamp;
@@ -156,39 +164,48 @@ public class ToscaInfosProcessor {
             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");
-            }
+            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);
+        }
+
+    }
+
+    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) {
-                throw new NullPointerException("no topology_template node in tosca file");
+            if (toscaFileHashMap.get("topology_template") != null) {
+                topologyTemplate = (LinkedHashMap) toscaFileHashMap.get("topology_template");
+            } else {
+                LOGGER.error("no Entry-Definitions node in TOSCA.meta");
             }
-            topologyTemplate = (LinkedHashMap) toscaFileHashMap.get("topology_template");
-
-        }  catch (NullPointerException e) {
-            LOGGER.warn("unable to parse tosca file for id : " + serviceId, e);
-            return null;
+        } else {
+            LOGGER.error("no topology_template node in tosca file");
+        }
+        return topologyTemplate;
+    }
 
-        }finally {
 
-            try {
+    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);
-                LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
-                FileUtils.forceDelete(toscaFile);
-                return topologyTemplate;
-
-            } catch (IOException e) {
-                LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e);
-                return null;
             }
+            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);
@@ -248,14 +265,10 @@ public class ToscaInfosProcessor {
                         while ((len = zis.read(buffer)) > 0) {
                             fos.write(buffer, 0, len);
                         }
-
-                        fos.close();
                     }
                     ze = zis.getNextEntry();
                 }
-
                 zis.closeEntry();
-                zis.close();
             }
 
             LOGGER.debug("Done");