Sync Integ to Master
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / MapValidator.java
index 7c86b6d..862766b 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
@@ -24,6 +24,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.apache.commons.lang.StringUtils;
 import org.openecomp.sdc.be.config.BeEcompErrorManager;
 import org.openecomp.sdc.be.model.DataTypeDefinition;
 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
@@ -31,7 +32,6 @@ import org.openecomp.sdc.common.util.JsonUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
@@ -50,16 +50,14 @@ when written line by line :
                                        key1 : val1
                                        key2 : val2
 key1 and val does not need "    " , even if val1 is a string.
-
 when written as one line : {"key1":val1 , "key2":val2}
 Keys always need " " around them.
- */
+*/
 public class MapValidator implements PropertyTypeValidator {
 
        private static MapValidator mapValidator = new MapValidator();
 
-       private static Logger log = LoggerFactory.getLogger(MapValidator.class.getName());
-       Gson gson = new Gson();
+       private static final Logger LOGGER = LoggerFactory.getLogger(MapValidator.class.getName());
 
        private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
 
@@ -72,7 +70,7 @@ public class MapValidator implements PropertyTypeValidator {
        @Override
        public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
 
-               if (value == null || value == "") {
+               if (StringUtils.isEmpty(value)) {
                        return true;
                }
                if (innerType == null) {
@@ -101,39 +99,28 @@ public class MapValidator implements PropertyTypeValidator {
                                innerValidator = ToscaPropertyType.JSON.getValidator();
                                break;
                        default:
-                               log.debug("inner Tosca Type is unknown. {}", innerToscaType);
+                               LOGGER.debug("inner Tosca Type is unknown. {}", innerToscaType);
                                return false;
                        }
 
                } else {
-                       log.debug("inner Tosca Type is: {}", innerType);
+                       LOGGER.debug("inner Tosca Type is: {}", innerType);
 
                        boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
-                       log.debug("Finish to validate value {} of map with inner type {}. result is {}",value,innerType,isValid);
+                       LOGGER.debug("Finish to validate value {} of map with inner type {}. result is {}",value,innerType,isValid);
                        return isValid;
 
                }
 
                try {
                        JsonElement jsonObject = jsonParser.parse(value);
-                       JsonObject asJsonObject = jsonObject.getAsJsonObject();
-                       Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
-                       for (Entry<String, JsonElement> entry : entrySet) {
-                               String currentKey = entry.getKey();
-                               JsonElement jsonValue = entry.getValue();
-
-                               String element = JsonUtils.toString(jsonValue);
-
-                               if (!innerValidator.isValid(element, null, allDataTypes)
-                                               || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) {
-                                       log.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue());
-                                       return false;
-                               }
-                       }
-
-                       return true;
+                       if (!jsonObject.isJsonObject()) {
+                           return false;
+            }
+                       JsonObject valueAsJson = jsonObject.getAsJsonObject();
+                       return validateJsonObject(allDataTypes, innerValidator, keyValidator, valueAsJson);
                } catch (JsonSyntaxException e) {
-                       log.debug("Failed to parse json : {}", value, e);
+                       LOGGER.debug("Failed to parse json : {}", value, e);
                        BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Validator");
                }
 
@@ -141,12 +128,30 @@ public class MapValidator implements PropertyTypeValidator {
 
        }
 
+       private boolean validateJsonObject(Map<String, DataTypeDefinition> allDataTypes, PropertyTypeValidator innerValidator, PropertyTypeValidator keyValidator, JsonObject asJsonObject) {
+               Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
+               for (Entry<String, JsonElement> entry : entrySet) {
+            String currentKey = entry.getKey();
+            JsonElement jsonValue = entry.getValue();
+
+            String element = JsonUtils.toString(jsonValue);
+
+            if (!innerValidator.isValid(element, null, allDataTypes)
+                    || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) {
+                LOGGER.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue());
+                return false;
+            }
+        }
+
+               return true;
+       }
+
        private boolean validateComplexInnerType(String value, String innerType,
                        Map<String, DataTypeDefinition> allDataTypes) {
 
                DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
                if (innerDataTypeDefinition == null) {
-                       log.debug("Data type {} cannot be found in our data types.", innerType);
+                       LOGGER.debug("Data type {} cannot be found in our data types.", innerType);
                        return false;
                }
 
@@ -163,14 +168,14 @@ public class MapValidator implements PropertyTypeValidator {
                                        boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition,
                                                        allDataTypes);
                                        if (!isValid) {
-                                               log.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey);
+                                               LOGGER.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey);
                                                return false;
                                        }
                                }
                        }
 
                } catch (Exception e) {
-                       log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
+                       LOGGER.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
                        return false;
                }