7154caee28a8457e063e074c6ae37eefb90240d1
[dcaegen2/platform.git] / mod / runtimeapi / runtime-core / src / main / java / org / onap / dcae / runtime / core / FlowGraphParser.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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=========================================================
17  */
18 package org.onap.dcae.runtime.core;
19
20 import org.onap.dcae.runtime.core.blueprint_creator.BlueprintCreator;
21
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;
27
28 public class FlowGraphParser {
29
30     private FlowGraph<Node,Edge> flowGraph;
31     private BlueprintCreator blueprintCreator;
32
33     public FlowGraphParser(BlueprintCreator blueprintCreator) {
34         this.blueprintCreator = blueprintCreator;
35     }
36
37     public void parse(FlowGraph<Node, Edge> flowGraph) {
38         this.flowGraph = flowGraph;
39     }
40
41     public static class BlueprintVessel {
42         public String blueprint;
43         public String name;
44         public int version;
45
46         @Override
47         public String toString() {
48             return String.format("%s:%d", this.name, this.version);
49         }
50     }
51
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);
57     }
58
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);
63     }
64
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")){
69                 continue;
70             }
71             BlueprintData blueprintData = new BlueprintData("1", blueprintCreator.createBlueprint(node.getComponentSpec()));
72             node.setBlueprintData(blueprintData);
73         }
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());
79
80             String tgtNodeId = edge.getTgt().getNode();
81             Node tgtNode = getNodeFromId(tgtNodeId);
82             blueprintCreator.resolveDmaapConnection(tgtNode ,edge.getTgt().getPort(),edge.getMetadata().getName());
83         }
84
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")) {
89                 continue;
90             }
91
92             BlueprintVessel bpv = new BlueprintVessel();
93             bpv.blueprint = node.getBlueprintData().getBlueprint_content();
94             bpv.version = createBlueprintVersion();
95             bpv.name = createBlueprintName(flowGraph, node.getComponentName());
96             blueprints.add(bpv);
97         }
98         return blueprints;
99     }
100
101     public Node getNodeFromId(String srcNodeId) {
102             for(Node node : flowGraph.getNodes()){
103                 if (node.getComponentId().equals(srcNodeId)) return node;
104             }
105             return null;
106         }
107 }