f1329c14f1bb87352a4841adc8c426161f2e4dbf
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / service / GraphServiceImpl.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.web.service;
19
20 import org.onap.dcae.runtime.core.*;
21 import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
22 import org.onap.dcae.runtime.web.exception.ActionsNotDefinedException;
23 import org.onap.dcae.runtime.web.exception.MainGraphAlreadyExistException;
24 import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException;
25 import org.onap.dcae.runtime.web.models.Action;
26 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
27 import org.onap.dcae.runtime.web.models.GraphRequest;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 import java.util.List;
35
36 @Service
37 public class GraphServiceImpl implements GraphService{
38
39     @Autowired
40     BlueprintInventory blueprintInventory;
41
42     private FlowGraph<Node, Edge> mainFlowGraph;
43
44     Logger logger = LoggerFactory.getLogger(GraphServiceImpl.class);
45
46     @Autowired
47     private FlowGraphParser flowGraphParser;
48
49     @Override
50     public FlowGraph<Node, Edge> getMainGraph() {
51         if(mainFlowGraph == null){
52             throw new MainGraphNotFoundException();
53         }
54         return mainFlowGraph;
55     }
56
57     @Override
58     public boolean initializeMainGraph(GraphRequest mainGraphRequest) {
59         if(mainFlowGraph != null){
60             throw new MainGraphAlreadyExistException("Can not initialize the main graph, it already exists");
61         }
62         mainFlowGraph = new FlowGraph<Node,Edge>(mainGraphRequest.getId(),mainGraphRequest.getName(),
63                             true, mainGraphRequest.getDescription());
64         mainFlowGraph.addNode(getDummyNode());
65         flowGraphParser.parse(mainFlowGraph);
66         return true;
67     }
68
69     private Node getDummyNode() {
70         return new Node("dummy_id","dummy_name","dummy_compspec");
71     }
72
73     @Override
74     public List<BlueprintVessel> distribute(DistributeGraphRequest distributeGraphRequest) {
75         //1.Iterate through list of actions
76         logger.info("applying actions to graph");
77         applyActionsToGraph(distributeGraphRequest);
78
79         //2. generate blueprint from compspec of the node
80         logger.info("generating blueprints for the affected nodes");
81         List<BlueprintVessel> blueprints = generateBlueprintsForAffectedNodes(distributeGraphRequest);
82
83         //3a. Push blueprints to the inventory
84         logger.info("pushing bluepirnts to the dashboard inventrory");
85         blueprintInventory.distributeToInventory(blueprints);
86         //3b. return blueprint map
87         return blueprints;
88     }
89
90     @Override
91     public void deleteMainGraph() {
92         if(mainFlowGraph == null){
93             throw new MainGraphNotFoundException();
94         }
95         mainFlowGraph = null;
96     }
97
98     private List<BlueprintVessel> generateBlueprintsForAffectedNodes(DistributeGraphRequest distributeGraphRequest) {
99         return flowGraphParser.createAndProcessBlueprints();
100     }
101
102     private void applyActionsToGraph(DistributeGraphRequest distributeGraphRequest) {
103         if(distributeGraphRequest.getActions() == null){
104             throw new ActionsNotDefinedException("Action(s) must be defined in the request");
105         }
106         for(Action action : distributeGraphRequest.getActions()){
107             if(action.getCommand().equals("addnode")){
108                 Node node = prepareNodeFromAddNAddNodeAction(action);
109                 mainFlowGraph.addNode(node);
110             }
111             else if(action.getCommand().equals("addedge")) {
112                 Edge edge = prepareEdgeFromAddEdgeAction(action);
113                 Node srcNode = flowGraphParser.getNodeFromId(edge.getSrc().getNode());
114                 Node tgtNode = flowGraphParser.getNodeFromId(edge.getTgt().getNode());
115                 srcNode = fillPlaceholderIfNodeIsEmpty(srcNode);
116                 tgtNode =fillPlaceholderIfNodeIsEmpty(tgtNode);
117                 mainFlowGraph.addEdge(srcNode,tgtNode,edge);
118             }
119         }
120     }
121
122     private Node fillPlaceholderIfNodeIsEmpty(Node node) {
123         if (node == null) {
124             node = flowGraphParser.getNodeFromId("dummy_id");
125         }
126         return node;
127     }
128
129
130     private Edge prepareEdgeFromAddEdgeAction(Action action) {
131         ObjectMapper objectMapper = new ObjectMapper();
132         Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class);
133         return edge;
134     }
135
136     private void fillPlaceholderIfLocaionsAreEmpty(Edge edge) {
137         if(edge.getSrc().getNode() == null && edge.getSrc().getPort() == null){
138             EdgeLocation src = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
139             edge.setSrc(src);
140         }
141         if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){
142             EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
143             edge.setTgt(tgt);
144         }
145     }
146
147     private Node prepareNodeFromAddNAddNodeAction(Action action) {
148         String componentId = (String) action.getPayload().get("component_id");
149         String componentName = (String) action.getPayload().get("name");
150         String componentSpec = (String) action.getPayload().get("component_spec");
151         return new Node(componentId,componentName,componentSpec);
152     }
153
154 }