Make Http as separate plugin
[cli.git] / main / src / main / java / org / onap / cli / main / utils / OnapCliArgsParser.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.main.utils;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.commons.io.FileUtils;
29 import org.onap.cli.fw.error.OnapCommandException;
30 import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
31 import org.onap.cli.fw.input.OnapCommandParameter;
32 import org.onap.cli.fw.input.OnapCommandParameterType;
33 import org.onap.cli.main.error.OnapCliArgumentValueMissing;
34 import org.onap.cli.main.error.OnapCliInvalidArgument;
35
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 import net.minidev.json.JSONObject;
40
41 /**
42  * Oclip CLI utilities.
43  *
44  */
45 public class OnapCliArgsParser {
46
47     /**
48      * private Constructor.
49      */
50     private OnapCliArgsParser() {
51
52     }
53
54     /**
55      * It read thru the args and populate the given params for short optional, long option and postional args the idx of
56      * positional args, is calculated based on the position at which it present in the params and args.
57      *
58      * @param params
59      *            List of command paramters
60      * @param args
61      *            Array of arguments
62      * @throws OnapCliArgumentValueMissing
63      *             ArgumentValueMissing exception
64      * @throws OnapCliInvalidArgument
65      *             Invalid argument exception
66      * @throws OnapCommandInvalidParameterValue
67      *             exception
68      */
69     public static void populateParams(Set<OnapCommandParameter> params, List<String> args)
70             throws OnapCommandException {
71         Map<String, String> shortOptionMap = new HashMap<>();
72         Map<String, String> longOptionMap = new HashMap<>();
73         List<String> positionArgs = new ArrayList<>();
74         Map<String, OnapCommandParameter> paramMap = new HashMap<>();
75
76         for (OnapCommandParameter param : params) {
77             boolean positional = true;
78             if (param.getShortOption() != null) {
79                 shortOptionMap.put(OnapCommandParameter.printShortOption(param.getShortOption()), param.getName());
80                 positional = false;
81             }
82             if (param.getLongOption() != null) {
83                 longOptionMap.put(OnapCommandParameter.printLongOption(param.getLongOption()), param.getName());
84                 positional = false;
85             }
86
87             if (positional) {
88                 positionArgs.add(param.getName());
89             }
90
91             paramMap.put(param.getName(), param);
92         }
93
94         int positionalIdx = 0;
95         // Skip the first args oclip cmd name, so start from 1
96         for (int i = 1; i < args.size(); i++) {
97             String paramName = null;
98             if (shortOptionMap.containsKey(args.get(i))) {
99                 paramName = shortOptionMap.get(args.get(i));
100             } else if (longOptionMap.containsKey(args.get(i))) {
101                 paramName = longOptionMap.get(args.get(i));
102             }
103
104             if (paramName != null) {
105                 // end of the list or if its option rather than a value
106                 if ((i + 1) == args.size() || args.get(i + 1).startsWith("-")) {
107                     if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.BOOL)) {
108                         paramMap.get(paramName).setValue("true");
109                         continue;
110                     }
111                     throw new OnapCliArgumentValueMissing(args.get(i));
112                 }
113
114                 if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.JSON)) {
115                     paramMap.get(paramName).setValue(readJsonStringFromUrl(args.get(i + 1),
116                             paramMap.get(paramName).getName()));
117                     i++;
118                     continue;
119                 } if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.TEXT)) {
120                     paramMap.get(paramName).setValue(readTextStringFromUrl(args.get(i + 1),
121                             paramMap.get(paramName).getName()));
122                     i++;
123                     continue;
124                 } else if (paramMap.get(paramName).getParameterType()
125                         .equals(OnapCommandParameterType.ARRAY)) {
126                     Object value = paramMap.get(paramName).getValue();
127                     List<String> list;
128                     if (value == "") {
129                         list = new ArrayList<>();
130                     } else {
131                         list = convertJsonToListString(paramMap.get(paramName).getName(),
132                                 value.toString());
133                     }
134                     list.add(args.get(i + 1));
135                     paramMap.get(paramName).setValue(list);
136                     i++;
137                     continue;
138                 } else if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.MAP)) {
139                     Object value = paramMap.get(paramName).getValue();
140
141                     Map<String, String> map;
142
143                     if (value == "") {
144                         map = new HashMap<>();
145                     } else {
146                         map = convertJsonToMapString(paramMap.get(paramName).getName(),
147                                 value.toString());
148                     }
149
150                     String arg = args.get(i + 1);
151                     String[] argArr = arg.split("=");
152
153                     if (argArr.length != 2) {
154                         throw new OnapCliInvalidArgument(paramMap.get(paramName).getName());
155                     }
156
157                     map.put(argArr[0], argArr[1]);
158                     paramMap.get(paramName).setValue(map);
159                     i++;
160                     continue;
161                 }
162
163                 paramMap.get(paramName).setValue(args.get(i + 1));
164
165                 i++;
166                 continue;
167             }
168
169             // it is positional option
170             // Positional arg is missing from the params
171             if (positionalIdx >= positionArgs.size()) {
172                 throw new OnapCliInvalidArgument(args.get(i));
173             }
174
175             paramMap.get(positionArgs.get(positionalIdx)).setValue(args.get(i));
176             positionalIdx++;
177         }
178
179         params.clear();
180         params.addAll(paramMap.values());
181     }
182
183     private static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument {
184         ObjectMapper mapper = new ObjectMapper();
185         try {
186             File file = new File(input);
187             if (file.isFile()) {
188                 return mapper.readValue(file, JSONObject.class).toJSONString();
189             } else if (input.startsWith("file:") || input.startsWith("http:") || input.startsWith("ftp:")) {
190                 URL jsonUrl = new URL(input);
191                 return mapper.readValue(jsonUrl, JSONObject.class).toJSONString();
192             } else {
193                 return mapper.readValue(input, JSONObject.class).toJSONString();
194             }
195         } catch (IOException e) {
196             throw new OnapCliInvalidArgument(argName, e);
197         }
198     }
199
200     private static String readTextStringFromUrl(String input, String argName) throws OnapCliInvalidArgument {
201         try {
202             File file = new File(input);
203             if (file.isFile()) {
204                 return FileUtils.readFileToString(file);
205             } else {
206                 return input;
207             }
208
209         } catch (IOException e) {
210             throw new OnapCliInvalidArgument(argName, e);
211         }
212     }
213
214     private static List<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument {
215         TypeReference<List<String>> mapType = new TypeReference<List<String>>() {
216         };
217         try {
218             return new ObjectMapper().readValue(json, mapType);
219         } catch (IOException e) {
220             throw new OnapCliInvalidArgument(arg, e);
221         }
222     }
223
224     private static Map<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument {
225         TypeReference<Map<String, String>> mapType = new TypeReference<Map<String, String>>() {
226         };
227         try {
228             return new ObjectMapper().readValue(json, mapType);
229         } catch (IOException e) {
230             throw new OnapCliInvalidArgument(arg, e);
231         }
232     }
233 }