2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
18 package org.onap.dcae.runtime.core;
20 import org.onap.dcae.runtime.core.blueprint_creator.BlueprintCreator;
22 import java.time.ZoneId;
23 import java.time.Instant;
24 import java.time.format.DateTimeFormatter;
25 import java.util.List;
26 import java.util.ArrayList;
28 public class FlowGraphParser {
30 private FlowGraph<Node,Edge> flowGraph;
31 private BlueprintCreator blueprintCreator;
33 public FlowGraphParser(BlueprintCreator blueprintCreator) {
34 this.blueprintCreator = blueprintCreator;
37 public void parse(FlowGraph<Node, Edge> flowGraph) {
38 this.flowGraph = flowGraph;
41 public static class BlueprintVessel {
42 public String blueprint;
47 public String toString() {
48 return String.format("%s:%d", this.name, this.version);
52 private static int createBlueprintVersion() {
53 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmm").withZone(ZoneId.of("UTC"));
54 Instant instant = Instant.now();
55 String timestamp = formatter.format(instant);
56 return Integer.parseInt(timestamp);
59 private static String createBlueprintName(FlowGraph flowGraph, String componentName) {
60 // NOTE: Replacing whitespaces with dash
61 // NOTE: Separator must be dash or underbar otherwise cloudify will flip out
62 return String.format("%s_%s", flowGraph.getName().replace(" ", "-"), componentName);
65 public List<BlueprintVessel> createAndProcessBlueprints() {
66 //1. generate blueprints for all the nodes
67 for(Node node : flowGraph.getNodes()){
68 if(node.getComponentId().equals("dummy_id")){
71 BlueprintData blueprintData = new BlueprintData("1", blueprintCreator.createBlueprint(node.getComponentSpec()));
72 node.setBlueprintData(blueprintData);
74 //2. replace dmaap info from the edges
75 for(Edge edge : flowGraph.getEdges()){
76 String srcNodeId = edge.getSrc().getNode();
77 Node srcNode = getNodeFromId(srcNodeId);
78 blueprintCreator.resolveDmaapConnection(srcNode ,edge.getSrc().getPort(),edge.getMetadata().getName());
80 String tgtNodeId = edge.getTgt().getNode();
81 Node tgtNode = getNodeFromId(tgtNodeId);
82 blueprintCreator.resolveDmaapConnection(tgtNode ,edge.getTgt().getPort(),edge.getMetadata().getName());
85 //3. return processed blueprints along with blueprint_name
86 List<BlueprintVessel> blueprints = new ArrayList<>();
87 for(Node node: flowGraph.getNodes()){
88 if(node.getComponentId().equals("dummy_id")) {
92 BlueprintVessel bpv = new BlueprintVessel();
93 bpv.blueprint = node.getBlueprintData().getBlueprint_content();
94 bpv.version = createBlueprintVersion();
95 bpv.name = createBlueprintName(flowGraph, node.getComponentName());
101 public Node getNodeFromId(String srcNodeId) {
102 for(Node node : flowGraph.getNodes()){
103 if (node.getComponentId().equals(srcNodeId)) return node;