From: Natalia Sheshukov Date: Thu, 22 Feb 2018 14:40:51 +0000 (+0200) Subject: [367741] vLAN Tagging - Support Tosca Policies X-Git-Tag: 1.6.6~22^2~50 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=5e7130be2350a29d8dbc65f52cdca596a188114f;p=sdc%2Fsdc-tosca.git [367741] vLAN Tagging - Support Tosca Policies Change-Id: Icb0739eaf5abd071c45b4d7c49d7b412e10c8c4d Issue-ID: SDC-1056 Signed-off-by: Natalia Sheshukov --- diff --git a/LICENSE.TXT b/LICENSE.TXT index 2b91311..724329f 100644 --- a/LICENSE.TXT +++ b/LICENSE.TXT @@ -1,8 +1,8 @@ -/* +/* * ============LICENSE_START========================================== * =================================================================== -* Copyright © 2018 AT&T Intellectual Property. -* Copyright © 2018 Amdocs +* Copyright © 2017 AT&T Intellectual Property. +* Copyright © 2017 Amdocs * All rights reserved. * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/pom.xml b/pom.xml index a6afeee..63da98a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.openecomp.sdc.jtosca jtosca - 1.2.1-SNAPSHOT + 1.2.2-SNAPSHOT sdc-jtosca diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java b/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java index b2a0da7..5e94378 100644 --- a/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java +++ b/src/main/java/org/openecomp/sdc/toscaparser/api/ImportsLoader.java @@ -28,7 +28,6 @@ public class ImportsLoader { private ArrayList typeDefinitionList; private LinkedHashMap customDefs; - private LinkedHashMap allCustomDefs; private ArrayList> nestedToscaTpls; private LinkedHashMap repositories; @@ -40,7 +39,6 @@ public class ImportsLoader { this.importslist = _importslist; customDefs = new LinkedHashMap(); - allCustomDefs = new LinkedHashMap(); nestedToscaTpls = new ArrayList>(); if((_path == null || _path.isEmpty()) && tpl == null) { //msg = _('Input tosca template is not provided.') @@ -67,7 +65,7 @@ public class ImportsLoader { } public LinkedHashMap getCustomDefs() { - return allCustomDefs; + return customDefs; } public ArrayList> getNestedToscaTpls() { @@ -133,50 +131,33 @@ public class ImportsLoader { } } - /** - * This method is used to get consolidated custom definitions by passing custom Types from - * each import. The resultant collection is then passed back which contains all import - * definitions - * - * @param customType the custom type - * @param namespacePrefix the namespace prefix - */ - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") private void _updateCustomDefs(LinkedHashMap customType, String namespacePrefix) { - LinkedHashMap outerCustomTypes; - for(String typeDef: typeDefinitionList) { - if(typeDef.equals("imports")) { - customDefs.put("imports", customType.get(typeDef)); - if (allCustomDefs.isEmpty() || allCustomDefs.get("imports") == null){ - allCustomDefs.put("imports",customType.get(typeDef)); - } - else if (customType.get(typeDef) != null){ - Set allCustomImports = new HashSet<>((ArrayList)allCustomDefs.get("imports")); - allCustomImports.addAll((ArrayList) customType.get(typeDef)); - allCustomDefs.put("imports", new ArrayList<>(allCustomImports)); - } - } - else { - outerCustomTypes = (LinkedHashMap)customType.get(typeDef); - if(outerCustomTypes != null) { - if(namespacePrefix != null && !namespacePrefix.isEmpty()) { - LinkedHashMap prefixCustomTypes = new LinkedHashMap(); - for(Map.Entry me: outerCustomTypes.entrySet()) { - String typeDefKey = me.getKey(); - String nameSpacePrefixToKey = namespacePrefix + "." + typeDefKey; - prefixCustomTypes.put(nameSpacePrefixToKey, outerCustomTypes.get(typeDefKey)); - } - customDefs.putAll(prefixCustomTypes); - allCustomDefs.putAll(prefixCustomTypes); - } - else { - customDefs.putAll(outerCustomTypes); - allCustomDefs.putAll(outerCustomTypes); - } - } - } - } - } + LinkedHashMap outerCustomTypes;// = new LinkedHashMap(); + for(String typeDef: typeDefinitionList) { + if(typeDef.equals("imports")) { + // imports are ArrayList... + customDefs.put("imports",(ArrayList)customType.get(typeDef)); + } + else { + outerCustomTypes = (LinkedHashMap)customType.get(typeDef); + if(outerCustomTypes != null) { + if(namespacePrefix != null && !namespacePrefix.isEmpty()) { + LinkedHashMap prefixCustomTypes = new LinkedHashMap(); + for(Map.Entry me: outerCustomTypes.entrySet()) { + String typeDefKey = me.getKey(); + String nameSpacePrefixToKey = namespacePrefix + "." + typeDefKey; + prefixCustomTypes.put(nameSpacePrefixToKey, outerCustomTypes.get(typeDefKey)); + } + customDefs.putAll(prefixCustomTypes); + } + else { + customDefs.putAll(outerCustomTypes); + } + } + } + } + } private void _updateNestedToscaTpls(String fullFileName,LinkedHashMap customTpl) { if(fullFileName != null && customTpl != null) { diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/Policy.java b/src/main/java/org/openecomp/sdc/toscaparser/api/Policy.java index 26805bd..1f536f8 100644 --- a/src/main/java/org/openecomp/sdc/toscaparser/api/Policy.java +++ b/src/main/java/org/openecomp/sdc/toscaparser/api/Policy.java @@ -119,6 +119,12 @@ public class Policy extends EntityTemplate { ", properties=" + properties + '}'; } + + public int compareTo(Policy other){ + if(this.equals(other)) + return 0; + return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName()); + } } /*python diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java b/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java index e96ca56..7553414 100644 --- a/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java +++ b/src/main/java/org/openecomp/sdc/toscaparser/api/ToscaTemplate.java @@ -9,9 +9,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.nio.file.Files; -import java.util.function.Predicate; -import java.nio.file.Paths; import org.openecomp.sdc.toscaparser.api.common.ValidationIssueCollector; import org.openecomp.sdc.toscaparser.api.common.JToscaException; @@ -73,7 +70,6 @@ public class ToscaTemplate extends Object { private boolean isFile; private String path; private String inputPath; - private String rootPath; private LinkedHashMap parsedParams; private boolean resolveGetInput; private LinkedHashMap tpl; @@ -95,7 +91,6 @@ public class ToscaTemplate extends Object { private String csarTempDir; private int nestingLoopCounter; private LinkedHashMap> metaProperties; - private Set processedImports; public ToscaTemplate(String _path, LinkedHashMap _parsedParams, @@ -198,9 +193,6 @@ public class ToscaTemplate extends Object { if(tpl != null) { parsedParams = _parsedParams; _validateField(); - this.rootPath = path; - this.processedImports = new HashSet(); - this.imports = _tplImports(); this.version = _tplVersion(); this.metaData = _tplMetaData(); this.relationshipTypes = _tplRelationshipTypes(); @@ -313,200 +305,30 @@ public class ToscaTemplate extends Object { private ArrayList _policies() { return topologyTemplate.getPolicies(); } - - /** - * This method is used to get consolidated custom definitions from all imports - * It is logically divided in two parts to handle imports; map and list formats. - * Before processing the imports; it sorts them to make sure the current directory imports are - * being processed first and then others. Once sorted; it processes each import one by one in - * recursive manner. - * To avoid cyclic dependency among imports; this method uses a set to keep track of all - * imports which are already processed and filters the imports which occurs more than once. - * - * @param alImports all imports which needs to be processed - * @return the linked hash map containing all import definitions - */ - private LinkedHashMap _getAllCustomDefs(Object alImports) { - + + private LinkedHashMap _getAllCustomDefs(ArrayList alImports) { + String types[] = { - IMPORTS, NODE_TYPES, CAPABILITY_TYPES, RELATIONSHIP_TYPES, - DATA_TYPES, INTERFACE_TYPES, POLICY_TYPES, GROUP_TYPES + IMPORTS, NODE_TYPES, CAPABILITY_TYPES, RELATIONSHIP_TYPES, + DATA_TYPES, INTERFACE_TYPES, POLICY_TYPES, GROUP_TYPES }; - LinkedHashMap customDefsFinal = new LinkedHashMap<>(); - - List> imports = (List>) alImports; - if (imports != null && !imports.isEmpty()) { - if (imports.get(0) instanceof LinkedHashMap) { - imports = sortImports(imports); - - for (Map map : imports) { - List> singleImportList = new ArrayList(); - singleImportList.add(map); - - Map importNameDetails = getValidFileNameForImportReference(singleImportList); - singleImportList = filterImportsForRecursion(singleImportList, importNameDetails); - - if(!singleImportList.get(0).isEmpty()){ - LinkedHashMap customDefs = _getCustomTypes(types, new ArrayList<>(singleImportList)); - processedImports.add(importNameDetails.get("importFileName")); - - if (customDefs != null) { - customDefsFinal.putAll(customDefs); - - if (customDefs.get(IMPORTS) != null) { - resetPathForRecursiveImports(importNameDetails.get("importRelativeName")); - LinkedHashMap importDefs = _getAllCustomDefs(customDefs.get(IMPORTS)); - customDefsFinal.putAll(importDefs); - } - } - } - } - } else { - LinkedHashMap customDefs = _getCustomTypes(types, new ArrayList<>(imports)); - if (customDefs != null) { - customDefsFinal.putAll(customDefs); - - if (customDefs.get(IMPORTS) != null) { - LinkedHashMap importDefs = _getAllCustomDefs(customDefs.get(IMPORTS)); - customDefsFinal.putAll(importDefs); - } - } + LinkedHashMap customDefsFinal = new LinkedHashMap(); + LinkedHashMap customDefs = _getCustomTypes(types,alImports); + if(customDefs != null) { + customDefsFinal.putAll(customDefs); + if(customDefs.get(IMPORTS) != null) { + @SuppressWarnings("unchecked") + LinkedHashMap importDefs = _getAllCustomDefs((ArrayList)customDefs.get(IMPORTS)); + customDefsFinal.putAll(importDefs); } } - - // As imports are not custom_types, remove from the dict - customDefsFinal.remove(IMPORTS); + + // As imports are not custom_types, remove from the dict + customDefsFinal.remove(IMPORTS); return customDefsFinal; } - /** - * This method is used to sort the imports in order so that same directory - * imports will be processed first - * - * @param customImports the custom imports - * @return the sorted list of imports - */ - private List> sortImports(List> customImports){ - List> finalList1 = new ArrayList<>(); - List> finalList2 = new ArrayList<>(); - Iterator> itr = customImports.iterator(); - while(itr.hasNext()) { - Map innerMap = itr.next(); - if (innerMap.toString().contains("../")) { - finalList2.add(innerMap); - itr.remove(); - } - else if (innerMap.toString().contains("/")) { - finalList1.add(innerMap); - itr.remove(); - } - } - - customImports.addAll(finalList1); - customImports.addAll(finalList2); - return customImports; - } - - /** - * This method is used to reset PATH variable after processing of current import file is done - * This is required because of relative path nature of imports present in files. - * - * @param currImportRelativeName the current import relative name - */ - private void resetPathForRecursiveImports(String currImportRelativeName){ - path = getPath(path, currImportRelativeName); - } - - /** - * This is a recursive method which starts from current import and then recursively finds a - * valid path relative to current import file name. - * By doing this it handles all nested hierarchy of imports defined in CSARs - * - * @param path the path - * @param importFileName the import file name - * @return the string containing updated path value - */ - private String getPath(String path, String importFileName){ - String tempFullPath = (Paths.get(path).toAbsolutePath().getParent() - .toString() + File.separator + importFileName.replace("../", "")).replace('\\', '/'); - String tempPartialPath = (Paths.get(path).toAbsolutePath().getParent().toString()).replace('\\', '/'); - if(Files.exists(Paths.get(tempFullPath))) - return tempFullPath; - else - return getPath(tempPartialPath, importFileName); - } - - /** - * This method is used to get full path name for the file which needs to be processed. It helps - * in situation where files are present in different directory and are references as relative - * paths. - * - * @param customImports the custom imports - * @return the map containing import file full and relative paths - */ - private Map getValidFileNameForImportReference(List> - customImports){ - String importFileName; - Map retMap = new HashMap<>(); - for (Map map1 : customImports) { - for (Map.Entry entry : map1.entrySet()) { - Map innerMostMap = (Map) entry.getValue(); - Iterator> it = innerMostMap.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry val = it.next(); - if(val.getValue().contains("/")){ - importFileName = (Paths.get(rootPath).toAbsolutePath().getParent().toString() + File - .separator + val.getValue().replace("../", "")).replace('\\', '/'); - } - else { - importFileName = (Paths.get(path).toAbsolutePath().getParent().toString() + File - .separator + val.getValue().replace("../", "")).replace('\\', '/'); - } - retMap.put("importFileName", importFileName); - retMap.put("importRelativeName", val.getValue()); - } - } - } - return retMap; - } - - /** - * This method is used to filter the imports which already gets processed in previous step. - * It handles the use case of cyclic dependency in imports which may cause Stack Overflow - * exception - * - * @param customImports the custom imports - * @param importNameDetails the import name details - * @return the list containing filtered imports - */ - private List> filterImportsForRecursion(List> - customImports, Map importNameDetails){ - for (Map map1 : customImports) { - for (Map.Entry entry : map1.entrySet()) { - Map innerMostMap = (Map) entry.getValue(); - Iterator> it = innerMostMap.entrySet().iterator(); - while (it.hasNext()) { - it.next(); - if (processedImports.contains(importNameDetails.get("importFileName"))) { - it.remove(); - } - } - } - } - - // Remove Empty elements - Iterator> itr = customImports.iterator(); - while(itr.hasNext()) { - Map innerMap = itr.next(); - Predicate predicate = p-> p.values().isEmpty(); - innerMap.values().removeIf(predicate); - } - - return customImports; - } - @SuppressWarnings("unchecked") private LinkedHashMap _getCustomTypes(Object typeDefinitions,ArrayList alImports) { @@ -574,8 +396,6 @@ public class ToscaTemplate extends Object { log.error("ToscaTemplate - _handleNestedToscaTemplatesWithTopology - Nested Topologies Loop: too many levels, aborting"); return; } - // Reset Processed Imports for nested templates - this.processedImports = new HashSet<>(); for(Map.Entry me: nestedToscaTplsWithTopology.entrySet()) { String fname = me.getKey(); LinkedHashMap toscaTpl = @@ -836,6 +656,10 @@ public class ToscaTemplate extends Object { return nestedToscaTemplatesWithTopology; } + public ConcurrentHashMap getNestedTopologyTemplates() { + return nestedToscaTplsWithTopology; + } + @Override public String toString() { return "ToscaTemplate{" + diff --git a/src/test/java/org/openecomp/sdc/toscaparser/api/JToscaImportTest.java b/src/test/java/org/openecomp/sdc/toscaparser/api/JToscaImportTest.java deleted file mode 100644 index c8a30fa..0000000 --- a/src/test/java/org/openecomp/sdc/toscaparser/api/JToscaImportTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.openecomp.sdc.toscaparser.api; - -import org.junit.Test; -import org.openecomp.sdc.toscaparser.api.common.JToscaException; -import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.Assert.assertEquals; - -public class JToscaImportTest { - - @Test - public void testNoMissingTypeValidationError() throws JToscaException { - String fileStr = JToscaImportTest.class.getClassLoader().getResource - ("csars/sdc-onboarding_csar.csar").getFile(); - File file = new File(fileStr); - new ToscaTemplate(file.getAbsolutePath(), null, true, null); - List missingTypeErrors = ThreadLocalsHolder.getCollector() - .getValidationIssueReport() - .stream() - .filter(s -> s.contains("JE136")) - .collect(Collectors.toList()); - assertEquals(0, missingTypeErrors.size()); - } - - @Test - public void testNoStackOverFlowError() { - Exception jte = null; - try { - String fileStr = JToscaImportTest.class.getClassLoader().getResource - ("csars/sdc-onboarding_csar.csar").getFile(); - File file = new File(fileStr); - new ToscaTemplate(file.getAbsolutePath(), null, true, null); - } catch (Exception e){ - jte = e; - } - assertEquals(null, jte); - } - - @Test - public void testNoInvalidImports() throws JToscaException { - List fileNames = new ArrayList<>(); - fileNames.add("csars/tmpCSAR_Huawei_vSPGW_fixed.csar"); - fileNames.add("csars/sdc-onboarding_csar.csar"); - fileNames.add("csars/resource-Spgw-csar-ZTE.csar"); - - for (String fileName : fileNames) { - String fileStr = JToscaImportTest.class.getClassLoader().getResource(fileName).getFile(); - File file = new File(fileStr); - new ToscaTemplate(file.getAbsolutePath(), null, true, null); - List invalidImportErrors = ThreadLocalsHolder.getCollector() - .getValidationIssueReport() - .stream() - .filter(s -> s.contains("JE195")) - .collect(Collectors.toList()); - assertEquals(0, invalidImportErrors.size()); - } - } - -} diff --git a/src/test/resources/csars/resource-Spgw-csar-ZTE.csar b/src/test/resources/csars/resource-Spgw-csar-ZTE.csar deleted file mode 100644 index 58c3ddd..0000000 Binary files a/src/test/resources/csars/resource-Spgw-csar-ZTE.csar and /dev/null differ diff --git a/src/test/resources/csars/sdc-onboarding_csar.csar b/src/test/resources/csars/sdc-onboarding_csar.csar deleted file mode 100644 index e1c3267..0000000 Binary files a/src/test/resources/csars/sdc-onboarding_csar.csar and /dev/null differ diff --git a/version.properties b/version.properties index 10a6323..a6be0db 100644 --- a/version.properties +++ b/version.properties @@ -5,7 +5,7 @@ major=1 minor=2 -patch=1 +patch=2 base_version=${major}.${minor}.${patch}