aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap
diff options
context:
space:
mode:
authorsebdet <sebastien.determe@intl.att.com>2019-03-12 15:08:11 +0100
committersebdet <sebastien.determe@intl.att.com>2019-03-12 15:25:11 +0100
commit92cc4185fca63ddd882be5bcd9d4a626b627164f (patch)
tree802a20de3520e90c1ad737ede5a057fa99ed115d /src/main/java/org/onap
parente3d4f773fd3c3078384aef77ff1f45612ff53356 (diff)
Add unit tests
Add unit tests and fix code to support them, columns modified and csar installer fixed as well Issue-ID: CLAMP-306 Change-Id: I946ef1aa957ca36bbb00357308ac67a3f07dcdce Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r--src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java10
-rw-r--r--src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java81
-rw-r--r--src/main/java/org/onap/clamp/loop/Loop.java2
-rw-r--r--src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java15
4 files changed, 50 insertions, 58 deletions
diff --git a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java
index 784d95e94..8a172abbc 100644
--- a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java
+++ b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java
@@ -82,13 +82,15 @@ public class ToscaYamlToJsonConvertor {
this.cldsDao = cldsDao;
}
- @SuppressWarnings("unchecked")
public String parseToscaYaml(String yamlString) {
Yaml yaml = new Yaml();
- LinkedHashMap<String, Object> loadedYaml = (LinkedHashMap<String, Object>) yaml.load(yamlString);
- LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<String, Object>();
- LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<String, Object>();
+ LinkedHashMap<String, Object> loadedYaml = yaml.load(yamlString);
+ if (loadedYaml == null) {
+ return "";
+ }
+ LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<>();
+ LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<>();
JSONObject jsonEditorObject = new JSONObject();
JSONObject jsonParentObject = new JSONObject();
JSONObject jsonTempObject = new JSONObject();
diff --git a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
index 9627445d6..6e12f2940 100644
--- a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
+++ b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
@@ -33,7 +33,6 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
import org.json.simple.parser.ParseException;
import org.onap.clamp.clds.client.DcaeInventoryServices;
@@ -53,6 +52,7 @@ import org.onap.clamp.policy.operational.OperationalPolicy;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.Yaml;
@@ -71,63 +71,40 @@ public class CsarInstallerImpl implements CsarInstaller {
public static final String MODEL_NAME_PREFIX = "Loop_";
@Autowired
- protected LoopsRepository loopRepository;
+ LoopsRepository loopRepository;
@Autowired
- private BlueprintParser blueprintParser;
+ BlueprintParser blueprintParser;
@Autowired
- private ChainGenerator chainGenerator;
+ ChainGenerator chainGenerator;
@Autowired
DcaeInventoryServices dcaeInventoryService;
- @Autowired
- public void CsarInstallerImpl(LoopsRepository loopRepository, BlueprintParser blueprintParser,
- ChainGenerator chainGenerator, DcaeInventoryServices dcaeInventoryService) {
- this.loopRepository = loopRepository;
- this.blueprintParser = blueprintParser;
- this.chainGenerator = chainGenerator;
- this.dcaeInventoryService = dcaeInventoryService;
- }
-
@Override
public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
boolean alreadyInstalled = true;
for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
alreadyInstalled = alreadyInstalled
- && loopRepository.existsById(buildModelName(csar, blueprint.getValue()));
+ && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
+ csar.getSdcNotification().getServiceVersion(),
+ blueprint.getValue().getResourceAttached().getResourceInstanceName(),
+ blueprint.getValue().getBlueprintArtifactName()));
}
return alreadyInstalled;
}
- public static String buildModelName(CsarHandler csar, BlueprintArtifact artifact) {
-
- return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
- + csar.getSdcNotification().getServiceVersion() + "_"
- + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
- + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
- }
-
- public static String buildOperationalPolicyName(CsarHandler csar, BlueprintArtifact artifact) {
-
- return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
- + csar.getSdcNotification().getServiceVersion() + "_"
- + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
- + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
- }
-
@Override
- @Transactional
+ @Transactional(propagation = Propagation.REQUIRED)
public void installTheCsar(CsarHandler csar)
throws SdcArtifactInstallerException, InterruptedException, PolicyModelException {
try {
logger.info("Installing the CSAR " + csar.getFilePath());
for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
- createLoopFromBlueprint(csar, blueprint.getValue());
+ loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue()));
}
- createPolicyModel(csar);
logger.info("Successfully installed the CSAR " + csar.getFilePath());
} catch (IOException e) {
throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
@@ -136,15 +113,6 @@ public class CsarInstallerImpl implements CsarInstaller {
}
}
- private void createPolicyModel(CsarHandler csar) throws PolicyModelException {
- try {
- Optional<String> policyModelYaml = csar.getPolicyModelYaml();
- // save policy model into the database
- } catch (IOException e) {
- throw new PolicyModelException("TransformerException when decoding the YamlText", e);
- }
- }
-
private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact)
throws IOException, ParseException, InterruptedException {
Loop newLoop = new Loop();
@@ -154,15 +122,8 @@ public class CsarInstallerImpl implements CsarInstaller {
blueprintArtifact.getResourceAttached().getResourceInstanceName(),
blueprintArtifact.getBlueprintArtifactName()));
newLoop.setLastComputedState(LoopState.DESIGN);
- for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
- newLoop.getMicroServicePolicies().add(new MicroServicePolicy(microService.getName(),
- csar.getPolicyModelYaml().orElse(""), false, new JsonObject(), new HashSet<>(Arrays.asList(newLoop))));
- }
- newLoop.setOperationalPolicies(
- new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
- csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
- blueprintArtifact.getResourceAttached().getResourceInstanceName(),
- blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject()))));
+ newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
+ newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
// Set SVG XML computed
// newLoop.setSvgRepresentation(svgRepresentation);
newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(csar, blueprintArtifact));
@@ -172,6 +133,24 @@ public class CsarInstallerImpl implements CsarInstaller {
return newLoop;
}
+ private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
+ Loop newLoop) {
+ return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
+ csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
+ blueprintArtifact.getResourceAttached().getResourceInstanceName(),
+ blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
+ }
+
+ private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
+ BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
+ HashSet<MicroServicePolicy> newSet = new HashSet<>();
+ for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
+ newSet.add(new MicroServicePolicy(microService.getName(), csar.getPolicyModelYaml().orElse(""), false,
+ new HashSet<>(Arrays.asList(newLoop))));
+ }
+ return newSet;
+ }
+
private JsonObject createGlobalPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
JsonObject globalProperties = new JsonObject();
globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact));
diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java
index cc7f1803c..a4cd86d07 100644
--- a/src/main/java/org/onap/clamp/loop/Loop.java
+++ b/src/main/java/org/onap/clamp/loop/Loop.java
@@ -91,7 +91,7 @@ public class Loop implements Serializable {
@Column(columnDefinition = "json", name = "model_properties_json")
private JsonObject modelPropertiesJson;
- @Column(nullable = false, name = "blueprint_yaml")
+ @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
private String blueprint;
@Expose
diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java
index 7ebe0edb2..857a3d747 100644
--- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java
+++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java
@@ -39,6 +39,8 @@ import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
+import org.onap.clamp.clds.util.JsonUtils;
import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
import org.onap.clamp.loop.Loop;
import org.onap.clamp.policy.Policy;
@@ -66,7 +68,7 @@ public class MicroServicePolicy implements Serializable, Policy {
@Column(name = "shared", nullable = false)
private Boolean shared;
- @Column(name = "policy_tosca", nullable = false)
+ @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
private String policyTosca;
@Expose
@@ -81,13 +83,22 @@ public class MicroServicePolicy implements Serializable, Policy {
// serialization
}
+ public MicroServicePolicy(String name, String policyTosca, Boolean shared, Set<Loop> usedByLoops) {
+ this.name = name;
+ this.policyTosca = policyTosca;
+ this.shared = shared;
+ this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
+ .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
+ this.usedByLoops = usedByLoops;
+ }
+
public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation,
Set<Loop> usedByLoops) {
this.name = name;
this.policyTosca = policyTosca;
this.shared = shared;
- this.jsonRepresentation = jsonRepresentation;
this.usedByLoops = usedByLoops;
+ this.jsonRepresentation = jsonRepresentation;
}
@Override