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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.auth.clieditor.tosca;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.List;
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;
36 * This class initiates an Apex CLI Tosca editor.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class ApexCliToscaEditorMain {
42 private static final Logger LOGGER = LoggerFactory.getLogger(ApexCliToscaEditorMain.class);
45 private boolean failure;
46 private ApexCliToscaParameters parameters;
47 private ApexCommandLineEditorMain apexCliEditor;
50 * Instantiates the Apex CLI Tosca editor.
52 * @param args the command line arguments
54 public ApexCliToscaEditorMain(final String[] args) {
55 final String argumentString = Arrays.toString(args);
56 LOGGER.info("Starting Apex CLI Tosca editor with arguments - {}", argumentString);
58 final ApexCliToscaParameterParser parser = new ApexCliToscaParameterParser();
59 parameters = parser.parse(args);
60 if (parameters.isHelpSet()) {
61 CliUtils.help(ApexCliToscaEditorMain.class.getName(), parser.getOptions());
64 parameters.validate();
66 String policyModelFilePath = null;
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);
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()]);
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.");
85 // Create the ToscaPolicy using the tosca template skeleton file, config file, and policy model created.
87 CliUtils.createToscaServiceTemplate(parameters, policyModelFilePath);
88 LOGGER.info("Apex CLI Tosca editor completed execution.");
89 } catch (IOException | CoderException e) {
91 LOGGER.error("Failed to create the ToscaPolicy using the generated policy model, apex config file and"
92 + " the tosca template skeleton file.");
96 LOGGER.error("execution of Apex command line editor failed: {} command execution failure(s) occurred",
97 apexCliEditor.getErrorCount());
103 * The main method, kicks off the cli tosca editor.
105 * @param args the arguments
107 public static void main(final String[] args) {
108 new ApexCliToscaEditorMain(args);