Add mod/runtimeapi
[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.BlueprintCreatorOnapDublin;
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("${onapDublin.topicUrl}")
48     String onapDublinTopicUrl;
49
50     @Value("${onapDublin.import.cloudifyPlugin}")
51     String onapDublinImportCloudifyPlugin;
52
53     @Value("${onapDublin.import.k8sPlugin}")
54     String onapDublinImportK8sPlugin;
55
56     @Value("${onapDublin.import.policyPlugin}")
57     String onapDublinImportPolicyPlugin;
58
59     
60     @Profile("onap_dublin")
61     @Primary
62     @Bean
63     public FlowGraphParser getFlowGraphParserForOnapDublin(){
64         BlueprintCreatorOnapDublin blueprintCreatorOnapDublin = new BlueprintCreatorOnapDublin();
65         blueprintCreatorOnapDublin.setTopicUrl(onapDublinTopicUrl);
66         blueprintCreatorOnapDublin.setImportFilePath(writeImportsTofile());
67         FlowGraphParser flowGraphParser = new FlowGraphParser(blueprintCreatorOnapDublin);
68         return flowGraphParser;
69     }
70
71     private String writeImportsTofile() {
72         String contentToWrite = getContentToWrite();
73         String fielPath = "";
74         try {
75             Path path = createDataImportDirAndImportFile();
76             fielPath = Files.write(path,contentToWrite.getBytes()).toString();
77             new String(Files.readAllBytes(path));
78         } catch (IOException e) {
79             e.printStackTrace();
80         }
81         System.out.println(fielPath);
82         return fielPath;
83     }
84
85     private Path createDataImportDirAndImportFile() {
86         Path importDirPath = Paths.get("./data/imports").toAbsolutePath().normalize();
87         Path onapDublinImportFilePath = Paths.get("./data/imports/onapDublinImports.yaml").toAbsolutePath().normalize();
88         try {
89             Files.createDirectories(importDirPath);
90             Files.createFile(onapDublinImportFilePath);
91         }
92         catch (FileAlreadyExistsException ignored){
93         }
94         catch (IOException e) {
95             e.printStackTrace();
96         }
97         return onapDublinImportFilePath;
98     }
99
100     private String getContentToWrite() {
101         Map<String,Object> result = new HashMap<String, Object>();
102         List<String> importList = new ArrayList<String>();
103         importList.add(onapDublinImportCloudifyPlugin);
104         importList.add(onapDublinImportK8sPlugin);
105         importList.add(onapDublinImportPolicyPlugin);
106         result.put("imports",importList);
107         return new Yaml().dump(result);
108     }
109
110 }