0741bfaefdcb4fb71684d8756223cd49614de1c5
[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  * ================================================================================
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.configuration;
19
20 import org.onap.dcae.runtime.core.FlowGraphParser;
21 import org.onap.dcae.runtime.core.blueprint_creator.BlueprintCreatorOnap;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.context.annotation.Primary;
27 import org.springframework.context.annotation.Profile;
28 import org.springframework.core.env.Environment;
29 import org.yaml.snakeyaml.Yaml;
30
31 import java.io.IOException;
32 import java.nio.file.FileAlreadyExistsException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 @Configuration
42 public class BlueprintCreatorConfig {
43
44     @Autowired
45     Environment env;
46     
47     @Value("${onap.topicUrl}")
48     String onapDublinTopicUrl;
49
50     @Value("${onap.useDmaapPlugin}")
51     boolean useDmaapPlugin;
52
53     @Value("${onap.import.cloudifyPlugin}")
54     String onapDublinImportCloudifyPlugin;
55
56     @Value("${onap.import.k8sPlugin}")
57     String onapDublinImportK8sPlugin;
58
59     @Value("${onap.import.policyPlugin}")
60     String onapDublinImportPolicyPlugin;
61
62     @Value("${onap.import.postgresPlugin}")
63     String onapDublinImportPostgresPlugin;
64
65     @Value("${onap.import.clampPlugin}")
66     String onapDublinImportClampPlugin;
67
68     @Value("${onap.import.dmaapPlugin}")
69     String onapDublinImportDmaapPlugin;
70
71
72     @Profile("onap")
73     @Primary
74     @Bean
75     public FlowGraphParser getFlowGraphParserForOnapDublin(){
76         BlueprintCreatorOnap blueprintCreatorOnap = new BlueprintCreatorOnap();
77         blueprintCreatorOnap.setImportFilePath(writeImportsTofile());
78         blueprintCreatorOnap.setUseDmaapPlugin(useDmaapPlugin);
79         FlowGraphParser flowGraphParser = new FlowGraphParser(blueprintCreatorOnap);
80         return flowGraphParser;
81     }
82
83     private String writeImportsTofile() {
84         String contentToWrite = getContentToWrite();
85         String fielPath = "";
86         try {
87             Path path = createDataImportDirAndImportFile();
88             fielPath = Files.write(path,contentToWrite.getBytes()).toString();
89             new String(Files.readAllBytes(path));
90         } catch (IOException e) {
91             e.printStackTrace();
92         }
93         System.out.println(fielPath);
94         return fielPath;
95     }
96
97     private Path createDataImportDirAndImportFile() {
98         Path importDirPath = Paths.get("./data/imports").toAbsolutePath().normalize();
99         Path onapImportFilePath = Paths.get("./data/imports/onapImports.yaml").toAbsolutePath().normalize();
100         try {
101             Files.createDirectories(importDirPath);
102             Files.createFile(onapImportFilePath);
103         }
104         catch (FileAlreadyExistsException ignored){
105         }
106         catch (IOException e) {
107             e.printStackTrace();
108         }
109         return onapImportFilePath;
110     }
111
112     private String getContentToWrite() {
113         Map<String,Object> result = new HashMap<String, Object>();
114         List<String> importList = new ArrayList<String>();
115         importList.add(onapDublinImportCloudifyPlugin);
116         importList.add(onapDublinImportK8sPlugin);
117         importList.add(onapDublinImportPolicyPlugin);
118
119         importList.add(onapDublinImportPostgresPlugin);
120         importList.add(onapDublinImportClampPlugin);
121         importList.add(onapDublinImportDmaapPlugin);
122                 
123     
124    
125         result.put("imports",importList);
126         return new Yaml().dump(result);
127     }
128
129 }