integrate helm-chart-generator to runtime api
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / service / GraphServiceHelmProxy.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
19 package org.onap.dcae.runtime.web.service;
20
21 import org.onap.dcae.runtime.core.BlueprintData;
22 import org.onap.dcae.runtime.core.Edge;
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.core.helm.HelmChartGeneratorClient;
27 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
28 import org.onap.dcae.runtime.web.models.GraphRequest;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31 import org.springframework.context.annotation.ComponentScan;
32 import org.springframework.context.annotation.Primary;
33 import org.springframework.stereotype.Service;
34
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * A proxy class which will be enable if artifact.type is set to HELM
41  */
42 @Service
43 @Primary
44 @ConditionalOnProperty(
45         value="artifact.type",
46         havingValue = "HELM",
47         matchIfMissing = true)
48 @ComponentScan(basePackages = "org.onap.dcae.runtime.core")
49 public class GraphServiceHelmProxy implements GraphService {
50
51     @Autowired
52     private GraphService defaultGraphService;
53
54     @Autowired
55     private HelmChartGeneratorClient helmChartGeneratorClient;
56
57     @Autowired
58     private GraphActionsParser actionsParser;
59
60     /**
61      * returns a main graph
62      * @return main graph
63      */
64     @Override
65     public FlowGraph<Node, Edge> getMainGraph() {
66         return defaultGraphService.getMainGraph();
67     }
68
69     /**
70      * initialize a main graph
71      * @param mainGraph a main graph request
72      */
73     @Override
74     public boolean initializeMainGraph(GraphRequest mainGraph) {
75         return defaultGraphService.initializeMainGraph(mainGraph);
76     }
77
78     /**
79      * apply actions on the main graph and creates, distributes helm charts
80      * @param distributeGraphRequest distribute request
81      * @return list of distributed charts
82      */
83     @Override
84     public List<FlowGraphParser.BlueprintVessel> distribute(DistributeGraphRequest distributeGraphRequest) {
85         actionsParser.applyActionsToGraph(getMainGraph(), distributeGraphRequest);
86         return createHelmCharts();
87     }
88
89     public List<FlowGraphParser.BlueprintVessel> createHelmCharts() {
90         final FlowGraph<Node, Edge> flowGraph = getMainGraph();
91         List<FlowGraphParser.BlueprintVessel> blueprints = new ArrayList<>();
92         for(Node node : flowGraph.getNodes()){
93             if(node.getComponentId().equals("dummy_id")){
94                 continue;
95             }
96             final File helmChart = helmChartGeneratorClient.generateHelmChart(node.getComponentSpec());
97             helmChartGeneratorClient.distribute(helmChart);
98             BlueprintData blueprintData = new BlueprintData("1", helmChart.getName());
99             node.setBlueprintData(blueprintData);
100             blueprints.add(createBlueprintVessel(helmChart));
101         }
102         return blueprints;
103     }
104
105     private FlowGraphParser.BlueprintVessel createBlueprintVessel(File helmChart) {
106         FlowGraphParser.BlueprintVessel bpv = new FlowGraphParser.BlueprintVessel();
107         bpv.blueprint = helmChart.getName();
108         bpv.version = 1;
109         bpv.name = helmChart.getName();
110         return bpv;
111     }
112
113     /**
114      * deletes main graph
115      */
116     @Override
117     public void deleteMainGraph() {
118         defaultGraphService.deleteMainGraph();
119     }
120 }