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
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 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;
33 * A helper class that parses the actions from the request and apply them to the main graph
36 public class GraphActionsParser {
39 private FlowGraphParser flowGraphParser;
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");
45 for(Action action : distributeGraphRequest.getActions()){
46 if(action.getCommand().equals("addnode")){
47 Node node = prepareNodeFromAddNAddNodeAction(action);
48 mainFlowGraph.addNode(node);
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);
61 private Node fillPlaceholderIfNodeIsEmpty(Node node) {
63 node = flowGraphParser.getNodeFromId("dummy_id");
69 private Edge prepareEdgeFromAddEdgeAction(Action action) {
70 ObjectMapper objectMapper = new ObjectMapper();
71 Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class);
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");
80 if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){
81 EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
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);