From 6851fef8dd97fd0cf3f2602c394296befc706e44 Mon Sep 17 00:00:00 2001 From: rama-huawei Date: Mon, 11 Sep 2017 11:48:38 +0530 Subject: Fix for Sonar blocking issues DCAEGEN2-93 Change-Id: I16462b216908010c582bad25d1a88281122b8d1b Signed-off-by: rama-huawei --- .../dcae/controller/operation/utils/DocUtils.java | 502 +++++++++++---------- 1 file changed, 262 insertions(+), 240 deletions(-) diff --git a/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java index 7b30cf5..57d22bd 100644 --- a/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java +++ b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java @@ -18,292 +18,314 @@ * limitations under the License. * ============LICENSE_END============================================ */ - + package org.openecomp.dcae.controller.operation.utils; +import org.json.JSONArray; +import org.json.JSONObject; +import org.openecomp.ncomp.utils.maps.CounterMap; +import org.openecomp.ncomp.webservice.utils.JsonUtils; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.json.JSONArray; -import org.json.JSONObject; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; - -import org.openecomp.ncomp.utils.maps.CounterMap; -import org.openecomp.ncomp.webservice.utils.JsonUtils; - import static org.openecomp.ncomp.sirius.manager.console.Utils.*; public class DocUtils { - protected String env; - protected static String root = "/home/vagrant/git/org.openecomp.dcae.controller.operation"; - protected String release; + protected String env; + protected static String root = "/home/vagrant/git/org.openecomp.dcae.controller.operation"; + protected String release; // public DocUtils(String release, String env) { // super(); // this.release = release; // this.env = env; // } - - public DocUtils(String release, String env, String root1, String root2) { - super(); - this.release = release; - this.env = env; - if (root1 != null) root = root1; - } + + public DocUtils(String release, String env, String root1, String root2) { + super(); + this.release = release; + this.env = env; + if (root1 != null) { + root = root1; + } + } - @SuppressWarnings("rawtypes") - public Map tenants() { - String fileName = release + "-" + env + "/tenants.yaml"; - Map o = (Map) file2object(fileName); - return o; - } + @SuppressWarnings("rawtypes") + public Map tenants() { + String fileName = release + "-" + env + "/tenants.yaml"; + Map o = (Map) file2object(fileName); + return o; + } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public Map vms() { - LinkedHashMap> s = new LinkedHashMap>(); - LinkedHashMap> ss = new LinkedHashMap>(); - CounterMap index = new CounterMap(); - Map locationTypes = locationTypes(); - Map vmTypes = (Map) types("vm"); - Map tenants = tenants(); - for (String t : locationTypes.keySet()) { - for (String tenantKey : tenants.keySet()) { - Map tenant = (Map) tenants.get(tenantKey); - if (! t.equals(tenant.get("type"))) continue; - Map locationType = (Map) locationTypes.get(t); - Map vmDeployments = (Map) locationType.get("vm-deployments"); - for (String vmDeploymentKey : vmDeployments.keySet()) { - Map vmDeployment = (Map) vmDeployments.get(vmDeploymentKey); - Map vmType = (Map) vmTypes.get(vmDeployment.get("vm-type")); - if (s.get(vmDeploymentKey) == null) { - LinkedHashMap s1 = new LinkedHashMap(); - s.put(vmDeploymentKey, s1); - s1.put("vm-type", vmDeployment.get("vm-type")); - s1.put("release", vmDeployment.get("release")); - s1.put("milestone", vmDeployment.get("milestone")); - boolean useController = (Boolean) get(vmType,"lifecycle-management", "use-dcae-manager",new Boolean(false)); - s1.put("use-controller", useController); - LinkedHashMap s2 = new LinkedHashMap(); - s1.put("locations", s2); - ss.put(vmDeploymentKey, s2); - } + @SuppressWarnings({"rawtypes", "unchecked"}) + public Map vms() { + LinkedHashMap> s = new LinkedHashMap>(); + LinkedHashMap> ss = new LinkedHashMap>(); + CounterMap index = new CounterMap(); + Map locationTypes = locationTypes(); + Map vmTypes = (Map) types("vm"); + Map tenants = tenants(); + for (String t : locationTypes.keySet()) { + for (String tenantKey : tenants.keySet()) { + Map tenant = (Map) tenants.get(tenantKey); + if (!t.equals(tenant.get("type"))) { + continue; + } + Map locationType = (Map) locationTypes.get(t); + Map vmDeployments = (Map) locationType.get("vm-deployments"); + for (String vmDeploymentKey : vmDeployments.keySet()) { + Map vmDeployment = (Map) vmDeployments.get(vmDeploymentKey); + Map vmType = (Map) vmTypes.get(vmDeployment.get("vm-type")); + if (s.get(vmDeploymentKey) == null) { + LinkedHashMap s1 = new LinkedHashMap(); + s.put(vmDeploymentKey, s1); + s1.put("vm-type", vmDeployment.get("vm-type")); + s1.put("release", vmDeployment.get("release")); + s1.put("milestone", vmDeployment.get("milestone")); + boolean useController = (Boolean) get(vmType, "lifecycle-management", "use-dcae-manager", new Boolean(false)); + s1.put("use-controller", useController); + LinkedHashMap s2 = new LinkedHashMap(); + s1.put("locations", s2); + ss.put(vmDeploymentKey, s2); + } // System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmTypes.keySet()+ " " + vmDeployment.get("vm-type")); // if (vmType.containsKey("error")) continue; - if (vmType == null) { - - } - Integer numVms = (Integer) vmType.get("number-vms-per-location"); - if (numVms == null) numVms = 2; + if (vmType == null) { + continue; + } + Integer numVms = (Integer) vmType.get("number-vms-per-location"); + if (numVms == null) { + numVms = 2; + } // System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmType.keySet()+ " " + tenant.keySet()); - LinkedHashMap s1 = new LinkedHashMap(); - LinkedHashMap sss = new LinkedHashMap(); - s1.put("dcae-service", vmDeploymentKey); - s1.put("dcae-service-instance", tenantKey); - s1.put("dcae-container", "/locations/" + tenantKey); - LinkedHashMap s2 = new LinkedHashMap(); - s1.put("vms", s2); - String shortName = short_name(vmDeployment.get("vm-type")); - String indexKey = tenant.get("name-short") + ":" + shortName; - numVms = (Integer) vmType.get("number-vms-per-location"); - if (numVms == null) numVms = 2; - for (int i = 0 ; i < numVms; i++) { - String name = vmname(tenant,vmType,index.get(indexKey)); + LinkedHashMap s1 = new LinkedHashMap(); + LinkedHashMap sss = new LinkedHashMap(); + s1.put("dcae-service", vmDeploymentKey); + s1.put("dcae-service-instance", tenantKey); + s1.put("dcae-container", "/locations/" + tenantKey); + LinkedHashMap s2 = new LinkedHashMap(); + s1.put("vms", s2); + String shortName = short_name(vmDeployment.get("vm-type")); + String indexKey = tenant.get("name-short") + ":" + shortName; + numVms = (Integer) vmType.get("number-vms-per-location"); + if (numVms == null) { + numVms = 2; + } + for (int i = 0; i < numVms; i++) { + String name = vmname(tenant, vmType, index.get(indexKey)); // System.err.println ("new VM: " + name + " " + index.get(indexKey)); - index.increment(indexKey); - LinkedHashMap s3 = new LinkedHashMap(); - s3.put("dns-name", name + "." + tenant.get("dns-surfix")); - s2.put(name, s3); - } - ss.get(vmDeploymentKey).put(vmDeploymentKey+"-"+tenantKey, s1); - } - } - } - return s; - } - - private String short_name(Object object) { - String fileName = release + "/vm-templates/" + object + ".yaml"; - Map o = (Map) file2object(fileName); - return (String) o.get("short-name"); - } + index.increment(indexKey); + LinkedHashMap s3 = new LinkedHashMap(); + s3.put("dns-name", name + "." + tenant.get("dns-surfix")); + s2.put(name, s3); + } + ss.get(vmDeploymentKey).put(vmDeploymentKey + "-" + tenantKey, s1); + } + } + } + return s; + } - Map locationTypes() { - String fileName = release + "/location-types.yaml"; - @SuppressWarnings("unchecked") - Map locationTypes = (Map) file2object(fileName); - return locationTypes; - } + private String short_name(Object object) { + String fileName = release + "/vm-templates/" + object + ".yaml"; + Map o = (Map) file2object(fileName); + return (String) o.get("short-name"); + } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public Map dockers() { - LinkedHashMap s = new LinkedHashMap(); - Map locationTypes = locationTypes(); - Map dockerTypes = (Map) types("docker"); - Map tenants = tenants(); - for (String t : locationTypes.keySet()) { - for (String tenantKey : tenants.keySet()) { - Map tenant = (Map) tenants.get(tenantKey); - if (! t.equals(tenant.get("type"))) continue; - Map locationType = (Map) locationTypes.get(t); - Map dockerDeployments = (Map) locationType.get("docker-deployments"); - if (dockerDeployments == null) continue; - for (String dockerDeploymentKey : dockerDeployments.keySet()) { - Map dockerDeployment = (Map) dockerDeployments.get(dockerDeploymentKey); - Map dockerType = (Map) dockerTypes.get(dockerDeployment.get("docker-type")); + Map locationTypes() { + String fileName = release + "/location-types.yaml"; + @SuppressWarnings("unchecked") + Map locationTypes = (Map) file2object(fileName); + return locationTypes; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + public Map dockers() { + LinkedHashMap s = new LinkedHashMap(); + Map locationTypes = locationTypes(); + Map dockerTypes = (Map) types("docker"); + Map tenants = tenants(); + for (String t : locationTypes.keySet()) { + for (String tenantKey : tenants.keySet()) { + Map tenant = (Map) tenants.get(tenantKey); + if (!t.equals(tenant.get("type"))) { + continue; + } + Map locationType = (Map) locationTypes.get(t); + Map dockerDeployments = (Map) locationType.get("docker-deployments"); + if (dockerDeployments == null) { + continue; + } + for (String dockerDeploymentKey : dockerDeployments.keySet()) { + Map dockerDeployment = (Map) dockerDeployments.get(dockerDeploymentKey); + Map dockerType = (Map) dockerTypes.get(dockerDeployment.get("docker-type")); // if (dockerType.containsKey("error")) continue; // System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmType.keySet()+ " " + container.keySet()); - LinkedHashMap s1 = new LinkedHashMap(); - s1.put("docker-type", dockerDeployment.get("docker-type")); - s1.put("release", dockerDeployment.get("release")); - s1.put("milestone", dockerDeployment.get("milestone")); - s1.put("use-controller", true); - s1.put("dcae-service", dockerDeploymentKey); - s1.put("dcae-service-instance", tenantKey); - String container = (String) dockerDeployment.get("container"); + LinkedHashMap s1 = new LinkedHashMap(); + s1.put("docker-type", dockerDeployment.get("docker-type")); + s1.put("release", dockerDeployment.get("release")); + s1.put("milestone", dockerDeployment.get("milestone")); + s1.put("use-controller", true); + s1.put("dcae-service", dockerDeploymentKey); + s1.put("dcae-service-instance", tenantKey); + String container = (String) dockerDeployment.get("container"); // container = container.replace("${location-name}", tenantKey); - s1.put("dcae-container", container); - s1.put("location-type", t); - s.put(dockerDeploymentKey, s1); - } - } - } - return s; - } + s1.put("dcae-container", container); + s1.put("location-type", t); + s.put(dockerDeploymentKey, s1); + } + } + } + return s; + } + + private Map containers() { + // TODO Auto-generated method stub + return null; + } + + private Object get(Map m, String k1, String k2, Object def) { + Object o = m.get(k1); + if (o instanceof Map) { + @SuppressWarnings("unchecked") + Map m1 = (Map) o; + return get(m1, k2, def); + } + return def; + } + + private Object get(Map m, String k1, Object def) { + Object o = m.get(k1); + return o == null ? def : o; + } + + static LinkedHashMap env2char = new LinkedHashMap(); - private Map containers() { - // TODO Auto-generated method stub - return null; - } + static { + env2char.put("DEV", "zldc"); + env2char.put("FTL", "zldc"); + env2char.put("FTL2", "zldc"); + env2char.put("IST", "zltc"); + env2char.put("ISTFTL", "zltc"); + env2char.put("ETE", "zlsc"); + env2char.put("PROD", "zlpc"); + } - private Object get(Map m, String k1, String k2, Object def) { - Object o = m.get(k1); - if (o instanceof Map) { - @SuppressWarnings("unchecked") - Map m1 = (Map) o; - return get(m1,k2,def); - } - return def; - } - private Object get(Map m, String k1, Object def) { - Object o = m.get(k1); - return o == null ? def : o; - } + private String vmname(Map tenant, Map vmType, int i) { + String env1 = (String) tenant.get("environment-type"); + if (env1 == null) { + env1 = env; + } + String s = env2char.get(env1); + if (s == null) { + s = env2char.get("DEV"); + } + return s + tenant.get("name-short") + vmType.get("short-name") + String.format("%02d", i); + } - static LinkedHashMap env2char = new LinkedHashMap(); - - static { - env2char.put("DEV", "zldc"); - env2char.put("FTL", "zldc"); - env2char.put("FTL2", "zldc"); - env2char.put("IST", "zltc"); - env2char.put("ISTFTL", "zltc"); - env2char.put("ETE", "zlsc"); - env2char.put("PROD", "zlpc"); - } - - - private String vmname(Map tenant, Map vmType, int i) { - String env1 = (String) tenant.get("environment-type"); - if (env1 == null) env1 = env; - String s = env2char.get(env1); - if (s == null) s = env2char.get("DEV"); - return s + tenant.get("name-short") + vmType.get("short-name") + String.format("%02d", i); - } + @SuppressWarnings({"rawtypes", "unchecked"}) + public Map types(String dType) { + LinkedHashMap s = new LinkedHashMap(); + Map locationTypes = locationTypes(); + for (String locationType : locationTypes.keySet()) { + Map o1 = (Map) locationTypes.get(locationType); + Map o2 = (Map) o1.get(dType + "-deployments"); + if (o2 == null) { + continue; + } + for (String type : o2.keySet()) { + Map o3 = (Map) o2.get(type); + if (o3.containsKey(dType + "-type")) { + type = (String) o3.get(dType + "-type"); + } + if (s.containsKey(type)) { + continue; + } + String path = release + "/" + dType + "-templates/" + type + ".yaml"; + s.put(type, file2object(path)); + } + } + return s; + } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public Map types(String dType) { - LinkedHashMap s = new LinkedHashMap(); - Map locationTypes = locationTypes(); - for (String locationType : locationTypes.keySet()) { - Map o1 = (Map) locationTypes.get(locationType); - Map o2 = (Map) o1.get(dType + "-deployments"); - if (o2 == null) continue; - for (String type : o2.keySet()) { - Map o3 = (Map) o2.get(type); - if (o3.containsKey(dType + "-type")) - type = (String) o3.get(dType + "-type"); - if (s.containsKey(type)) continue; - String path = release + "/" + dType + "-templates/" + type + ".yaml"; - s.put(type,file2object(path)); - } - } - return s; - } + protected static Object file2object(String path) { + return file2object(path, true); + } - protected static Object file2object(String path) { - return file2object(path, true); - } - protected static Object file2object(String path, boolean required) { + protected static Object file2object(String path, boolean required) { // System.err.println (root + "/" + path); - try { - if (path.endsWith(".json")) { + try { + if (path.endsWith(".json")) { // System.err.println (root + "/" + path); - return json2object(JsonUtils.file2json(root + "/" + path)); - } - else { - Yaml yaml = new Yaml(); - String s = new String(Files.readAllBytes(Paths.get(root + "/" + path)), StandardCharsets.UTF_8); - Object o = yaml.load(s); - if (o != null) return o; - } - } catch (org.yaml.snakeyaml.parser.ParserException e) { - System.err.println("YAML exception: " + path + " " + e); - System.exit(2); - } catch (Exception e) { - System.err.println("exception: " + path + " " + e.getClass()); - if (required) - System.err.println("exception: " + path + " " + e); - else { + return json2object(JsonUtils.file2json(root + "/" + path)); + } else { + Yaml yaml = new Yaml(); + String s = new String(Files.readAllBytes(Paths.get(root + "/" + path)), StandardCharsets.UTF_8); + Object o = yaml.load(s); + if (o != null) { + return o; + } + } + } catch (org.yaml.snakeyaml.parser.ParserException e) { + System.err.println("YAML exception: " + path + " " + e); + System.exit(2); + } catch (Exception e) { + System.err.println("exception: " + path + " " + e.getClass()); + if (required) { + System.err.println("exception: " + path + " " + e); + } else { // System.err.println("exception: " + path + " " + e); - return null; - } - } - HashMap m = new HashMap(); - m.put("error", "No information about: " + path); - return m; - } + return null; + } + } + HashMap m = new HashMap(); + m.put("error", "No information about: " + path); + return m; + } - private static String object2yaml(Object object, List l) { - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setSplitLines(true); - options.setWidth(100000); - Yaml y = new Yaml(options); - Object o = object2json(object); - if (o instanceof JSONArray) { - JSONObject j = new JSONObject(); - j.put("ARRAY", object); - o = j; - } - JSONObject json = (JSONObject) o; - if (l != null) { - for (String s : l) { - if (json.has(s)) - json.remove(s); - } - } - // the replace is due to JSONObject changes " l) { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setSplitLines(true); + options.setWidth(100000); + Yaml y = new Yaml(options); + Object o = object2json(object); + if (o instanceof JSONArray) { + JSONObject j = new JSONObject(); + j.put("ARRAY", object); + o = j; + } + JSONObject json = (JSONObject) o; + if (l != null) { + for (String s : l) { + if (json.has(s)) { + json.remove(s); + } + } + } + // the replace is due to JSONObject changes "