DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_tools / src / main / java / tools / Main.java
1 package tools;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4
5 import com.google.gson.JsonObject;
6 import json.Environment;
7 import json.response.ItemsResponse.Item;
8 import json.templateInfo.DeployTemplateConfig;
9 import json.templateInfo.TemplateInfo;
10
11 import utilities.IDcaeRestClient;
12 import utilities.IReport;
13 import utilities.Report;
14 import utilities.DcaeRestClient;
15
16 import java.io.*;
17 import java.net.ConnectException;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Map;
21
22 public class Main {
23     private static final String CONFIG_FILE = "DcaeDtDeployConfigFile";
24     private static final String ENVIRONMENT_CONFIG = "environment.resource";
25
26     private static LoggerError errLogger = LoggerError.getInstance();
27     private static LoggerDebug debugLogger = LoggerDebug.getInstance();
28
29     private Main() {
30         throw new IllegalAccessError("Utility class");
31     }
32
33     public static void main(String[] args) {
34         System.setProperty("logback.configurationFile", "conf/logback.xml");
35         debugLogger.log("Starting VFCMT template deployment");
36         if (args.length != 2) {
37             errLogger.log("Got " + args.length + ", but expecting exactly 2 arguments ONLY!");
38             return;
39         }
40         debugLogger.log("Arguments:");
41         Arrays.stream(args).forEach(arg -> debugLogger.log(arg));
42
43         initConfiguration(args);
44         IReport report = new Report();
45         try {
46             ObjectMapper mapper = new ObjectMapper();
47             DeployTemplateConfig deployTemplateConfig = mapper.readValue(new File(System.getProperty(CONFIG_FILE, "conf/config.json")), DeployTemplateConfig.class);
48             Environment environment = mapper.readValue(new File(System.getProperty(ENVIRONMENT_CONFIG, "conf/environment.json")), Environment.class);
49
50             IDcaeRestClient dcaeRestClient = new DcaeRestClient(environment.getCredential());
51             dcaeRestClient.init(environment);
52
53             EntitiesRetriever entitiesRetriever = new EntitiesRetriever(dcaeRestClient);
54             Map<String, List<Item>> elementsByFolderNames = entitiesRetriever.getElementsByFolder();
55
56             TemplateContainer templateContainer = new TemplateContainer(report, dcaeRestClient, deployTemplateConfig.getTemplateInfo(), elementsByFolderNames);
57             Map<TemplateInfo, JsonObject> templateInfoToJsonObjectMap = templateContainer.getCdumps();
58
59             DeployTemplate deployTemplate = new DeployTemplate(report, dcaeRestClient);
60             deployTemplate.deploy(templateInfoToJsonObjectMap);
61
62             debugLogger.log( "VFCMT template deployment completed successfully");
63         } catch (RuntimeException e) {
64             errLogger.log("ERROR - Template deployment failed with error " + e);
65         } catch (ConnectException e) {
66             errLogger.log( "ERROR - Failed connection to server, are you on AT&T network? {}" + e);
67         } catch (IOException e) {
68             errLogger.log( "ERROR - Fatal Error! " + e);
69         } finally {
70             debugLogger.log(report.toString());
71         }
72     }
73
74     private static void initConfiguration(String[] args) {
75         System.setProperty(ENVIRONMENT_CONFIG, args[0]);
76         System.setProperty(CONFIG_FILE, args[1]);
77     }
78
79
80 }