From cc9d389bf197c1a365e669b407ea781f2bc87fd4 Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Wed, 20 Nov 2019 12:32:48 -0500 Subject: Add mod/runtimeapi Change-Id: I6c0a45ddf460a63a1e4b9284e19bf4ab111bd463 Issue-ID: DCAEGEN2-1860 Signed-off-by: Michael Hwang --- .../org/onap/dcae/runtime/core/BlueprintData.java | 45 +++++++++ .../main/java/org/onap/dcae/runtime/core/Edge.java | 56 +++++++++++ .../org/onap/dcae/runtime/core/EdgeLocation.java | 47 +++++++++ .../org/onap/dcae/runtime/core/EdgeMetadata.java | 58 +++++++++++ .../java/org/onap/dcae/runtime/core/FlowGraph.java | 108 +++++++++++++++++++++ .../onap/dcae/runtime/core/FlowGraphParser.java | 107 ++++++++++++++++++++ .../main/java/org/onap/dcae/runtime/core/Node.java | 75 ++++++++++++++ .../core/blueprint_creator/BlueprintCreator.java | 26 +++++ .../BlueprintCreatorOnapDublin.java | 78 +++++++++++++++ 9 files changed, 600 insertions(+) create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/BlueprintData.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Edge.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeLocation.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeMetadata.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraph.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraphParser.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Node.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreator.java create mode 100644 mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreatorOnapDublin.java (limited to 'mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core') diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/BlueprintData.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/BlueprintData.java new file mode 100644 index 0000000..a32646b --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/BlueprintData.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +public class BlueprintData { + + public String version; + public String blueprint_content; + + public BlueprintData(String version, String blueprint_content) { + this.version = version; + this.blueprint_content = blueprint_content; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getBlueprint_content() { + return blueprint_content; + } + + public void setBlueprint_content(String blueprint_content) { + this.blueprint_content = blueprint_content; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Edge.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Edge.java new file mode 100644 index 0000000..c488eee --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Edge.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Edge { + private EdgeLocation src; + private EdgeLocation tgt; + private EdgeMetadata metadata; + + public Edge(@JsonProperty("src") EdgeLocation src,@JsonProperty("tgt") EdgeLocation tgt,@JsonProperty("metadata") EdgeMetadata metadata) { + this.src = src; + this.tgt = tgt; + this.metadata = metadata; + } + + public EdgeLocation getSrc() { + return src; + } + + public void setSrc(EdgeLocation src) { + this.src = src; + } + + public EdgeLocation getTgt() { + return tgt; + } + + public void setTgt(EdgeLocation tgt) { + this.tgt = tgt; + } + + public EdgeMetadata getMetadata() { + return metadata; + } + + public void setMetadata(EdgeMetadata metadata) { + this.metadata = metadata; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeLocation.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeLocation.java new file mode 100644 index 0000000..ad19373 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeLocation.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class EdgeLocation { + + private String node; + private String port; + + public EdgeLocation(@JsonProperty("node") String node,@JsonProperty("port") String port) { + this.node = node; + this.port = port; + } + + public String getNode() { + return node; + } + + public void setNode(String node) { + this.node = node; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeMetadata.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeMetadata.java new file mode 100644 index 0000000..4898349 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/EdgeMetadata.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class EdgeMetadata { + + private String name; + private String dataType; + private String dmaapType; + + public EdgeMetadata(@JsonProperty("name") String name,@JsonProperty("data_type") String dataType, + @JsonProperty("dmaap_type") String dmaapType) { + this.name = name; + this.dataType = dataType; + this.dmaapType = dmaapType; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDataType() { + return dataType; + } + + public void setDataType(String dataType) { + this.dataType = dataType; + } + + public String getDmaapType() { + return dmaapType; + } + + public void setDmaapType(String dmaapType) { + this.dmaapType = dmaapType; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraph.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraph.java new file mode 100644 index 0000000..4413d72 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraph.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +import com.google.common.graph.MutableNetwork; +import com.google.common.graph.NetworkBuilder; + +import java.util.Set; + +public class FlowGraph { + private String id; + private String name; + private boolean isMain; + private String description; + private MutableNetwork mutableNetwork; + + public FlowGraph(String id, String name, boolean isMain, String description) { + this.id = id; + this.name = name; + this.isMain = isMain; + this.description = description; + this.mutableNetwork = NetworkBuilder.directed().build(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isMain() { + return isMain; + } + + public void setMain(boolean main) { + isMain = main; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void addNode(Node node) { + mutableNetwork.addNode(node); + } + + public int getNodeSize() { + return mutableNetwork.nodes().size(); + } + + public Set getNodes() { + return mutableNetwork.nodes(); + } + + public Set getEdges() { + return mutableNetwork.edges(); + } + + public void addEdge(Node node_1, Node node_2, Edge edge) { + mutableNetwork.addEdge(node_1,node_2,edge); + } + + public int getEdgeSize() { + return mutableNetwork.edges().size(); + } + + public Edge getEdge(Node node_1, Node node_2) { + return mutableNetwork.edgeConnecting(node_1,node_2).get(); + } + + public void removeNode(Node node_1) { + mutableNetwork.removeNode(node_1); + } + + public void removeEdge(Edge edge_1) { + mutableNetwork.removeEdge(edge_1); + } + +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraphParser.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraphParser.java new file mode 100644 index 0000000..7154cae --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraphParser.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +import org.onap.dcae.runtime.core.blueprint_creator.BlueprintCreator; + +import java.time.ZoneId; +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.ArrayList; + +public class FlowGraphParser { + + private FlowGraph flowGraph; + private BlueprintCreator blueprintCreator; + + public FlowGraphParser(BlueprintCreator blueprintCreator) { + this.blueprintCreator = blueprintCreator; + } + + public void parse(FlowGraph flowGraph) { + this.flowGraph = flowGraph; + } + + public static class BlueprintVessel { + public String blueprint; + public String name; + public int version; + + @Override + public String toString() { + return String.format("%s:%d", this.name, this.version); + } + } + + private static int createBlueprintVersion() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmm").withZone(ZoneId.of("UTC")); + Instant instant = Instant.now(); + String timestamp = formatter.format(instant); + return Integer.parseInt(timestamp); + } + + private static String createBlueprintName(FlowGraph flowGraph, String componentName) { + // NOTE: Replacing whitespaces with dash + // NOTE: Separator must be dash or underbar otherwise cloudify will flip out + return String.format("%s_%s", flowGraph.getName().replace(" ", "-"), componentName); + } + + public List createAndProcessBlueprints() { + //1. generate blueprints for all the nodes + for(Node node : flowGraph.getNodes()){ + if(node.getComponentId().equals("dummy_id")){ + continue; + } + BlueprintData blueprintData = new BlueprintData("1", blueprintCreator.createBlueprint(node.getComponentSpec())); + node.setBlueprintData(blueprintData); + } + //2. replace dmaap info from the edges + for(Edge edge : flowGraph.getEdges()){ + String srcNodeId = edge.getSrc().getNode(); + Node srcNode = getNodeFromId(srcNodeId); + blueprintCreator.resolveDmaapConnection(srcNode ,edge.getSrc().getPort(),edge.getMetadata().getName()); + + String tgtNodeId = edge.getTgt().getNode(); + Node tgtNode = getNodeFromId(tgtNodeId); + blueprintCreator.resolveDmaapConnection(tgtNode ,edge.getTgt().getPort(),edge.getMetadata().getName()); + } + + //3. return processed blueprints along with blueprint_name + List blueprints = new ArrayList<>(); + for(Node node: flowGraph.getNodes()){ + if(node.getComponentId().equals("dummy_id")) { + continue; + } + + BlueprintVessel bpv = new BlueprintVessel(); + bpv.blueprint = node.getBlueprintData().getBlueprint_content(); + bpv.version = createBlueprintVersion(); + bpv.name = createBlueprintName(flowGraph, node.getComponentName()); + blueprints.add(bpv); + } + return blueprints; + } + + public Node getNodeFromId(String srcNodeId) { + for(Node node : flowGraph.getNodes()){ + if (node.getComponentId().equals(srcNodeId)) return node; + } + return null; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Node.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Node.java new file mode 100644 index 0000000..d240520 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/Node.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core; + +public class Node { + private String componentId; + private String componentName; + private String componentSpec; + private BlueprintData blueprintData; + + public String getComponentId() { + return componentId; + } + + public void setComponentId(String componentId) { + this.componentId = componentId; + } + + public String getComponentName() { + return componentName; + } + + public void setComponentName(String componentName) { + this.componentName = componentName; + } + + public String getComponentSpec() { + return componentSpec; + } + + public void setComponentSpec(String componentSpec) { + this.componentSpec = componentSpec; + } + + public BlueprintData getBlueprintData() { + return blueprintData; + } + + public void setBlueprintData(BlueprintData blueprintData) { + this.blueprintData = blueprintData; + } + + public Node(String componentId, String componentName, String componentSpec) { + this.componentId = componentId; + this.componentName = componentName; + this.componentSpec = componentSpec; + } + + + + @Override + public boolean equals(Object obj) { + return this.componentId.equals(((Node)obj).componentId); + } + + @Override + public String toString() { + return componentId; + } +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreator.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreator.java new file mode 100644 index 0000000..b633472 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreator.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core.blueprint_creator; + +import org.onap.dcae.runtime.core.Node; + +public interface BlueprintCreator { + + String createBlueprint(String componentSpecString); + void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName); +} diff --git a/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreatorOnapDublin.java b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreatorOnapDublin.java new file mode 100644 index 0000000..2225971 --- /dev/null +++ b/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/blueprint_creator/BlueprintCreatorOnapDublin.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * 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========================================================= + */ +package org.onap.dcae.runtime.core.blueprint_creator; + +import org.onap.dcae.runtime.core.Node; +import org.onap.blueprintgenerator.models.blueprint.Blueprint; +import org.onap.blueprintgenerator.models.componentspec.ComponentSpec; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import java.util.Map; + +public class BlueprintCreatorOnapDublin implements BlueprintCreator{ + + private String topicUrl; + private String importFilePath; + + public void setTopicUrl(String topicUrl) { + this.topicUrl = topicUrl; + } + + public void setImportFilePath(String importFilePath) { + this.importFilePath = importFilePath; + } + + @Override + public String createBlueprint(String componentSpecString) { + ComponentSpec componentSpec = new ComponentSpec(); + componentSpec.createComponentSpecFromString(componentSpecString); + Blueprint blueprint = new Blueprint().createBlueprint(componentSpec,"",'o',importFilePath); + return blueprint.blueprintToString(); + } + + @Override + public void resolveDmaapConnection(Node node, String locationPort, String dmaapEntityName) { + if(node == null || locationPort == null){ + return; + } + String blueprintContent = node.getBlueprintData().getBlueprint_content(); + locationPort = locationPort.replaceAll("-","_"); + Yaml yaml = getYamlInstance(); + Map obj = yaml.load(blueprintContent); + Map inputsObj = (Map) obj.get("inputs"); + for(Map.Entry entry: inputsObj.entrySet()){ + if(entry.getKey().matches(locationPort+".*url")) { + Map inputValue = (Map) entry.getValue(); + inputValue.put("default",topicUrl + "/" + dmaapEntityName); + } + } + node.getBlueprintData().setBlueprint_content(yaml.dump(obj)); + } + + private Yaml getYamlInstance() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setPrettyFlow(true); + return new Yaml(options); + } + +// private String attachSingleQoutes(String str) { +// return "'" + str + "'"; +// } +} -- cgit 1.2.3-korg