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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
18 package org.onap.dcae.runtime.web.service;
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;
34 import java.util.List;
37 public class GraphServiceImpl implements GraphService{
40 BlueprintInventory blueprintInventory;
42 private FlowGraph<Node, Edge> mainFlowGraph;
44 Logger logger = LoggerFactory.getLogger(GraphServiceImpl.class);
47 private FlowGraphParser flowGraphParser;
50 public FlowGraph<Node, Edge> getMainGraph() {
51 if(mainFlowGraph == null){
52 throw new MainGraphNotFoundException();
58 public boolean initializeMainGraph(GraphRequest mainGraphRequest) {
59 if(mainFlowGraph != null){
60 throw new MainGraphAlreadyExistException("Can not initialize the main graph, it already exists");
62 mainFlowGraph = new FlowGraph<Node,Edge>(mainGraphRequest.getId(),mainGraphRequest.getName(),
63 true, mainGraphRequest.getDescription());
64 mainFlowGraph.addNode(getDummyNode());
65 flowGraphParser.parse(mainFlowGraph);
69 private Node getDummyNode() {
70 return new Node("dummy_id","dummy_name","dummy_compspec");
74 public List<BlueprintVessel> distribute(DistributeGraphRequest distributeGraphRequest) {
75 //1.Iterate through list of actions
76 logger.info("applying actions to graph");
77 applyActionsToGraph(distributeGraphRequest);
79 //2. generate blueprint from compspec of the node
80 logger.info("generating blueprints for the affected nodes");
81 List<BlueprintVessel> blueprints = generateBlueprintsForAffectedNodes(distributeGraphRequest);
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
91 public void deleteMainGraph() {
92 if(mainFlowGraph == null){
93 throw new MainGraphNotFoundException();
98 private List<BlueprintVessel> generateBlueprintsForAffectedNodes(DistributeGraphRequest distributeGraphRequest) {
99 return flowGraphParser.createAndProcessBlueprints();
102 private void applyActionsToGraph(DistributeGraphRequest distributeGraphRequest) {
103 if(distributeGraphRequest.getActions() == null){
104 throw new ActionsNotDefinedException("Action(s) must be defined in the request");
106 for(Action action : distributeGraphRequest.getActions()){
107 if(action.getCommand().equals("addnode")){
108 Node node = prepareNodeFromAddNAddNodeAction(action);
109 mainFlowGraph.addNode(node);
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);
122 private Node fillPlaceholderIfNodeIsEmpty(Node node) {
124 node = flowGraphParser.getNodeFromId("dummy_id");
130 private Edge prepareEdgeFromAddEdgeAction(Action action) {
131 ObjectMapper objectMapper = new ObjectMapper();
132 Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class);
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");
141 if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){
142 EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
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);