summaryrefslogtreecommitdiffstats
path: root/mod/runtimeapi/runtime-core/src/main/java/org/onap/dcae/runtime/core/FlowGraphParser.java
blob: d56455077c44d6268e4b72ef04edcc964c916c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019,2022 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<Node,Edge> flowGraph;
    private BlueprintCreator blueprintCreator;

    public FlowGraphParser(BlueprintCreator blueprintCreator) {
        this.blueprintCreator = blueprintCreator;
    }

    public void parse(FlowGraph<Node, Edge> 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() {
        // Removed year from the text to fix invalid integer issue (DCAEGEN2-3028)
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddHHmm").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<BlueprintVessel> 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<BlueprintVessel> 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;
        }
}