3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
7 * * ================================================================================
8 * * Licensed under the Apache License, Version 2.0 (the "License");
9 * * you may not use this file except in compliance with the License.
10 * * You may obtain a copy of the License at
12 * * http://www.apache.org/licenses/LICENSE-2.0
14 * * Unless required by applicable law or agreed to in writing, software
15 * * distributed under the License is distributed on an "AS IS" BASIS,
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * * See the License for the specific language governing permissions and
18 * * limitations under the License.
19 * * ============LICENSE_END=========================================================
24 package org.onap.blueprintgenerator.service.common;
26 import org.apache.commons.cli.CommandLineParser;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.ParseException;
29 import org.apache.commons.cli.DefaultParser;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.CommandLine;
32 import org.onap.blueprintgenerator.model.common.Input;
34 import org.springframework.stereotype.Service;
35 import org.springframework.util.StringUtils;
37 import static java.lang.System.exit;
40 * @author : Ravi Mantena
41 * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service used by ONAP and
42 * DMAAP Blueprint to Print Instructions and Parse Inputs
44 @Service("onapCommonUtilsService")
45 public class CommonUtils {
48 * Prints input arguments options to run the Blueprint Application
50 public void printInstructions() {
51 System.out.println("OPTIONS:");
53 "-i OR --component-spec: The path of the ONAP Blueprint INPUT JSON SPEC FILE (Required)");
55 "-p OR --blueprint-path: The path of the ONAP Blueprint OUTPUT where it will be saved (Required)");
57 "-n OR --blueprint-name: The NAME of the ONAP Blueprint OUTPUT that will be created (Optional)");
59 .println("-t OR --imports: The path of the ONAP Blueprint IMPORT FILE (Optional)");
61 "-o OR --service-name-override: The Value used to OVERRIDE the SERVICE NAME of the ONAP Blueprint (Optional)");
63 "-d OR --dmaap-plugin: The option to create a ONAP Blueprint with DMAAP Plugin (Optional)");
65 "Syntax to run from command line: \n For Blueprint : java -jar target/<JAR Filename>.jar app ONAP -i componentspec -p OutputBlueprintPath -n Blueprintname -d \n For PolicyCreate: java -jar target/<JAR Filename>.jar app ONAP -type policycreate -i componentspec -p OutputPolicyPath");
69 * Parses Input Arguments and validates is reuired arguments are provided or not
71 * @param args Input Arguments
74 public Input parseInputs(String[] args) {
75 String[] modArgs = new String[args.length];
76 for (int i = 0; i < args.length; i++) {
77 if (args[i].contains("--component-spec")) {
78 modArgs[i] = "-_component_spec";
79 } else if (args[i].contains("--blueprint-path")) {
80 modArgs[i] = "-_blueprint_path";
81 } else if (args[i].contains("--blueprint-name")) {
82 modArgs[i] = "-_blueprint_name";
83 } else if (args[i].contains("--imports")) {
84 modArgs[i] = "-_imports";
85 } else if (args[i].contains("--service-name-override")) {
86 modArgs[i] = "-_service_name_override";
87 } else if (args[i].contains("--dmaap-plugin")) {
88 modArgs[i] = "-_dmaap_plugin";
94 StringBuilder commandsBuf = new StringBuilder("");
96 for (String s : modArgs) {
97 commandsBuf.append(sep).append(s);
100 String commands = commandsBuf.toString();
102 // checks if the required inputs are present or not
103 if (!(commands.contains(" -i ") || commands.contains(" -_component_spec "))
104 && !(commands.contains(" -p ") || commands.contains(" -_blueprint_path "))) {
106 "\n Please enter the ONAP Blueprint required inputs for: \n -i (The path of the ONAP Blueprint INPUT JSON SPEC FILE), \n -p (The path of the ONAP Blueprint OUTPUT where it will be saved)");
110 CommandLineParser parser = new DefaultParser();
111 HelpFormatter formatter = new HelpFormatter();
113 Options options = new Options();
115 options.addOption("i", "Spec", true, "ComponentSpec Input File of the ONAP Blueprint");
117 "_component_spec", "Spec", true, "ComponentSpec Input File of the ONAP Blueprint");
118 options.addOption("p", "Path", true, "Path of the ONAP Blueprint OUTPUT");
119 options.addOption("_blueprint_path", "Path", true, "Path of the ONAP Blueprint OUTPUT");
120 options.addOption("n", "name", true, "NAME of the ONAP Blueprint OUTPUT");
121 options.addOption("_blueprint_name", "name", true, "NAME of the ONAP Blueprint OUTPUT");
122 options.addOption("t", "Import File", true, "Import file for the OUTPUT Blueprint Imports");
124 "_imports", "Import File", true, "Import file for the OUTPUT Blueprint Imports");
127 "Service name Override",
129 "Value used to override the OUTPUT Blueprint service name");
131 "_service_name_override",
132 "Service name Override",
134 "Value used to override the OUTPUT Blueprint service name");
139 "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
144 "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
146 Input input = new Input();
148 CommandLine commandLine = parser.parse(options, modArgs);
149 input.setComponentSpecPath(
150 commandLine.getOptionValue("i") == null
151 ? commandLine.getOptionValue("_component_spec")
152 : commandLine.getOptionValue("i"));
154 commandLine.getOptionValue("p") == null
155 ? commandLine.getOptionValue("_blueprint_path")
156 : commandLine.getOptionValue("p"));
157 input.setBluePrintName(
158 commandLine.getOptionValue("n") == null
159 ? commandLine.getOptionValue("_blueprint_name")
160 : commandLine.getOptionValue("n"));
162 commandLine.getOptionValue("t") == null
163 ? commandLine.getOptionValue("_imports")
164 : commandLine.getOptionValue("t"));
166 (commands.contains(" -d ") || commands.contains(" -_dmaap_plugin ")) ? "d" : "o");
167 input.setServiceNameOverride(
168 commandLine.getOptionValue("o") == null
169 ? commandLine.getOptionValue("_service_name_override") == null
171 : commandLine.getOptionValue("_service_name_override")
172 : commandLine.getOptionValue("o"));
173 } catch (ParseException ex) {
174 ex.printStackTrace();
175 System.out.println(ex.getMessage());
177 "Required/Valid Inputs to create ONAP Blueprint are not provided", options);
180 if (StringUtils.isEmpty(input.getComponentSpecPath())) {
182 .println("The path of the ONAP Blueprint INPUT JSON SPEC FILE is not specified");
185 if (StringUtils.isEmpty(input.getOutputPath())) {
187 "The path of the ONAP Blueprint OUTPUT where it will be saved is not specified");
190 if (commands.contains(" -n ") || commands.contains(" -_blueprint_name ")) {
191 if (StringUtils.isEmpty(input.getBluePrintName())) {
193 "The NAME of the ONAP Blueprint OUTPUT that will be created is not specified");
197 if (commands.contains(" -t ") || commands.contains(" -_imports ")) {
198 if (StringUtils.isEmpty(input.getImportPath())) {
199 System.out.println("The path of the ONAP Blueprint Imports File is not specified");