diff options
author | Adam Krysiak <adam.krysiak@nokia.com> | 2019-03-08 09:28:46 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-03-08 09:28:46 +0000 |
commit | 2d01f5210b6b5c8f0b6cdd99728d8b7b9f2fa886 (patch) | |
tree | 259fd4e7a2e515434c19261fbe8dd49a2d14d700 /src/main | |
parent | 322f2fe16e1649833ade316d367c30e203d2c26b (diff) | |
parent | c2ac232af66853dc6bc06f6cf4b03e5319eceb85 (diff) |
Merge "Extract modules names and relations"
Diffstat (limited to 'src/main')
4 files changed, 303 insertions, 0 deletions
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 00000000..16aee277 --- /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<MicroService> getMicroServices(String blueprintString) { + Set <MicroService> microServices = new HashSet<>(); + JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString); + JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject(); + + for (Entry<String, JsonElement> 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<MicroService> fallbackToOneMicroService(String blueprintString) { + JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString); + JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject(); + String theBiggestMicroServiceContent = ""; + String theBiggestMicroServiceKey = ""; + for (Entry<String, JsonElement> 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<String, JsonElement> 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<String, JsonElement> 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<String, JsonElement> 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<String, Object> 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 00000000..b05b80f0 --- /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<MicroService> getChainOfMicroServices(Set<MicroService> input) { + LinkedList<MicroService> returnList = new LinkedList<>(); + if(preValidate(input)) { + LinkedList<MicroService> theList = new LinkedList<>(); + for (MicroService ms : input) { + insertNodeTemplateIntoChain(ms, theList); + } + if(postValidate(theList)) { + returnList = theList; + } + } + return returnList; + } + + private boolean preValidate(Set<MicroService> input) { + List<MicroService> noInputs = + input.stream().filter(ms -> "".equals(ms.getInputFrom())).collect(Collectors.toList()); + return noInputs.size() == 1; + } + + private boolean postValidate(LinkedList<MicroService> 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<MicroService> 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 6841b87b..1303f2ab 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<MicroService> microServicesFromBlueprint = blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()) ; + List<MicroService> 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 00000000..287ac9a9 --- /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); + } +} |