Changes for checkstyle 8.32
[policy/apex-pdp.git] / auth / cli-editor / src / main / java / org / onap / policy / apex / auth / clieditor / tosca / ApexCliToscaEditorMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.auth.clieditor.tosca;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.List;
27 import lombok.Getter;
28 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
29 import org.onap.policy.apex.auth.clieditor.CommandLineParameters;
30 import org.onap.policy.apex.auth.clieditor.utils.CliUtils;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class initiates an Apex CLI Tosca editor.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class ApexCliToscaEditorMain {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(ApexCliToscaEditorMain.class);
43
44     @Getter
45     private boolean failure;
46     private ApexCliToscaParameters parameters;
47     private ApexCommandLineEditorMain apexCliEditor;
48
49     /**
50      * Instantiates the Apex CLI Tosca editor.
51      *
52      * @param args the command line arguments
53      */
54     public ApexCliToscaEditorMain(final String[] args) {
55         final String argumentString = Arrays.toString(args);
56         LOGGER.info("Starting Apex CLI Tosca editor with arguments - {}", argumentString);
57
58         final ApexCliToscaParameterParser parser = new ApexCliToscaParameterParser();
59         parameters = parser.parse(args);
60         if (parameters.isHelpSet()) {
61             CliUtils.help(ApexCliToscaEditorMain.class.getName(), parser.getOptions());
62             return;
63         }
64         parameters.validate();
65
66         String policyModelFilePath = null;
67         try {
68             final File tempModelFile = File.createTempFile("policyModel", ".json");
69             policyModelFilePath = tempModelFile.getAbsolutePath();
70         } catch (IOException e) {
71             LOGGER.error("Cannot create the policy model temp file.", e);
72         }
73
74         List<String> cliArgsList = CliUtils.generateArgumentsForCliEditor(parameters, parser.getOptionVariableMap(),
75             CommandLineParameters.class);
76         cliArgsList.add("-o");
77         cliArgsList.add(policyModelFilePath);
78         String[] cliArgs = cliArgsList.toArray(new String[cliArgsList.size()]);
79
80         apexCliEditor = new ApexCommandLineEditorMain(cliArgs);
81         if (apexCliEditor.getErrorCount() == 0) {
82             LOGGER.info("Apex CLI editor completed execution. Creating the ToscaPolicy using the tosca template"
83                 + "skeleton file, config file, and policy model created.");
84
85             // Create the ToscaPolicy using the tosca template skeleton file, config file, and policy model created.
86             try {
87                 CliUtils.createToscaServiceTemplate(parameters, policyModelFilePath);
88                 LOGGER.info("Apex CLI Tosca editor completed execution.");
89             } catch (IOException | CoderException e) {
90                 failure = true;
91                 LOGGER.error("Failed to create the ToscaPolicy using the generated policy model, apex config file and"
92                     + " the tosca template skeleton file.");
93             }
94         } else {
95             failure = true;
96             LOGGER.error("execution of Apex command line editor failed: {} command execution failure(s) occurred",
97                 apexCliEditor.getErrorCount());
98         }
99
100     }
101
102     /**
103      * The main method, kicks off the cli tosca editor.
104      *
105      * @param args the arguments
106      */
107     public static void main(final String[] args) {
108         new ApexCliToscaEditorMain(args);
109     }
110 }