Update runtime-api with refactored bp-gen
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / main / java / org / onap / dcae / runtime / web / configuration / BlueprintCreatorConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19 package org.onap.dcae.runtime.web.configuration;
20
21 import java.io.IOException;
22 import java.nio.file.FileAlreadyExistsException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.onap.blueprintgenerator.BlueprintGeneratorConfiguration;
31 import org.onap.blueprintgenerator.service.BlueprintCreatorService;
32 import org.onap.blueprintgenerator.service.base.BlueprintService;
33 import org.onap.blueprintgenerator.service.base.FixesService;
34 import org.onap.blueprintgenerator.service.common.ComponentSpecService;
35 import org.onap.dcae.runtime.core.FlowGraphParser;
36 import org.onap.dcae.runtime.core.blueprint_creator.BlueprintCreatorOnap;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Import;
42 import org.springframework.context.annotation.Primary;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.core.env.Environment;
45 import org.yaml.snakeyaml.Yaml;
46
47 @Configuration
48 @Import(BlueprintGeneratorConfiguration.class)
49 public class BlueprintCreatorConfig {
50
51     private final Environment env;
52     private final BlueprintService blueprintService;
53     private final ComponentSpecService componentSpecService;
54     private final BlueprintCreatorService blueprintCreatorService;
55     private final FixesService fixesService;
56
57     @Value("${onap.topicUrl}")
58     String onapDublinTopicUrl;
59
60     @Value("${onap.useDmaapPlugin}")
61     boolean useDmaapPlugin;
62
63     @Value("${onap.import.cloudifyPlugin}")
64     String onapDublinImportCloudifyPlugin;
65
66     @Value("${onap.import.k8sPlugin}")
67     String onapDublinImportK8sPlugin;
68
69     @Value("${onap.import.policyPlugin}")
70     String onapDublinImportPolicyPlugin;
71
72     @Value("${onap.import.postgresPlugin}")
73     String onapDublinImportPostgresPlugin;
74
75     @Value("${onap.import.clampPlugin}")
76     String onapDublinImportClampPlugin;
77
78     @Value("${onap.import.dmaapPlugin}")
79     String onapDublinImportDmaapPlugin;
80
81     @Autowired
82     public BlueprintCreatorConfig(Environment env, BlueprintService blueprintService,
83         ComponentSpecService componentSpecService, BlueprintCreatorService blueprintCreatorService,
84         FixesService fixesService) {
85         this.env = env;
86         this.blueprintService = blueprintService;
87         this.componentSpecService = componentSpecService;
88         this.blueprintCreatorService = blueprintCreatorService;
89         this.fixesService = fixesService;
90     }
91
92
93     @Profile("onap")
94     @Primary
95     @Bean
96     public FlowGraphParser getFlowGraphParserForOnapDublin() {
97         BlueprintCreatorOnap blueprintCreatorOnap = new BlueprintCreatorOnap(componentSpecService,
98             blueprintCreatorService, blueprintService, fixesService);
99         blueprintCreatorOnap.setImportFilePath(writeImportsTofile());
100         blueprintCreatorOnap.setUseDmaapPlugin(useDmaapPlugin);
101         FlowGraphParser flowGraphParser = new FlowGraphParser(blueprintCreatorOnap);
102         return flowGraphParser;
103     }
104
105     private String writeImportsTofile() {
106         String contentToWrite = getContentToWrite();
107         String fielPath = "";
108         try {
109             Path path = createDataImportDirAndImportFile();
110             fielPath = Files.write(path, contentToWrite.getBytes()).toString();
111             new String(Files.readAllBytes(path));
112         } catch (IOException e) {
113             e.printStackTrace();
114         }
115         System.out.println(fielPath);
116         return fielPath;
117     }
118
119     private Path createDataImportDirAndImportFile() {
120         Path importDirPath = Paths.get("./data/imports").toAbsolutePath().normalize();
121         Path onapImportFilePath = Paths.get("./data/imports/onapImports.yaml").toAbsolutePath().normalize();
122         try {
123             Files.createDirectories(importDirPath);
124             Files.createFile(onapImportFilePath);
125         } catch (FileAlreadyExistsException ignored) {
126         } catch (IOException e) {
127             e.printStackTrace();
128         }
129         return onapImportFilePath;
130     }
131
132     private String getContentToWrite() {
133         Map<String, Object> result = new HashMap<String, Object>();
134         List<String> importList = new ArrayList<String>();
135         importList.add(onapDublinImportCloudifyPlugin);
136         importList.add(onapDublinImportK8sPlugin);
137         importList.add(onapDublinImportPolicyPlugin);
138
139         importList.add(onapDublinImportPostgresPlugin);
140         importList.add(onapDublinImportClampPlugin);
141         importList.add(onapDublinImportDmaapPlugin);
142
143         result.put("imports", importList);
144         return new Yaml().dump(result);
145     }
146
147 }