integrate helm-chart-generator to runtime api
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / service / GraphActionsParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.web.service;
19
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import org.onap.dcae.runtime.core.Edge;
22 import org.onap.dcae.runtime.core.EdgeLocation;
23 import org.onap.dcae.runtime.core.FlowGraph;
24 import org.onap.dcae.runtime.core.FlowGraphParser;
25 import org.onap.dcae.runtime.core.Node;
26 import org.onap.dcae.runtime.web.exception.ActionsNotDefinedException;
27 import org.onap.dcae.runtime.web.models.Action;
28 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 /**
33  * A helper class that parses the actions from the request and apply them to the main graph
34  */
35 @Component
36 public class GraphActionsParser {
37
38     @Autowired
39     private FlowGraphParser flowGraphParser;
40
41     void applyActionsToGraph(FlowGraph<Node, Edge> mainFlowGraph, DistributeGraphRequest distributeGraphRequest) {
42         if(distributeGraphRequest.getActions() == null){
43             throw new ActionsNotDefinedException("Action(s) must be defined in the request");
44         }
45         for(Action action : distributeGraphRequest.getActions()){
46             if(action.getCommand().equals("addnode")){
47                 Node node = prepareNodeFromAddNAddNodeAction(action);
48                 mainFlowGraph.addNode(node);
49             }
50             else if(action.getCommand().equals("addedge")) {
51                 Edge edge = prepareEdgeFromAddEdgeAction(action);
52                 Node srcNode = flowGraphParser.getNodeFromId(edge.getSrc().getNode());
53                 Node tgtNode = flowGraphParser.getNodeFromId(edge.getTgt().getNode());
54                 srcNode = fillPlaceholderIfNodeIsEmpty(srcNode);
55                 tgtNode =fillPlaceholderIfNodeIsEmpty(tgtNode);
56                 mainFlowGraph.addEdge(srcNode,tgtNode,edge);
57             }
58         }
59     }
60
61     private Node fillPlaceholderIfNodeIsEmpty(Node node) {
62         if (node == null) {
63             node = flowGraphParser.getNodeFromId("dummy_id");
64         }
65         return node;
66     }
67
68
69     private Edge prepareEdgeFromAddEdgeAction(Action action) {
70         ObjectMapper objectMapper = new ObjectMapper();
71         Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class);
72         return edge;
73     }
74
75     private void fillPlaceholderIfLocaionsAreEmpty(Edge edge) {
76         if(edge.getSrc().getNode() == null && edge.getSrc().getPort() == null){
77             EdgeLocation src = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
78             edge.setSrc(src);
79         }
80         if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){
81             EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
82             edge.setTgt(tgt);
83         }
84     }
85
86     private Node prepareNodeFromAddNAddNodeAction(Action action) {
87         String componentId = (String) action.getPayload().get("component_id");
88         String componentName = (String) action.getPayload().get("name");
89         String componentSpec = (String) action.getPayload().get("component_spec");
90         return new Node(componentId,componentName,componentSpec);
91     }
92 }