configuration overriding capabilities. 75/28875/4
authorPriyanshuAgarwal <pagarwal@amdocs.com>
Mon, 22 Jan 2018 18:15:01 +0000 (20:15 +0200)
committerpriyanshu <pagarwal@amdocs.com>
Tue, 6 Feb 2018 20:31:09 +0000 (02:01 +0530)
Updated files in SDC Parser Library.

Change-Id: I885f0a155e52e337f776f74ef5675c080eecfaa8
Issue-ID: SDC-955
Signed-off-by: priyanshu <pagarwal@amdocs.com>
src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java
src/main/java/org/openecomp/sdc/tosca/parser/config/ErrorInfo.java
src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java
src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java
src/main/resources/config/error-configuration.yaml
src/test/java/org/openecomp/sdc/impl/ToscaParserConfigurationTest.java
src/test/java/org/openecomp/sdc/impl/ToscaParserMetadataTest.java
src/test/resources/config/error-configuration-test.yaml [new file with mode: 0644]
src/test/resources/config/error-configuration.yaml
src/test/resources/csars/service-missing-csar-meta-file.csar [new file with mode: 0644]

index 72c06ed..f2fc903 100644 (file)
@@ -118,4 +118,7 @@ public class ConfigurationManager {
        public Configuration getConfiguration() {
                return (Configuration) configurations.get((Configuration.class.getSimpleName()));
        }
+       public void setErrorConfiguration(String fileName) {
+               loadConfigurationClass(ErrorConfiguration.class, fileName);
+       }
 }
index 01df115..34983ef 100644 (file)
@@ -24,6 +24,7 @@ public class ErrorInfo {
 
        private String code;
        private String message;
+       private boolean failOnError;
 
        public String getCode() {
                return code;
@@ -40,10 +41,19 @@ public class ErrorInfo {
        public void setMessage(String message) {
                this.message = message;
        }
+       
+       public boolean getFailOnError() {
+               return failOnError;
+       }
+
+       public void setFailOnError(boolean failOnError) {
+               this.failOnError = failOnError;
+       }
 
        public void cloneData(ErrorInfo other) {
                this.code = other.getCode();
                this.message = other.getMessage();
+               this.failOnError = other.getFailOnError();
        }
 
 }
index 7b94000..901b315 100644 (file)
@@ -27,6 +27,7 @@ import java.util.stream.Collectors;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.openecomp.sdc.tosca.parser.config.ConfigurationManager;
 import org.openecomp.sdc.toscaparser.api.CapabilityAssignments;
 import org.openecomp.sdc.tosca.parser.utils.GeneralUtility;
 import org.openecomp.sdc.toscaparser.api.RequirementAssignments;
@@ -46,12 +47,18 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper {
     private static final String PATH_DELIMITER = "#";
     private static final String CUSTOMIZATION_UUID = "customizationUUID";
     private ToscaTemplate toscaTemplate;
+    private ConfigurationManager configurationManager;
     private static Logger log = LoggerFactory.getLogger(SdcCsarHelperImpl.class.getName());
 
     public SdcCsarHelperImpl(ToscaTemplate toscaTemplate) {
         this.toscaTemplate = toscaTemplate;
     }
 
+    public SdcCsarHelperImpl(ToscaTemplate toscaTemplate, ConfigurationManager configurationManager) {
+        this.toscaTemplate = toscaTemplate;
+        this.configurationManager = configurationManager;
+    }
+
     @Override
     //Sunny flow  - covered with UT, flat and nested
     public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) {
@@ -579,24 +586,40 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper {
         return nodeTemplate.getTypeDefinition().getType();
     }
 
-       @Override
-       public String getConformanceLevel() {
-               LinkedHashMap<String, Object> csarMeta = toscaTemplate.getMetaProperties("csar.meta");
-               if (csarMeta == null){
-                       log.warn("No csar.meta file is found in CSAR - this file should hold the conformance level of the CSAR. This might be OK for older CSARs.");
-                       return null;
-               }
-
-               Object conformanceLevel = csarMeta.get("SDC-TOSCA-Definitions-Version");
-               if (conformanceLevel != null){
-                       String confLevelStr = conformanceLevel.toString();
-                       log.debug("CSAR conformance level is {}", confLevelStr);
-                       return confLevelStr;
-               } else {
-                       log.error("Invalid csar.meta file - no entry found for SDC-TOSCA-Definitions-Version key. This entry should hold the conformance level.");
-                       return null;
-               }
-       }
+    /**
+     * This methdd is returning the csarConformanceLevel for input CSAR
+     * When csarConformanceLevel is configured with failOnError as False in Error Configuration; it
+     * assigns the default value to csarConformanceLevel which is the max level provided in
+     * Configuration file
+     * @return csarConformanceLevel
+     */
+    @Override
+    public String getConformanceLevel() {
+      LinkedHashMap<String, Object> csarMeta = toscaTemplate.getMetaProperties("csar.meta");
+      if (csarMeta == null){
+        log.warn("No csar.meta file is found in CSAR - this file should hold the conformance level of the CSAR. This might be OK for older CSARs.");
+        if (configurationManager != null && !configurationManager.getErrorConfiguration()
+            .getErrorInfo("CONFORMANCE_LEVEL_ERROR").getFailOnError()){
+          String csarConLevel = configurationManager.getConfiguration().getConformanceLevel().getMaxVersion();
+          log.warn("csarConformanceLevel is not found in input csar; defaulting to max version {}" , csarConLevel);
+          return csarConLevel;
+        }
+        else {
+          log.warn("csarConformanceLevel is not found in input csar; returning null as no defaults defined in error configuration");
+          return null;
+        }
+      }
+
+      Object conformanceLevel = csarMeta.get("SDC-TOSCA-Definitions-Version");
+      if (conformanceLevel != null){
+        String confLevelStr = conformanceLevel.toString();
+        log.debug("CSAR conformance level is {}", confLevelStr);
+        return confLevelStr;
+      } else {
+        log.error("Invalid csar.meta file - no entry found for SDC-TOSCA-Definitions-Version key. This entry should hold the conformance level.");
+        return null;
+      }
+    }
        
        
        @Override
index f1a03b7..9fc59f2 100644 (file)
@@ -77,7 +77,7 @@ public class SdcToscaParserFactory {
             } catch (JToscaException e) {\r
                 throwSdcToscaParserException(e);\r
             }\r
-            SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca);\r
+            SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca, configurationManager);\r
             String cSarConformanceLevel = sdcCsarHelperImpl.getConformanceLevel();\r
             validateCsarVersion(cSarConformanceLevel);\r
             try {\r
index 3febd33..58d1de3 100644 (file)
@@ -2,17 +2,21 @@
 errors:
     FILE_NOT_FOUND: {
         code: TP0001,
+        failOnError: true,
         message: "Error: CSAR file not found."
     }
     BAD_FORMAT: {
         code: TP0002,
+        failOnError: true,
         message: "Error: CSAR file bad format. Check the log for details."
     }
     CONFORMANCE_LEVEL_ERROR: {
         code: TP0003,
+        failOnError: true,
         message: "Error: CSAR version is unsupported. Parser supports versions %s to %s." 
     }
     GENERAL_ERROR: {
         code: TP0004,
+        failOnError: true,
         message: "Error: an unexpected internal error occured."
-    }
\ No newline at end of file
+    }
index 8c96303..b815064 100644 (file)
@@ -1,12 +1,14 @@
 package org.openecomp.sdc.impl;
 
 import org.openecomp.sdc.tosca.parser.config.ErrorConfiguration;
+import org.openecomp.sdc.tosca.parser.config.JtoscaValidationIssueConfiguration;
 import org.testng.annotations.Test;
 import org.openecomp.sdc.tosca.parser.config.Configuration;
 import org.openecomp.sdc.tosca.parser.config.ConfigurationManager;
 
 import java.io.IOException;
 
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
 public class ToscaParserConfigurationTest extends SdcToscaParserBasicTest {
@@ -28,4 +30,22 @@ public class ToscaParserConfigurationTest extends SdcToscaParserBasicTest {
         assertNotNull(errorConfig.getErrors());
     }
 
+    @Test
+    public void testSetErrorConfiguration() throws IOException {
+        ConfigurationManager configurationManager = ConfigurationManager.getInstance();
+        configurationManager.setErrorConfiguration("error-configuration-test.yaml");
+        ErrorConfiguration errorConfig = configurationManager.getErrorConfiguration();
+        assertEquals(false, errorConfig.getErrorInfo("CONFORMANCE_LEVEL_ERROR").getFailOnError());
+        assertEquals(true, errorConfig.getErrorInfo("FILE_NOT_FOUND").getFailOnError());
+    }
+
+    @Test
+    public void testSetJtoscaValidationIssueConfiguration() throws IOException {
+        ConfigurationManager configurationManager = ConfigurationManager.getInstance();
+        configurationManager.setJtoscaValidationIssueConfiguration(
+            "jtosca-validation-issue-configuration-test.yaml");
+        JtoscaValidationIssueConfiguration issueConfig = configurationManager
+            .getJtoscaValidationIssueConfiguration();
+        assertNotNull(issueConfig);
+    }
 }
index 4e5c9bc..65c013a 100644 (file)
@@ -1,8 +1,11 @@
 package org.openecomp.sdc.impl;
 
+import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.openecomp.sdc.tosca.parser.config.ConfigurationManager;
+import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
-import org.testng.annotations.Test;
 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
+import org.testng.annotations.Test;
 
 import java.util.List;
 import java.util.Map;
@@ -187,5 +190,30 @@ public class ToscaParserMetadataTest extends SdcToscaParserBasicTest {
         assertEquals(serviceEcompNaming, "true");
     }
     //endregion
+
+  @Test
+  public void testCSARMissingConformanceLevelWithCustomErrorConfig() throws
+      SdcToscaParserException {
+    ConfigurationManager configurationManager = ConfigurationManager.getInstance();
+    configurationManager.setErrorConfiguration("error-configuration-test.yaml");
+    factory.setConfigurationManager(configurationManager);
+
+    ISdcCsarHelper missingCSARMetaCsarCustomConfig = getCsarHelper
+        ("csars/service-missing-csar-meta-file.csar");
+    String conformanceLevel = missingCSARMetaCsarCustomConfig.getConformanceLevel();
+    assertNotNull(conformanceLevel);
+    assertEquals(conformanceLevel, configurationManager.getConfiguration().getConformanceLevel()
+        .getMaxVersion());
+
+    configurationManager.setErrorConfiguration("error-configuration.yaml");
+    factory.setConfigurationManager(configurationManager);
+  }
+
+  @Test(expectedExceptions = SdcToscaParserException.class)
+  public void testCSARMissingConformanceLevelWithDefaultErrorConfig() throws
+      SdcToscaParserException {
+    ISdcCsarHelper missingCSARMetaCsarDefaultConfig = getCsarHelper("csars/service-missing-csar-meta-file.csar");
+    missingCSARMetaCsarDefaultConfig.getConformanceLevel();
+  }
    
 }
diff --git a/src/test/resources/config/error-configuration-test.yaml b/src/test/resources/config/error-configuration-test.yaml
new file mode 100644 (file)
index 0000000..f5c20aa
--- /dev/null
@@ -0,0 +1,22 @@
+# Errors
+errors:
+    FILE_NOT_FOUND: {
+        code: TP0001,
+        failOnError: true,
+        message: "Error: CSAR file not found."
+    }
+    BAD_FORMAT: {
+        code: TP0002,
+        failOnError: true,
+        message: "Error: CSAR file bad format. Check the log for details."
+    }
+    CONFORMANCE_LEVEL_ERROR: {
+        code: TP0003,
+        failOnError: false,
+        message: "Error: CSAR version is unsupported. Parser supports versions %s to %s." 
+    }
+    GENERAL_ERROR: {
+        code: TP0004,
+        failOnError: true,
+        message: "Error: an unexpected internal error occured."
+    }
\ No newline at end of file
index 3febd33..44173cd 100644 (file)
@@ -2,17 +2,21 @@
 errors:
     FILE_NOT_FOUND: {
         code: TP0001,
+        failOnError: true,
         message: "Error: CSAR file not found."
     }
     BAD_FORMAT: {
         code: TP0002,
+        failOnError: true,
         message: "Error: CSAR file bad format. Check the log for details."
     }
     CONFORMANCE_LEVEL_ERROR: {
         code: TP0003,
+        failOnError: true,
         message: "Error: CSAR version is unsupported. Parser supports versions %s to %s." 
     }
     GENERAL_ERROR: {
         code: TP0004,
+        failOnError: true,
         message: "Error: an unexpected internal error occured."
     }
\ No newline at end of file
diff --git a/src/test/resources/csars/service-missing-csar-meta-file.csar b/src/test/resources/csars/service-missing-csar-meta-file.csar
new file mode 100644 (file)
index 0000000..7c75314
Binary files /dev/null and b/src/test/resources/csars/service-missing-csar-meta-file.csar differ