From: romaingimbert Date: Tue, 24 Apr 2018 12:02:52 +0000 (+0200) Subject: sonar clean code X-Git-Tag: 2.0.0-ONAP~28 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=e71a543f91d6b612be63c181cefcd1655e0b3b0e;p=externalapi%2Fnbi.git sonar clean code - refactor parse tosca Change-Id: Ifbdd5427a24fd549730e59859ede62dd8ba18b93 Issue-ID: EXTAPI-46 Signed-off-by: romaingimbert --- 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 48f433c..e3dbbd7 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java @@ -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; @@ -43,11 +44,11 @@ public class ToscaInfosProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class); public void buildResponseWithToscaInfos(LinkedHashMap toscaInfosTopologyTemplate, - LinkedHashMap serviceCatalogResponse) { + LinkedHashMap serviceCatalogResponse) { if (toscaInfosTopologyTemplate.get("inputs") != null) { ArrayList serviceSpecCharacteristic = new ArrayList(); LinkedHashMap toscaInfos = (LinkedHashMap) toscaInfosTopologyTemplate.get("inputs"); - for (Object key : toscaInfos.keySet()) { + for (Object key : toscaInfos.entrySet()) { String keyString = (String) key; LinkedHashMap inputParameter = (LinkedHashMap) toscaInfos.get(key); LinkedHashMap mapParameter = new LinkedHashMap(); @@ -59,7 +60,7 @@ public class ToscaInfosProcessor { mapParameter.put("required", inputParameter.get("required")); mapParameter.put("status", inputParameter.get("status")); List serviceSpecCharacteristicValues = - buildServiceSpecCharacteristicsValues(inputParameter, parameterType); + buildServiceSpecCharacteristicsValues(inputParameter, parameterType); mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues); serviceSpecCharacteristic.add(mapParameter); } @@ -69,10 +70,10 @@ public class ToscaInfosProcessor { LinkedHashMap nodeTemplate = (LinkedHashMap) toscaInfosTopologyTemplate.get("node_templates"); List resourceSpecifications = - (List) serviceCatalogResponse.get("resourceSpecification"); + (List) serviceCatalogResponse.get("resourceSpecification"); for (LinkedHashMap resourceSpecification : resourceSpecifications) { String id = (String) resourceSpecification.get("id"); - LOGGER.debug("get tosca infos for service id: " + id); + LOGGER.debug("get tosca infos for service id: {0}", id); LinkedHashMap toscaInfosFromResourceId = getToscaInfosFromResourceUUID(nodeTemplate, id); if (toscaInfosFromResourceId != null) { resourceSpecification.put("modelCustomizationId", toscaInfosFromResourceId.get("customizationUUID")); @@ -85,13 +86,13 @@ public class ToscaInfosProcessor { 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); + LOGGER.debug("get tosca infos for serviceSpecCharacteristicValues of type map or string : {0}", 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,7 +100,7 @@ public class ToscaInfosProcessor { } private void buildCharacteristicValuesFormShema(String parameterType, - List serviceSpecCharacteristicValues, Object aDefault, ArrayList entry_schema) { + List serviceSpecCharacteristicValues, Object aDefault, ArrayList entry_schema) { LinkedHashMap constraints = (LinkedHashMap) entry_schema.get(0); if (constraints != null) { ArrayList constraintsList = (ArrayList) constraints.get("constraints"); @@ -111,7 +112,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); @@ -123,7 +124,7 @@ public class ToscaInfosProcessor { private LinkedHashMap getToscaInfosFromResourceUUID(LinkedHashMap node_templates, String name) { - if (node_templates != null) { + if(node_templates!=null) { for (Object nodeTemplateObject : node_templates.values()) { LinkedHashMap nodeTemplate = (LinkedHashMap) nodeTemplateObject; LinkedHashMap metadata = (LinkedHashMap) nodeTemplate.get("metadata"); @@ -134,7 +135,6 @@ public class ToscaInfosProcessor { } } } - return null; } @@ -156,39 +156,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);