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