Merge "Blueprint Generator Refactored Code Issue-ID: DCAEGEN2-2472"
[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
27
28 import org.apache.commons.cli.CommandLineParser;
29 import org.apache.commons.cli.HelpFormatter;
30 import org.apache.commons.cli.ParseException;
31 import org.apache.commons.cli.DefaultParser;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.CommandLine;
34 import org.onap.blueprintgenerator.model.common.Input;
35
36 import org.springframework.stereotype.Service;
37 import org.springframework.util.StringUtils;
38
39 import static java.lang.System.exit;
40
41 /**
42  * @author : Ravi Mantena
43  * @date 10/16/2020
44  * Application: ONAP - Blueprint Generator
45  * Common ONAP Service used by ONAP and DMAAP Blueprint to Print Instructions and Parse Inputs
46  */
47
48
49 @Service("onapCommonUtilsService")
50 public class CommonUtils {
51
52     public void printInstructions() {
53         System.out.println("OPTIONS:");
54         System.out.println("-i OR --component-spec: The path of the ONAP Blueprint INPUT JSON SPEC FILE (Required)");
55         System.out.println("-p OR --blueprint-path: The path of the ONAP Blueprint OUTPUT where it will be saved (Required)");
56         System.out.println("-n OR --blueprint-name: The NAME of the ONAP Blueprint OUTPUT that will be created (Optional)");
57         System.out.println("-t OR --imports: The path of the ONAP Blueprint IMPORT FILE (Optional)");
58         System.out.println("-o OR --service-name-override: The Value used to OVERRIDE the SERVICE NAME of the ONAP Blueprint  (Optional)");
59         System.out.println("-d OR --dmaap-plugin: The option to create a ONAP Blueprint with DMAAP Plugin (Optional)");
60         System.out.println("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");
61     }
62
63     public Input parseInputs(String[] args) {
64         String[] modArgs = new String[args.length];
65         for(int i=0; i<args.length; i++){
66             if(args[i].contains("--component-spec"))
67                 modArgs[i] = "-_component_spec";
68             else if(args[i].contains("--blueprint-path"))
69                 modArgs[i] = "-_blueprint_path";
70             else if(args[i].contains("--blueprint-name"))
71                 modArgs[i] = "-_blueprint_name";
72             else if(args[i].contains("--imports"))
73                 modArgs[i] = "-_imports";
74             else if(args[i].contains("--service-name-override"))
75                 modArgs[i] = "-_service_name_override";
76             else if(args[i].contains("--dmaap-plugin"))
77                 modArgs[i] = "-_dmaap_plugin";
78             else
79                 modArgs[i] = args[i];
80         }
81         String commands = " ";
82         for (String s : modArgs) {
83
84                 commands = commands + " " + s;
85         }
86
87         //checks if the required inputs are present or not
88         if (!(commands.contains(" -i ")|| commands.contains(" -_component_spec "))
89                 && !(commands.contains(" -p ") || commands.contains(" -_blueprint_path ") ) ) {
90             System.out.println("\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)");
91             exit(-1);
92         }
93
94
95         CommandLineParser parser = new DefaultParser();
96         HelpFormatter formatter = new HelpFormatter();
97
98         Options options = new Options();
99
100         options.addOption("i", "Spec", true, "ComponentSpec Input File of the ONAP Blueprint");
101         options.addOption("_component_spec", "Spec", true, "ComponentSpec Input File of the ONAP Blueprint");
102         options.addOption("p", "Path", true, "Path of the ONAP Blueprint OUTPUT");
103         options.addOption("_blueprint_path", "Path", true, "Path of the ONAP Blueprint OUTPUT");
104         options.addOption("n", "name", true, "NAME of the ONAP Blueprint OUTPUT");
105         options.addOption("_blueprint_name", "name", true, "NAME of the ONAP Blueprint OUTPUT");
106         options.addOption("t", "Import File", true, "Import file for the OUTPUT Blueprint Imports");
107         options.addOption("_imports", "Import File", true, "Import file for the OUTPUT Blueprint Imports");
108         options.addOption("o", "Service name Override", true, "Value used to override the OUTPUT Blueprint service name");
109         options.addOption("_service_name_override", "Service name Override", true, "Value used to override the OUTPUT Blueprint service name");
110         options.addOption("d", "Dmaap Plugin", false, "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
111         options.addOption("_dmaap_plugin", "Dmaap Plugin", false, "Flag used to indicate ONAP Blueprint OUTPUT uses the DMaaP plugin");
112
113         Input input = new Input();
114         try {
115             CommandLine commandLine = parser.parse(options, modArgs);
116             input.setComponentSpecPath(commandLine.getOptionValue("i") == null ? commandLine.getOptionValue("_component_spec") : commandLine.getOptionValue("i"));
117             input.setOutputPath(commandLine.getOptionValue("p") == null ? commandLine.getOptionValue("_blueprint_path") : commandLine.getOptionValue("p"));
118             input.setBluePrintName(commandLine.getOptionValue("n") == null ? commandLine.getOptionValue("_blueprint_name") : commandLine.getOptionValue("n"));
119             input.setImportPath(commandLine.getOptionValue("t")  == null ? commandLine.getOptionValue("_imports") : commandLine.getOptionValue("t"));
120             input.setBpType((commands.contains(" -d ") || commands.contains(" -_dmaap_plugin ") ) ? "d" : "o");
121             input.setServiceNameOverride(commandLine.getOptionValue("o") == null ? commandLine.getOptionValue("_service_name_override") ==  null ? "" : commandLine.getOptionValue("_service_name_override") : commandLine.getOptionValue("o"));
122         } catch (ParseException ex) {
123             ex.printStackTrace();
124             System.out.println(ex.getMessage());
125             formatter.printHelp("Required/Valid Inputs to create ONAP Blueprint are not provided", options);
126             exit(-1);
127         }
128         if (StringUtils.isEmpty(input.getComponentSpecPath())) {
129             System.out.println("The path of the ONAP Blueprint INPUT JSON SPEC FILE  is not specified");
130             exit(-1);
131         }
132         if (StringUtils.isEmpty(input.getOutputPath())) {
133             System.out.println("The path of the ONAP Blueprint OUTPUT where it will be saved is not specified");
134             exit(-1);
135         }
136         if (commands.contains(" -n ") || commands.contains(" -_blueprint_name ")) {
137             if (StringUtils.isEmpty(input.getBluePrintName())) {
138                 System.out.println("The NAME of the ONAP Blueprint OUTPUT that will be created is not specified");
139                 exit(-1);
140             }
141         }
142         if (commands.contains(" -t ")|| commands.contains(" -_imports ")) {
143             if (StringUtils.isEmpty(input.getImportPath())) {
144                 System.out.println("The path of the ONAP Blueprint Imports File is not specified");
145                 exit(-1);
146             }
147         }
148
149         return input;
150     }
151
152 }