From c2ac232af66853dc6bc06f6cf4b03e5319eceb85 Mon Sep 17 00:00:00 2001 From: Piotr Darosz Date: Thu, 7 Mar 2019 13:04:52 +0100 Subject: Extract modules names and relations Extract modules names and relations from DCAE_INVENTORY_BLUEPRINT Change-Id: Ia419b946e15f399d3c4e8c75f836fde6b4b3dbed Issue-ID: CLAMP-283 Signed-off-by: Piotr Darosz --- .../sdc/controller/installer/BlueprintParser.java | 138 +++++++++++++++++++++ .../sdc/controller/installer/ChainGenerator.java | 82 ++++++++++++ .../controller/installer/CsarInstallerImpl.java | 15 +++ .../sdc/controller/installer/MicroService.java | 68 ++++++++++ 4 files changed, 303 insertions(+) create mode 100644 src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java create mode 100644 src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java create mode 100644 src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java new file mode 100644 index 000000000..16aee2772 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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============================================ + * =================================================================== + * + */ +package org.onap.clamp.clds.sdc.controller.installer; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import org.json.JSONObject; +import org.springframework.stereotype.Component; +import org.yaml.snakeyaml.Yaml; + +@Component +public class BlueprintParser { + + static final String TCA = "TCA"; + static final String HOLMES = "Holmes"; + private static final String TCA_POLICY = "tca_policy"; + private static final String HOLMES_PREFIX = "holmes"; + private static final String NODE_TEMPLATES = "node_templates"; + private static final String DCAE_NODES = "dcae.nodes."; + private static final String TYPE = "type"; + private static final String PROPERTIES = "properties"; + private static final String NAME = "name"; + private static final String RELATIONSHIPS = "relationships"; + private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from"; + private static final String TARGET = "target"; + + BlueprintParser() {} + + Set getMicroServices(String blueprintString) { + Set microServices = new HashSet<>(); + JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString); + JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject(); + + for (Entry entry : results.entrySet()) { + JsonObject nodeTemplate = entry.getValue().getAsJsonObject(); + if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) { + MicroService microService = getNodeRepresentation(entry); + microServices.add(microService); + } + } + microServices.removeIf(ms -> TCA_POLICY.equals(ms.getName())); + return microServices; + } + + List fallbackToOneMicroService(String blueprintString) { + JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString); + JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject(); + String theBiggestMicroServiceContent = ""; + String theBiggestMicroServiceKey = ""; + for (Entry entry : results.entrySet()) { + String msAsString = entry.getValue().toString(); + int len =msAsString.length(); + if(len > theBiggestMicroServiceContent.length()) { + theBiggestMicroServiceContent = msAsString; + theBiggestMicroServiceKey = entry.getKey(); + } + } + String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA; + return Collections.singletonList(new MicroService(msName, "")); + } + + String getName(Entry entry) { + String microServiceYamlName = entry.getKey(); + JsonObject ob = entry.getValue().getAsJsonObject(); + if (ob.has(PROPERTIES)) { + JsonObject properties = ob.get(PROPERTIES).getAsJsonObject(); + if (properties.has(NAME)) { + return properties.get(NAME).getAsString(); + } + } + return microServiceYamlName; + } + + String getInput(Entry entry) { + JsonObject ob = entry.getValue().getAsJsonObject(); + if (ob.has(RELATIONSHIPS)) { + JsonArray relationships = ob.getAsJsonArray(RELATIONSHIPS); + for (JsonElement element : relationships) { + String target = getTarget(element.getAsJsonObject()); + if(!target.isEmpty()) { + return target; + } + } + } + return ""; + } + + MicroService getNodeRepresentation(Entry entry) { + String name = getName(entry); + String getInputFrom = getInput(entry); + return new MicroService(name, getInputFrom); + } + + private String getTarget(JsonObject elementObject) { + if (elementObject.has(TYPE) && + elementObject.has(TARGET) && + elementObject.get(TYPE).getAsString().equals(CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM)) { + return elementObject.get(TARGET).getAsString(); + } + return ""; + } + + private static JsonObject convertToJson(String yamlString) { + Yaml yaml = new Yaml(); + Map map = yaml.load(yamlString); + + JSONObject jsonObject = new JSONObject(map); + return new Gson().fromJson(jsonObject.toString(), JsonObject.class); + } +} diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java new file mode 100644 index 000000000..b05b80f00 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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============================================ + * =================================================================== + * + */ +package org.onap.clamp.clds.sdc.controller.installer; + +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.springframework.stereotype.Component; + +@Component +public class ChainGenerator { + + ChainGenerator() {} + + List getChainOfMicroServices(Set input) { + LinkedList returnList = new LinkedList<>(); + if(preValidate(input)) { + LinkedList theList = new LinkedList<>(); + for (MicroService ms : input) { + insertNodeTemplateIntoChain(ms, theList); + } + if(postValidate(theList)) { + returnList = theList; + } + } + return returnList; + } + + private boolean preValidate(Set input) { + List noInputs = + input.stream().filter(ms -> "".equals(ms.getInputFrom())).collect(Collectors.toList()); + return noInputs.size() == 1; + } + + private boolean postValidate(LinkedList microServices) { + for (int i = 1; i < microServices.size() - 1; i++) { + MicroService prev = microServices.get(i-1); + MicroService current = microServices.get(i); + if(!current.getInputFrom().equals(prev.getName())) { + return false; + } + } + return true; + } + + private void insertNodeTemplateIntoChain(MicroService microServicetoInsert, + LinkedList chainOfMicroServices) { + int insertIndex = 0; + for (int i = 0; i < chainOfMicroServices.size(); i++) { + MicroService current = chainOfMicroServices.get(i); + if (microServicetoInsert.getName().equals(current.getInputFrom())) { + insertIndex = i; + break; + } else if (current.getName().equals(microServicetoInsert.getInputFrom())) { + insertIndex = i + 1; + break; + } + } + chainOfMicroServices.add(insertIndex, microServicetoInsert); + } +} diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java index 6841b87b0..1303f2abd 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java @@ -35,6 +35,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.Set; import javax.annotation.PostConstruct; import javax.xml.transform.TransformerException; @@ -92,6 +93,12 @@ public class CsarInstallerImpl implements CsarInstaller { @Autowired private XslTransformer cldsBpmnTransformer; + @Autowired + private BlueprintParser blueprintParser; + + @Autowired + private ChainGenerator chainGenerator; + @PostConstruct public void loadConfiguration() throws IOException { BlueprintParserMappingConfiguration @@ -242,6 +249,14 @@ public class CsarInstallerImpl implements CsarInstaller { private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact, BlueprintParserFilesConfiguration configFiles) throws IOException, SdcArtifactInstallerException { + + Set microServicesFromBlueprint = blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()) ; + List microServicesChain = chainGenerator.getChainOfMicroServices(microServicesFromBlueprint); + if(microServicesChain.isEmpty()) { + microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint()); + } + //place where SVG text will be generated + CldsTemplate template = new CldsTemplate(); template.setBpmnId("Sdc-Generated"); template diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java new file mode 100644 index 000000000..287ac9a90 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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============================================ + * =================================================================== + * + */ +package org.onap.clamp.clds.sdc.controller.installer; + +import java.util.Objects; + +public class MicroService { + private final String name; + private final String inputFrom; + + public MicroService(String name, String inputFrom) { + this.name = name; + this.inputFrom = inputFrom; + } + public String getName() { + return name; + } + + public String getInputFrom() { + return inputFrom; + } + + @Override + public String toString() { + return "MicroService{" + + "name='" + name + '\'' + + ", inputFrom='" + inputFrom + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MicroService that = (MicroService) o; + return name.equals(that.name) && + inputFrom.equals(that.inputFrom); + } + + @Override + public int hashCode() { + return Objects.hash(name, inputFrom); + } +} -- cgit 1.2.3-korg