- private void applyActionsToGraph(DistributeGraphRequest distributeGraphRequest) {
- if(distributeGraphRequest.getActions() == null){
- throw new ActionsNotDefinedException("Action(s) must be defined in the request");
- }
- for(Action action : distributeGraphRequest.getActions()){
- if(action.getCommand().equals("addnode")){
- Node node = prepareNodeFromAddNAddNodeAction(action);
- mainFlowGraph.addNode(node);
- }
- else if(action.getCommand().equals("addedge")) {
- Edge edge = prepareEdgeFromAddEdgeAction(action);
- Node srcNode = flowGraphParser.getNodeFromId(edge.getSrc().getNode());
- Node tgtNode = flowGraphParser.getNodeFromId(edge.getTgt().getNode());
- srcNode = fillPlaceholderIfNodeIsEmpty(srcNode);
- tgtNode =fillPlaceholderIfNodeIsEmpty(tgtNode);
- mainFlowGraph.addEdge(srcNode,tgtNode,edge);
- }
- }
- }
-
- private Node fillPlaceholderIfNodeIsEmpty(Node node) {
- if (node == null) {
- node = flowGraphParser.getNodeFromId("dummy_id");
- }
- return node;
- }
-
-
- private Edge prepareEdgeFromAddEdgeAction(Action action) {
- ObjectMapper objectMapper = new ObjectMapper();
- Edge edge = objectMapper.convertValue(action.getPayload(),Edge.class);
- return edge;
- }
-
- private void fillPlaceholderIfLocaionsAreEmpty(Edge edge) {
- if(edge.getSrc().getNode() == null && edge.getSrc().getPort() == null){
- EdgeLocation src = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
- edge.setSrc(src);
- }
- if(edge.getTgt().getNode() == null && edge.getTgt().getPort() == null){
- EdgeLocation tgt = new EdgeLocation("node-id-placeholder", "node-port-placeholder");
- edge.setTgt(tgt);
- }
- }
-
- private Node prepareNodeFromAddNAddNodeAction(Action action) {
- String componentId = (String) action.getPayload().get("component_id");
- String componentName = (String) action.getPayload().get("name");
- String componentSpec = (String) action.getPayload().get("component_spec");
- return new Node(componentId,componentName,componentSpec);
- }