Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / PolicyType.java
index e4d1dd6..b227a31 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.
 package org.onap.sdc.toscaparser.api.elements;
 
 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
+import org.onap.sdc.toscaparser.api.utils.TOSCAVersionProperty;
+import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
 
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 
-import org.onap.sdc.toscaparser.api.utils.TOSCAVersionProperty;
-import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
-
 public class PolicyType extends StatefulEntityType {
-       
-       private static final String DERIVED_FROM = "derived_from";
-       private static final String METADATA = "metadata";
-       private static final String PROPERTIES = "properties";
-       private static final String VERSION = "version";
-       private static final String DESCRIPTION = "description";
-       private static final String TARGETS = "targets";
-       private static final String TRIGGERS = "triggers";
-       private static final String TYPE = "type";
-
-       private static final String SECTIONS[] = {
-                       DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, TARGETS, TRIGGERS, TYPE
-       };
-
-       private LinkedHashMap<String,Object> customDef;
-       private String policyDescription;
-       private Object policyVersion;
-       private LinkedHashMap<String,Object> properties;
-       private LinkedHashMap<String,Object> parentPolicies;
-       private LinkedHashMap<String,Object> metaData;
-       private ArrayList<String> targetsList;
-       
-       
-       public PolicyType(String _type, LinkedHashMap<String,Object> _customDef) {
-               super(_type,POLICY_PREFIX,_customDef);
-               
-               type = _type;
-               customDef = _customDef;
-               _validateKeys();
-               
+
+    private static final String DERIVED_FROM = "derived_from";
+    private static final String METADATA = "metadata";
+    private static final String PROPERTIES = "properties";
+    private static final String VERSION = "version";
+    private static final String DESCRIPTION = "description";
+    private static final String TARGETS = "targets";
+    private static final String TRIGGERS = "triggers";
+    private static final String TYPE = "type";
+
+    private static final String[] SECTIONS = {
+            DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, TARGETS, TRIGGERS, TYPE
+    };
+
+    private LinkedHashMap<String, Object> customDef;
+    private String policyDescription;
+    private Object policyVersion;
+    private LinkedHashMap<String, Object> properties;
+    private LinkedHashMap<String, Object> parentPolicies;
+    private LinkedHashMap<String, Object> metaData;
+    private ArrayList<String> targetsList;
+
+
+    public PolicyType(String type, LinkedHashMap<String, Object> customDef) {
+        super(type, POLICY_PREFIX, customDef);
+
+        this.type = type;
+        this.customDef = customDef;
+        validateKeys();
+
         metaData = null;
-        if(defs != null && defs.get(METADATA) != null) {
-            metaData = (LinkedHashMap<String,Object>)defs.get(METADATA);
-            _validateMetadata(metaData);
+        if (defs != null && defs.get(METADATA) != null) {
+            metaData = (LinkedHashMap<String, Object>) defs.get(METADATA);
+            validateMetadata(metaData);
         }
 
         properties = null;
-        if(defs != null && defs.get(PROPERTIES) != null) {
-               properties = (LinkedHashMap<String,Object>)defs.get(PROPERTIES);
+        if (defs != null && defs.get(PROPERTIES) != null) {
+            properties = (LinkedHashMap<String, Object>) defs.get(PROPERTIES);
         }
-        parentPolicies = _getParentPolicies();
+        parentPolicies = getParentPolicies();
 
         policyVersion = null;
-        if(defs != null && defs.get(VERSION) != null) {
+        if (defs != null && defs.get(VERSION) != null) {
             policyVersion = (new TOSCAVersionProperty(
-                defs.get(VERSION))).getVersion();
+                    defs.get(VERSION).toString())).getVersion();
         }
 
         policyDescription = null;
-        if(defs != null && defs.get(DESCRIPTION) != null) {
-               policyDescription = (String)defs.get(DESCRIPTION);
+        if (defs != null && defs.get(DESCRIPTION) != null) {
+            policyDescription = (String) defs.get(DESCRIPTION);
         }
-            
+
         targetsList = null;
-        if(defs != null && defs.get(TARGETS) != null) {
-               targetsList = (ArrayList<String>)defs.get(TARGETS);
-            _validateTargets(targetsList,customDef);
+        if (defs != null && defs.get(TARGETS) != null) {
+            targetsList = (ArrayList<String>) defs.get(TARGETS);
+            validateTargets(targetsList, this.customDef);
+        }
+
+    }
+
+    private LinkedHashMap<String, Object> getParentPolicies() {
+        LinkedHashMap<String, Object> policies = new LinkedHashMap<>();
+        String parentPolicy;
+        if (getParentType() != null) {
+            parentPolicy = getParentType().getType();
+        } else {
+            parentPolicy = null;
         }
-               
-       }
-
-       private LinkedHashMap<String,Object> _getParentPolicies() {
-               LinkedHashMap<String,Object> policies = new LinkedHashMap<>();
-               String parentPolicy;
-               if(getParentType() != null) {
-                       parentPolicy = getParentType().getType();
-               }
-               else {
-                       parentPolicy = null;
-               }
-               if(parentPolicy != null) {
-                       while(parentPolicy != null && !parentPolicy.equals("tosca.policies.Root")) {
-                               policies.put(parentPolicy, TOSCA_DEF.get(parentPolicy));
-                               parentPolicy = (String)
-                                       ((LinkedHashMap<String,Object>)policies.get(parentPolicy)).get("derived_from);");
-                       }
-               }
-               return policies;
-       }
-
-       public String getType() {
-               return type;
-       }
-
-       public PolicyType getParentType() {
+        if (parentPolicy != null) {
+            while (parentPolicy != null && !parentPolicy.equals("tosca.policies.Root")) {
+                policies.put(parentPolicy, TOSCA_DEF.get(parentPolicy));
+                parentPolicy = (String)
+                        ((LinkedHashMap<String, Object>) policies.get(parentPolicy)).get("derived_from);");
+            }
+        }
+        return policies;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public PolicyType getParentType() {
         // Return a policy statefulentity of this node is derived from
-               if(defs == null) {
-                       return null;
-               }
-        String ppolicyEntity = derivedFrom(defs);
-        if(ppolicyEntity != null) {
-            return new PolicyType(ppolicyEntity,customDef);
+        if (defs == null) {
+            return null;
+        }
+        String policyEntity = derivedFrom(defs);
+        if (policyEntity != null) {
+            return new PolicyType(policyEntity, customDef);
         }
         return null;
-       }
-       
-       public Object getPolicy(String name) {
+    }
+
+    public Object getPolicy(String name) {
         // Return the definition of a policy field by name
-        if(defs != null &&  defs.get(name) != null) {
+        if (defs != null && defs.get(name) != null) {
             return defs.get(name);
         }
         return null;
-       }
+    }
 
-       public ArrayList<String> getTargets() {
+    public ArrayList<String> getTargets() {
         // Return targets
         return targetsList;
-       }
-       
-       public String getDescription() {
-               return policyDescription;
-       }
-
-       public Object getVersion() {
-               return policyVersion;
-       }
-       
-       private void _validateKeys() {
-               for(String key: defs.keySet()) {
-                       boolean bFound = false;
-                       for(String sect: SECTIONS) {
-                               if(key.equals(sect)) {
-                                       bFound = true;
-                                       break;
-                               }
-                       }
-                       if(!bFound) {
+    }
+
+    public String getDescription() {
+        return policyDescription;
+    }
+
+    public Object getVersion() {
+        return policyVersion;
+    }
+
+    private void validateKeys() {
+        for (String key : defs.keySet()) {
+            boolean bFound = false;
+            for (String sect : SECTIONS) {
+                if (key.equals(sect)) {
+                    bFound = true;
+                    break;
+                }
+            }
+            if (!bFound) {
                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE125", String.format(
-                    "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
-                    type,key))); 
-                       }
-               }
-       }
-       
-       private void _validateTargets(ArrayList<String> _targetsList,
-                                                                 LinkedHashMap<String,Object> _customDef) {
-               for(String nodetype: _targetsList) {
-                       if(_customDef.get(nodetype) == null) {
+                        "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
+                        type, key)));
+            }
+        }
+    }
+
+    private void validateTargets(ArrayList<String> targetsList,
+                                 LinkedHashMap<String, Object> customDef) {
+        for (String nodetype : targetsList) {
+            if (customDef.get(nodetype) == null) {
                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE126", String.format(
-                    "InvalidTypeError: \"%s\" defined in targets for policy \"%s\"",
-                    nodetype,type))); 
-                               
-                       }
-               }
-       }
-       private void _validateMetadata(LinkedHashMap<String,Object> _metaData) {
-               String mtype = (String)_metaData.get("type");
-               if(mtype != null && !mtype.equals("map") && !mtype.equals("tosca:map")) {
+                        "InvalidTypeError: \"%s\" defined in targets for policy \"%s\"",
+                        nodetype, type)));
+
+            }
+        }
+    }
+
+    private void validateMetadata(LinkedHashMap<String, Object> metaData) {
+        String mtype = (String) metaData.get("type");
+        if (mtype != null && !mtype.equals("map") && !mtype.equals("tosca:map")) {
             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE127", String.format(
-                "InvalidTypeError: \"%s\" defined in policy for metadata",
-                mtype))); 
-               }
-               for(String entrySchema: metaData.keySet()) {
-                       Object estob = metaData.get(entrySchema);
-                       if(estob instanceof LinkedHashMap) {
-                               String est = (String)
-                                               ((LinkedHashMap<String,Object>)estob).get("type");
-                               if(!est.equals("string")) {
-                       ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE128", String.format(
-                           "InvalidTypeError: \"%s\" defined in policy for metadata \"%s\"",
-                           est,entrySchema))); 
-                               }
-                       }
-               }
-       }
+                    "InvalidTypeError: \"%s\" defined in policy for metadata",
+                    mtype)));
+        }
+        for (String entrySchema : this.metaData.keySet()) {
+            Object estob = this.metaData.get(entrySchema);
+            if (estob instanceof LinkedHashMap) {
+                String est = (String)
+                        ((LinkedHashMap<String, Object>) estob).get("type");
+                if (!est.equals("string")) {
+                    ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE128", String.format(
+                            "InvalidTypeError: \"%s\" defined in policy for metadata \"%s\"",
+                            est, entrySchema)));
+                }
+            }
+        }
+    }
 
 }