BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / CommonUtils.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service.common;
25
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;
33
34 import org.springframework.stereotype.Service;
35 import org.springframework.util.StringUtils;
36
37 import static java.lang.System.exit;
38
39 /**
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
43  */
44 @Service("onapCommonUtilsService")
45 public class CommonUtils {
46
47     /**
48      * Prints input arguments options to run the Blueprint Application
49      */
50     public void printInstructions() {
51         System.out.println("OPTIONS:");
52         System.out.println(
53             "-i OR --component-spec: The path of the ONAP Blueprint INPUT JSON SPEC FILE (Required)");
54         System.out.println(
55             "-p OR --blueprint-path: The path of the ONAP Blueprint OUTPUT where it will be saved (Required)");
56         System.out.println(
57             "-n OR --blueprint-name: The NAME of the ONAP Blueprint OUTPUT that will be created (Optional)");
58         System.out
59             .println("-t OR --imports: The path of the ONAP Blueprint IMPORT FILE (Optional)");
60         System.out.println(
61             "-o OR --service-name-override: The Value used to OVERRIDE the SERVICE NAME of the ONAP Blueprint  (Optional)");
62         System.out.println(
63             "-d OR --dmaap-plugin: The option to create a ONAP Blueprint with DMAAP Plugin (Optional)");
64         System.out.println(
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");
66     }
67
68     /**
69      * Parses Input Arguments and validates is reuired arguments are provided or not
70      *
71      * @param args Input Arguments
72      * @return
73      */
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";
89             } else {
90                 modArgs[i] = args[i];
91             }
92         }
93
94         StringBuilder commandsBuf = new StringBuilder("");
95         String sep = "";
96         for (String s : modArgs) {
97             commandsBuf.append(sep).append(s);
98             sep = " ";
99         }
100         String commands = commandsBuf.toString();
101
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 "))) {
105             System.out.println(
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)");
107             exit(-1);
108         }
109
110         CommandLineParser parser = new DefaultParser();
111         HelpFormatter formatter = new HelpFormatter();
112
113         Options options = new Options();
114
115         options.addOption("i", "Spec", true, "ComponentSpec Input File of the ONAP Blueprint");
116         options.addOption(
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");
123         options.addOption(
124             "_imports", "Import File", true, "Import file for the OUTPUT Blueprint Imports");
125         options.addOption(
126             "o",
127             "Service name Override",
128             true,
129             "Value used to override the OUTPUT Blueprint service name");
130         options.addOption(
131             "_service_name_override",
132             "Service name Override",
133             true,
134             "Value used to override the OUTPUT Blueprint service name");
135         options.addOption(
136             "d",
137             "Dmaap Plugin",
138             false,
139             "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
140         options.addOption(
141             "_dmaap_plugin",
142             "Dmaap Plugin",
143             false,
144             "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
145
146         Input input = new Input();
147         try {
148             CommandLine commandLine = parser.parse(options, modArgs);
149             input.setComponentSpecPath(
150                 commandLine.getOptionValue("i") == null
151                     ? commandLine.getOptionValue("_component_spec")
152                     : commandLine.getOptionValue("i"));
153             input.setOutputPath(
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"));
161             input.setImportPath(
162                 commandLine.getOptionValue("t") == null
163                     ? commandLine.getOptionValue("_imports")
164                     : commandLine.getOptionValue("t"));
165             input.setBpType(
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
170                     ? ""
171                     : commandLine.getOptionValue("_service_name_override")
172                     : commandLine.getOptionValue("o"));
173         } catch (ParseException ex) {
174             ex.printStackTrace();
175             System.out.println(ex.getMessage());
176             formatter.printHelp(
177                 "Required/Valid Inputs to create ONAP Blueprint are not provided", options);
178             exit(-1);
179         }
180         if (StringUtils.isEmpty(input.getComponentSpecPath())) {
181             System.out
182                 .println("The path of the ONAP Blueprint INPUT JSON SPEC FILE  is not specified");
183             exit(-1);
184         }
185         if (StringUtils.isEmpty(input.getOutputPath())) {
186             System.out.println(
187                 "The path of the ONAP Blueprint OUTPUT where it will be saved is not specified");
188             exit(-1);
189         }
190         if (commands.contains(" -n ") || commands.contains(" -_blueprint_name ")) {
191             if (StringUtils.isEmpty(input.getBluePrintName())) {
192                 System.out.println(
193                     "The NAME of the ONAP Blueprint OUTPUT that will be created is not specified");
194                 exit(-1);
195             }
196         }
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");
200                 exit(-1);
201             }
202         }
203
204         return input;
205     }
206 }