From 80c167b8b051fc81aa726e31f62982e0fcb942fe Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Wed, 23 Sep 2020 13:57:45 +0200 Subject: Improve code quality: - Refactor Properties class - Refactor ResourceConfig class Issue-ID: DCAEGEN2-2449 Signed-off-by: Tomasz Wrobel Change-Id: I9f7debafc21d9dd2912a537499d6936e511dd824 --- .../common/blueprint/BlueprintHelper.java | 16 ++ .../models/blueprint/Properties.java | 83 ++++---- .../models/blueprint/ResourceConfig.java | 36 ++-- .../models/componentspec/ComponentSpec.java | 211 +++++++++++---------- .../models/dmaapbp/DmaapBlueprintTest.java | 1 + 5 files changed, 184 insertions(+), 163 deletions(-) diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/common/blueprint/BlueprintHelper.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/common/blueprint/BlueprintHelper.java index ef335a8..c3b1453 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/common/blueprint/BlueprintHelper.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/common/blueprint/BlueprintHelper.java @@ -29,6 +29,7 @@ public class BlueprintHelper { public static final String INTEGER_TYPE = "integer"; public static final String BOOLEAN_TYPE = "boolean"; + public static final String STRING_TYPE = "string"; public static LinkedHashMap createInputValue(String type, String description, Object defaultValue) { LinkedHashMap inputMap = new LinkedHashMap<>(); @@ -38,6 +39,13 @@ public class BlueprintHelper { return inputMap; } + public static LinkedHashMap createInputValue(String type, Object defaultValue) { + LinkedHashMap inputMap = new LinkedHashMap<>(); + inputMap.put("type", type); + inputMap.put("default", defaultValue); + return inputMap; + } + public static LinkedHashMap createIntegerInput(String description, Object defaultValue){ return createInputValue(INTEGER_TYPE, description, defaultValue); } @@ -46,6 +54,14 @@ public class BlueprintHelper { return createInputValue(BOOLEAN_TYPE, description, defaultValue); } + public static LinkedHashMap createStringInput(String description, Object defaultValue){ + return createInputValue(STRING_TYPE, description, defaultValue); + } + + public static LinkedHashMap createStringInput(Object defaultValue){ + return createInputValue(STRING_TYPE, defaultValue); + } + public static String joinUnderscore(String firstValue, String secondValue){ return firstValue + "_" + secondValue; } diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Properties.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Properties.java index 4140ea3..c847f13 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Properties.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/Properties.java @@ -22,6 +22,7 @@ package org.onap.blueprintgenerator.models.blueprint; import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createBooleanInput; import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createIntegerInput; +import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput; import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType; import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType; import static org.onap.blueprintgenerator.models.blueprint.tls.TlsConstants.USE_EXTERNAL_TLS_FIELD; @@ -51,6 +52,8 @@ import org.onap.blueprintgenerator.models.dmaapbp.DmaapStreams; @JsonInclude(value = Include.NON_NULL) public class Properties { + private static final String EMPTY_VALUE = ""; + ArrayList streams_publishes; ArrayList streams_subscribes; private Appconfig application_config; @@ -78,29 +81,23 @@ public class Properties { } public TreeMap> createOnapProperties( - TreeMap> inps, ComponentSpec cs, String override) { + TreeMap> inps, ComponentSpec componentSpec, String override) { TreeMap> retInputs = inps; //set the image GetInput image = new GetInput(); image.setBpInputName("image"); this.setImage(image); - LinkedHashMap img = new LinkedHashMap(); - img.put("type", "string"); - img.put("default", cs.getArtifacts()[0].getUri()); - retInputs.put("image", img); + retInputs.put("image", createStringInput(componentSpec.getImageUri())); //set the location id GetInput location = new GetInput(); location.setBpInputName("location_id"); this.setLocation_id(location); - LinkedHashMap locMap = new LinkedHashMap(); - locMap.put("type", "string"); - locMap.put("default", ""); - retInputs.put("location_id", locMap); + retInputs.put("location_id", createStringInput(EMPTY_VALUE)); //set the log info - this.setLog_info(cs.getAuxilary().getLog_info()); + this.setLog_info(componentSpec.getAuxilary().getLog_info()); //set the replicas GetInput replica = new GetInput(); @@ -116,7 +113,7 @@ public class Properties { //this.setName(cs.getSelf().getName()); //set the docker config - Auxilary aux = cs.getAuxilary(); + Auxilary aux = componentSpec.getAuxilary(); // if(aux.getPorts() != null) { // retInputs = aux.createPorts(retInputs); // } @@ -124,7 +121,7 @@ public class Properties { //set the app config Appconfig app = new Appconfig(); - retInputs = app.createAppconfig(retInputs, cs, override, false); + retInputs = app.createAppconfig(retInputs, componentSpec, override, false); this.setApplication_config(app); // set always_pull_image @@ -136,63 +133,55 @@ public class Properties { retInputs.put("always_pull_image", inputAlwaysPullImage); //set service component type - String sType = cs.getSelf().getName(); - sType = sType.replace('.', '-'); - this.setService_component_type(sType); + String serviceComponentType = componentSpec.getSelfName().replace('.', '-'); + this.setService_component_type(serviceComponentType); //set the tls info for internal and external communication - TreeMap tls_info = cs.getAuxilary().getTls_info(); + TreeMap tls_info = componentSpec.getAuxilary().getTls_info(); if (tls_info != null) { - addTlsInfo(cs, retInputs); + addTlsInfo(componentSpec, retInputs); if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) { - retInputs.putAll(addExternalTlsInfo(cs)); + retInputs.putAll(addExternalTlsInfo(componentSpec)); } } //set the reource config ResourceConfig resource = new ResourceConfig(); - retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName()); + retInputs = resource.createResourceConfig(retInputs, componentSpec.getSelf().getName()); this.setResource_config(resource); return retInputs; } public TreeMap> createDmaapProperties( - TreeMap> inps, ComponentSpec cs, String override) { + TreeMap> inps, ComponentSpec componentSpec, String override) { TreeMap> retInputs = inps; //set the image GetInput image = new GetInput(); image.setBpInputName("tag_version"); this.setImage(image); - LinkedHashMap img = new LinkedHashMap(); - img.put("type", "string"); - img.put("default", cs.getArtifacts()[0].getUri()); - retInputs.put("tag_version", img); + retInputs.put("tag_version", createStringInput(componentSpec.getImageUri())); //set the location id GetInput location = new GetInput(); location.setBpInputName("location_id"); this.setLocation_id(location); - LinkedHashMap locMap = new LinkedHashMap<>(); - locMap.put("type", "string"); - locMap.put("default", ""); - retInputs.put("location_id", locMap); + retInputs.put("location_id", createStringInput(EMPTY_VALUE)); //set the log info - this.setLog_info(cs.getAuxilary().getLog_info()); + this.setLog_info(componentSpec.getAuxilary().getLog_info()); //set service component type - String sType = cs.getSelf().getName(); - sType = sType.replace('.', '-'); - this.setService_component_type(sType); + String serviceComponentType = componentSpec.getSelfName().replace('.', '-'); + this.setService_component_type(serviceComponentType); //set the tls info for internal and external communication - TreeMap tls_info = cs.getAuxilary().getTls_info(); + TreeMap tls_info = componentSpec.getAuxilary().getTls_info(); if (tls_info != null) { - addTlsInfo(cs, retInputs); + addTlsInfo(componentSpec, retInputs); if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) { - retInputs.putAll(addExternalTlsInfo(cs)); + retInputs.putAll(addExternalTlsInfo(componentSpec)); } } @@ -200,7 +189,7 @@ public class Properties { GetInput replica = new GetInput(); replica.setBpInputName("replicas"); this.setReplicas(replica); - LinkedHashMap rep = createIntegerInput( "number of instances", 1); + LinkedHashMap rep = createIntegerInput("number of instances", 1); retInputs.put("replicas", rep); // //set the dns name @@ -210,7 +199,7 @@ public class Properties { // this.setName(cs.getSelf().getName()); //set the docker config - Auxilary aux = cs.getAuxilary(); + Auxilary aux = componentSpec.getAuxilary(); // if(aux.getPorts() != null) { // retInputs = aux.createPorts(retInputs); // } @@ -218,25 +207,25 @@ public class Properties { //set the appconfig Appconfig app = new Appconfig(); - retInputs = app.createAppconfig(retInputs, cs, override, true); + retInputs = app.createAppconfig(retInputs, componentSpec, override, true); this.setApplication_config(app); //set the stream publishes ArrayList pubStreams = new ArrayList<>(); - if (cs.getStreams().getPublishes() != null) { - for (Publishes publishes : cs.getStreams().getPublishes()) { + if (componentSpec.getStreams().getPublishes() != null) { + for (Publishes publishes : componentSpec.getStreams().getPublishes()) { if (isMessageRouterType(publishes.getType())) { String topic = publishes.getConfig_key() + "_topic"; DmaapStreams mrStreams = new DmaapStreams(); retInputs = mrStreams - .createStreams(inps, cs, topic, publishes.getType(), publishes.getConfig_key(), + .createStreams(inps, componentSpec, topic, publishes.getType(), publishes.getConfig_key(), publishes.getRoute(), 'p'); pubStreams.add(mrStreams); } else if (isDataRouterType(publishes.getType())) { String feed = publishes.getConfig_key() + "_feed"; DmaapStreams drStreams = new DmaapStreams(); retInputs = drStreams - .createStreams(inps, cs, feed, publishes.getType(), publishes.getConfig_key(), + .createStreams(inps, componentSpec, feed, publishes.getType(), publishes.getConfig_key(), publishes.getRoute(), 'p'); pubStreams.add(drStreams); } @@ -245,20 +234,20 @@ public class Properties { //set the stream subscribes ArrayList subStreams = new ArrayList<>(); - if (cs.getStreams().getSubscribes() != null) { - for (Subscribes subscribes : cs.getStreams().getSubscribes()) { + if (componentSpec.getStreams().getSubscribes() != null) { + for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) { if (isMessageRouterType(subscribes.getType())) { String topic = subscribes.getConfig_key() + "_topic"; DmaapStreams mrStreams = new DmaapStreams(); retInputs = mrStreams - .createStreams(inps, cs, topic, subscribes.getType(), subscribes.getConfig_key(), + .createStreams(inps, componentSpec, topic, subscribes.getType(), subscribes.getConfig_key(), subscribes.getRoute(), 's'); subStreams.add(mrStreams); } else if (isDataRouterType(subscribes.getType())) { String feed = subscribes.getConfig_key() + "_feed"; DmaapStreams drStreams = new DmaapStreams(); retInputs = drStreams - .createStreams(inps, cs, feed, subscribes.getType(), subscribes.getConfig_key(), + .createStreams(inps, componentSpec, feed, subscribes.getType(), subscribes.getConfig_key(), subscribes.getRoute(), 's'); subStreams.add(drStreams); } @@ -274,7 +263,7 @@ public class Properties { //set the reource config ResourceConfig resource = new ResourceConfig(); - retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName()); + retInputs = resource.createResourceConfig(retInputs, componentSpec.getSelf().getName()); this.setResource_config(resource); return retInputs; diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java index 6595a67..f750178 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ResourceConfig.java @@ -21,6 +21,8 @@ package org.onap.blueprintgenerator.models.blueprint; +import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createInputValue; + import java.util.LinkedHashMap; import java.util.TreeMap; @@ -72,50 +74,44 @@ public class ResourceConfig { */ public TreeMap> createResourceConfig(TreeMap> inps, String name){ - LinkedHashMap mi = new LinkedHashMap<>(); - mi.put("type", "string"); - mi.put("default", "128Mi"); - - LinkedHashMap m = new LinkedHashMap<>(); - m.put("type", "string"); - m.put("default", "250m"); - + LinkedHashMap memoryLimit = createInputValue("string", "", "128Mi"); + LinkedHashMap cpuLimit = createInputValue("string", "", "250m"); if(!name.equals("")) { name = name + "_"; } //set the limits - TreeMap lim = new TreeMap<>(); + TreeMap limits = new TreeMap<>(); GetInput cpu = new GetInput(); cpu.setBpInputName(name + "cpu_limit"); - lim.put("cpu", cpu); + limits.put("cpu", cpu); GetInput memL = new GetInput(); memL.setBpInputName(name + "memory_limit"); - lim.put("memory", memL); + limits.put("memory", memL); - inps.put(name + "cpu_limit", m); - inps.put(name + "memory_limit", mi); + inps.put(name + "cpu_limit", cpuLimit); + inps.put(name + "memory_limit", memoryLimit); - this.setLimits(lim); + this.setLimits(limits); //set the requests - TreeMap req = new TreeMap<>(); + TreeMap requests = new TreeMap<>(); GetInput cpuR = new GetInput(); cpuR.setBpInputName(name + "cpu_request"); - req.put("cpu", cpuR); + requests.put("cpu", cpuR); GetInput memR = new GetInput(); memR.setBpInputName(name + "memory_request"); - req.put("memory", memR); + requests.put("memory", memR); - inps.put(name + "cpu_request", m); - inps.put(name + "memory_request", mi); + inps.put(name + "cpu_request", cpuLimit); + inps.put(name + "memory_request", memoryLimit); - this.setRequests(req); + this.setRequests(requests); return inps; } diff --git a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/componentspec/ComponentSpec.java b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/componentspec/ComponentSpec.java index 3876b53..0cd2b72 100644 --- a/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/componentspec/ComponentSpec.java +++ b/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/componentspec/ComponentSpec.java @@ -1,21 +1,22 @@ -/**============LICENSE_START======================================================= - org.onap.dcae - ================================================================================ - Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. - ================================================================================ - 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. - See the License for the specific language governing permissions and - limitations under the License. - ============LICENSE_END========================================================= - +/*============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + Copyright (c) 2020 Nokia. All rights reserved. + ================================================================================ + 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. + See the License for the specific language governing permissions and + limitations under the License. + ============LICENSE_END========================================================= + */ package org.onap.blueprintgenerator.models.componentspec; @@ -30,11 +31,13 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.Getter; import lombok.Setter; +import lombok.Getter; +import lombok.Setter; import lombok.NoArgsConstructor; import org.onap.blueprintgenerator.models.componentspec.policy_info.PolicyInfo; // TODO: Auto-generated Javadoc + /** * The Class ComponentSpec. */ @@ -43,13 +46,13 @@ import org.onap.blueprintgenerator.models.componentspec.policy_info.PolicyInfo; /* (non-Javadoc) * @see java.lang.Object#toString() */ -@Getter @Setter +@Getter +@Setter /* (non-Javadoc) * @see java.lang.Object#toString() */ - /** * Instantiates a new component spec. */ @@ -66,82 +69,98 @@ import org.onap.blueprintgenerator.models.componentspec.policy_info.PolicyInfo; * @param artifacts the artifacts */ -@JsonInclude(value=Include.NON_NULL) +@JsonInclude(value = Include.NON_NULL) //main object that the component spec file is written in public class ComponentSpec { - - /** The self. */ - private Self self; - - /** The services. */ - private Services services; - - /** The streams. */ - private Streams streams; - - /** The parameters. */ - private Parameters[] parameters; - - /** The auxilary. */ - private Auxilary auxilary; - - @JsonProperty("policy_info") - private PolicyInfo policyInfo; - - /** The artifacts. */ - private Artifacts[] artifacts; - - /** - * Creates the component spec from file. - * - * @param path the path - */ - public void createComponentSpecFromFile(String path) { - ObjectMapper componentMapper = new ObjectMapper(); - File specPathFile = new File(path); - ComponentSpec cs = new ComponentSpec(); - - try { - cs = componentMapper.readValue(specPathFile, ComponentSpec.class); - } catch (IOException e) { - throw new RuntimeException(e); - } - - - - //set all the pieces of the component spec - this.setSelf(cs.getSelf()); - this.setArtifacts(cs.getArtifacts()); - this.setAuxilary(cs.getAuxilary()); - this.setParameters(cs.getParameters()); - this.setServices(cs.getServices()); - this.setStreams(cs.getStreams()); - this.setPolicyInfo(cs.getPolicyInfo()); - } - - - /** - * Creates the component spec from string. - * - * @param specString the spec string - */ - public void createComponentSpecFromString(String specString) { - ObjectMapper componentMapper = new ObjectMapper(); - ComponentSpec cs = new ComponentSpec(); - try { - cs = componentMapper.readValue(specString, ComponentSpec.class); - } catch (IOException e) { - throw new RuntimeException(e); - } - - //set all the pieces of the component spec - this.setSelf(cs.getSelf()); - this.setArtifacts(cs.getArtifacts()); - this.setAuxilary(cs.getAuxilary()); - this.setParameters(cs.getParameters()); - this.setServices(cs.getServices()); - this.setStreams(cs.getStreams()); - } - + /** + * The self. + */ + private Self self; + + /** + * The services. + */ + private Services services; + + /** + * The streams. + */ + private Streams streams; + + /** + * The parameters. + */ + private Parameters[] parameters; + + /** + * The auxilary. + */ + private Auxilary auxilary; + + @JsonProperty("policy_info") + private PolicyInfo policyInfo; + + /** + * The artifacts. + */ + private Artifacts[] artifacts; + + /** + * Creates the component spec from file. + * + * @param path the path + */ + public void createComponentSpecFromFile(String path) { + ObjectMapper componentMapper = new ObjectMapper(); + File specPathFile = new File(path); + ComponentSpec cs = new ComponentSpec(); + + try { + cs = componentMapper.readValue(specPathFile, ComponentSpec.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + + //set all the pieces of the component spec + this.setSelf(cs.getSelf()); + this.setArtifacts(cs.getArtifacts()); + this.setAuxilary(cs.getAuxilary()); + this.setParameters(cs.getParameters()); + this.setServices(cs.getServices()); + this.setStreams(cs.getStreams()); + this.setPolicyInfo(cs.getPolicyInfo()); + } + + + /** + * Creates the component spec from string. + * + * @param specString the spec string + */ + public void createComponentSpecFromString(String specString) { + ObjectMapper componentMapper = new ObjectMapper(); + ComponentSpec cs = new ComponentSpec(); + try { + cs = componentMapper.readValue(specString, ComponentSpec.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + + //set all the pieces of the component spec + this.setSelf(cs.getSelf()); + this.setArtifacts(cs.getArtifacts()); + this.setAuxilary(cs.getAuxilary()); + this.setParameters(cs.getParameters()); + this.setServices(cs.getServices()); + this.setStreams(cs.getStreams()); + } + + public String getImageUri() { + return artifacts[0].getUri(); + } + + public String getSelfName() { + return this.self.getName(); + } } diff --git a/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/dmaapbp/DmaapBlueprintTest.java b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/dmaapbp/DmaapBlueprintTest.java index 820082e..9e31002 100644 --- a/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/dmaapbp/DmaapBlueprintTest.java +++ b/mod/bpgenerator/src/test/java/org/onap/blueprintgenerator/models/dmaapbp/DmaapBlueprintTest.java @@ -239,6 +239,7 @@ public class DmaapBlueprintTest { when(componentSpec.getStreams()).thenReturn(streams); when(componentSpec.getArtifacts()).thenReturn(arrayArtifacts); when(componentSpec.getParameters()).thenReturn(new Parameters[0]); + when(componentSpec.getSelfName()).thenReturn(MOCKED_NAME); return componentSpec; } } -- cgit 1.2.3-korg