integrate helm-chart-generator to runtime api
[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) 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 org.onap.dcae.runtime.core.Edge;
21 import org.onap.dcae.runtime.core.FlowGraph;
22 import org.onap.dcae.runtime.core.FlowGraphParser;
23 import org.onap.dcae.runtime.core.FlowGraphParser.BlueprintVessel;
24 import org.onap.dcae.runtime.core.Node;
25 import org.onap.dcae.runtime.web.exception.MainGraphAlreadyExistException;
26 import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException;
27 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
28 import org.onap.dcae.runtime.web.models.GraphRequest;
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     @Autowired
50     private GraphActionsParser actionsParser;
51
52     @Override
53     public FlowGraph<Node, Edge> getMainGraph() {
54         if(mainFlowGraph == null){
55             throw new MainGraphNotFoundException();
56         }
57         return mainFlowGraph;
58     }
59
60     @Override
61     public boolean initializeMainGraph(GraphRequest mainGraphRequest) {
62         if(mainFlowGraph != null){
63             throw new MainGraphAlreadyExistException("Can not initialize the main graph, it already exists");
64         }
65         mainFlowGraph = new FlowGraph<Node,Edge>(mainGraphRequest.getId(),mainGraphRequest.getName(),
66                             true, mainGraphRequest.getDescription());
67         mainFlowGraph.addNode(getDummyNode());
68         flowGraphParser.parse(mainFlowGraph);
69         return true;
70     }
71
72     private Node getDummyNode() {
73         return new Node("dummy_id","dummy_name","dummy_compspec");
74     }
75
76     @Override
77     public List<BlueprintVessel> distribute(DistributeGraphRequest distributeGraphRequest) {
78         //1.Iterate through list of actions
79         logger.info("applying actions to graph");
80         actionsParser.applyActionsToGraph(mainFlowGraph, distributeGraphRequest);
81
82         //2. generate blueprint from compspec of the node
83         logger.info("generating blueprints for the affected nodes");
84         List<BlueprintVessel> blueprints = generateBlueprintsForAffectedNodes(distributeGraphRequest);
85
86         //3a. Push blueprints to the inventory
87         logger.info("pushing bluepirnts to the dashboard inventrory");
88         blueprintInventory.distributeToInventory(blueprints);
89         //3b. return blueprint map
90         return blueprints;
91     }
92
93     @Override
94     public void deleteMainGraph() {
95         if(mainFlowGraph == null){
96             throw new MainGraphNotFoundException();
97         }
98         mainFlowGraph = null;
99     }
100
101     private List<BlueprintVessel> generateBlueprintsForAffectedNodes(DistributeGraphRequest distributeGraphRequest) {
102         return flowGraphParser.createAndProcessBlueprints();
103     }
104
105
106 }