[DCAEMOD/Runtime] Fix NFE error on BP generation
[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,2022 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         // Removed year from the text to fix invalid integer issue (DCAEGEN2-3028)
54         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddHHmm").withZone(ZoneId.of("UTC"));
55         Instant instant = Instant.now();
56         String timestamp = formatter.format(instant);
57         return Integer.parseInt(timestamp);
58     }
59
60     private static String createBlueprintName(FlowGraph flowGraph, String componentName) {
61         // NOTE: Replacing whitespaces with dash
62         // NOTE: Separator must be dash or underbar otherwise cloudify will flip out
63         return String.format("%s_%s", flowGraph.getName().replace(" ", "-"), componentName);
64     }
65
66     public List<BlueprintVessel> createAndProcessBlueprints() {
67         //1. generate blueprints for all the nodes
68         for(Node node : flowGraph.getNodes()){
69             if(node.getComponentId().equals("dummy_id")){
70                 continue;
71             }
72             BlueprintData blueprintData = new BlueprintData("1", blueprintCreator.createBlueprint(node.getComponentSpec()));
73             node.setBlueprintData(blueprintData);
74         }
75         //2. replace dmaap info from the edges
76         for(Edge edge : flowGraph.getEdges()){
77             String srcNodeId = edge.getSrc().getNode();
78             Node srcNode = getNodeFromId(srcNodeId);
79             blueprintCreator.resolveDmaapConnection(srcNode ,edge.getSrc().getPort(),edge.getMetadata().getName());
80
81             String tgtNodeId = edge.getTgt().getNode();
82             Node tgtNode = getNodeFromId(tgtNodeId);
83             blueprintCreator.resolveDmaapConnection(tgtNode ,edge.getTgt().getPort(),edge.getMetadata().getName());
84         }
85
86         //3. return processed blueprints along with blueprint_name
87         List<BlueprintVessel> blueprints = new ArrayList<>();
88         for(Node node: flowGraph.getNodes()){
89             if(node.getComponentId().equals("dummy_id")) {
90                 continue;
91             }
92
93             BlueprintVessel bpv = new BlueprintVessel();
94             bpv.blueprint = node.getBlueprintData().getBlueprint_content();
95             bpv.version = createBlueprintVersion();
96             bpv.name = createBlueprintName(flowGraph, node.getComponentName());
97             blueprints.add(bpv);
98         }
99         return blueprints;
100     }
101
102     public Node getNodeFromId(String srcNodeId) {
103             for(Node node : flowGraph.getNodes()){
104                 if (node.getComponentId().equals(srcNodeId)) return node;
105             }
106             return null;
107         }
108 }