1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright 2017 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.cli.fw.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.onap.cli.fw.cmd.OnapCommand;
import org.onap.cli.fw.conf.OnapCommandConfig;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.*;
import org.onap.cli.fw.registrar.OnapCommandRegistrar;
import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import static org.onap.cli.fw.conf.OnapCommandConstants.*;
public class OnapCommandDiscoveryUtils {
/**
* Fetch a particular schema details.
*
* @param cmd
* command name
* @return ExternalSchema obj
* @throws OnapCommandInvalidSchema
* exception
* @throws OnapCommandDiscoveryFailed
* exception
*/
public static OnapCommandSchemaInfo getSchemaInfo(String cmd, String version) throws OnapCommandException {
List<OnapCommandSchemaInfo> list = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(false);
OnapCommandSchemaInfo schemaInfo = null;
if (list != null) {
for (OnapCommandSchemaInfo schema : list) {
if (cmd.equals(schema.getCmdName()) && version.equals(schema.getProduct())) {
schemaInfo = schema;
break;
}
}
}
return schemaInfo;
}
/**
* Load the previous discovered json file.
*
* @return list
* @throws OnapCommandInvalidSchema
* exception
* @throws OnapCommandDiscoveryFailed
* exception
*/
public static List<OnapCommandSchemaInfo> discoverOrLoadSchemas(boolean forceRefresh) throws OnapCommandException {
List<OnapCommandSchemaInfo> schemas = new ArrayList<>();
if (forceRefresh || Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.DISCOVER_ALWAYS))
|| !OnapCommandDiscoveryUtils.isAlreadyDiscovered()) {
schemas = OnapCommandDiscoveryUtils.discoverSchemas();
if (!schemas.isEmpty()) {
OnapCommandDiscoveryUtils.persistSchemaInfo(schemas);
}
} else {
try {
Resource resource = OnapCommandDiscoveryUtils.findResource(DISCOVERY_FILE,
DATA_PATH_JSON_PATTERN);
if (resource != null) {
File file = new File(resource.getURI().getPath());
ObjectMapper mapper = new ObjectMapper();
OnapCommandSchemaInfo[] list = mapper.readValue(file, OnapCommandSchemaInfo[].class);
schemas.addAll(Arrays.asList(list));
}
} catch (IOException e) {
throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY,
DISCOVERY_FILE, e);
}
}
return schemas;
}
/**
* Check if json file discovered or not.
*
* @return boolean
* @throws OnapCommandDiscoveryFailed
* exception
*/
public static boolean isAlreadyDiscovered() throws OnapCommandDiscoveryFailed {
Resource resource = null;
try {
resource = OnapCommandDiscoveryUtils.findResource(DISCOVERY_FILE,
DATA_PATH_JSON_PATTERN);
if (resource != null) {
return true;
}
} catch (IOException e) {
throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY,
DISCOVERY_FILE, e);
}
return false;
}
/**
* Persist the external schema details.
*
* @param schemas
* list
* @throws OnapCommandDiscoveryFailed
* exception
*/
public static void persistSchemaInfo(List<OnapCommandSchemaInfo> schemas) throws OnapCommandDiscoveryFailed {
if (schemas != null) {
try {
Resource[] resources = OnapCommandDiscoveryUtils.findResources(DATA_DIRECTORY);
if (resources != null && resources.length == 1) {
String path = resources[0].getURI().getPath();
File file = new File(path + File.separator + DISCOVERY_FILE);
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(file, schemas);
}
} catch (IOException e1) {
throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY,
DISCOVERY_FILE, e1);
}
}
}
/**
* Get schema map.
*
* @param resource
* resource obj
* @return map
* @throws OnapCommandInvalidSchema
* exception
*/
public static Map<String, ?> loadSchema(Resource resource) throws OnapCommandInvalidSchema {
Map<String, ?> values = null;
try {
values = (Map<String, ?>) new Yaml().load(resource.getInputStream());
} catch (Exception e) {
throw new OnapCommandInvalidSchema(resource.getFilename(), e);
}
return values;
}
/**
* Returns a resource available under certain directory in class-path.
*
* @param pattern
* search pattern
* @return found resource
* @throws IOException
* exception
*/
public static Resource findResource(String fileName, String pattern) throws IOException {
Resource[] resources = OnapCommandDiscoveryUtils.findResources(pattern);
if (resources != null && resources.length > 0) {
for (Resource res : resources) {
if (res.getFilename().equals(fileName)) {
return res;
}
}
}
return null;
}
/**
* Returns all resources available under certain directory in class-path.
*
* @param pattern
* search pattern
* @return resources found resources
* @throws IOException
* exception
*/
public static Resource[] findResources(String pattern) throws IOException {
ClassLoader cl = OnapCommandUtils.class.getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
return resolver.getResources("classpath*:" + pattern);
}
public static String identitySchemaProfileType(Map<String, ?> schemaYamlMap) {
for (String schemeType : OnapCommandConfig.getCommaSeparatedList(OnapCommandConstants.SCHEMA_TYPES_SUPPORTED)) {
if (schemaYamlMap.get(schemeType) != null) {
return schemeType;
}
}
return OnapCommandConstants.BASIC_SCHEMA_PROFILE;
}
/**
* Find external schema files.
*
* @return list ExternalSchema
* @throws OnapCommandDiscoveryFailed
* exception
* @throws OnapCommandInvalidSchema
* exception
*/
public static List<OnapCommandSchemaInfo> discoverSchemas() throws OnapCommandException {
List<OnapCommandSchemaInfo> extSchemas = new ArrayList<>();
try {
Resource[] res = findResources(SCHEMA_PATH_PATERN);
if (res != null && res.length > 0) {
Map<String, ?> resourceMap;
for (Resource resource : res) {
try {
resourceMap = loadYaml(resource);
} catch (OnapCommandException e) {
OnapCommandUtils.LOG.error("Ignores invalid schema " + resource.getURI().toString(), e);
continue;
}
if (resourceMap != null && resourceMap.size() > 0) {
OnapCommandSchemaInfo schema = new OnapCommandSchemaInfo();
schema.setSchemaURI(resource.getURI().toString());
Object obj = resourceMap.get(OPEN_CLI_SCHEMA_VERSION);
if (obj == null) {
OnapCommandUtils.LOG.info("Invalid Schema yaml " + schema.getSchemaURI());
continue;
}
schema.setVersion(obj.toString());
if (!schema.getVersion().equalsIgnoreCase(OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0)) {
OnapCommandUtils.LOG.info("Unsupported Schema version found " + schema.getSchemaURI());
continue;
}
//There are schema like default input parameters and does not have command name
if (resourceMap.get(NAME) == null) {
continue;
}
schema.setSchemaName(resource.getFilename());
schema.setCmdName((String) resourceMap.get(NAME));
Map<String, ?> infoMap = (Map<String, ?>) resourceMap.get(OnapCommandConstants.INFO);
if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_TYPE) != null) {
schema.setType(infoMap.get(OnapCommandConstants.INFO_TYPE).toString());
}
if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_PRODUCT) != null) {
schema.setProduct(infoMap.get(OnapCommandConstants.INFO_PRODUCT).toString());
}
if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_IGNORE) != null) {
schema.setIgnore(infoMap.get(OnapCommandConstants.INFO_IGNORE).toString());
}
schema.setSchemaProfile(identitySchemaProfileType(resourceMap));
extSchemas.add(schema);
}
}
}
} catch (IOException e) {
throw new OnapCommandDiscoveryFailed(SCHEMA_DIRECTORY, e);
}
try {
Resource[] samples = findResources(OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN);
for (Resource sample : samples) {
updateSchemaInfoWithSample(sample, extSchemas);
}
} catch (IOException e) {
throw new OnapCommandDiscoveryFailed(OnapCommandConstants.VERIFY_SAMPLES_DIRECTORY, e);
}
return extSchemas;
}
private static void updateSchemaInfoWithSample(Resource sampleResourse,
List<OnapCommandSchemaInfo> schemaInfos) throws OnapCommandInvalidSchema, IOException {
Map<String, ?> infoMap = loadSchema(sampleResourse);
if (infoMap == null) {
return;
}
Object sampleVersion = infoMap.get(OPEN_CLI_SAMPLE_VERSION);
if (sampleVersion == null) {
OnapCommandUtils.LOG.info("Invalid Sample yaml " + sampleResourse.getURI().toString());
return;
}
if (!sampleVersion.toString().equalsIgnoreCase(OnapCommandConstants.OPEN_CLI_SAMPLE_VERSION_VALUE_1_0)) {
OnapCommandUtils.LOG.info("Unsupported Sample version found " + sampleResourse.getURI().toString());
return;
}
String cmdName = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_NAME);
String version = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_VERSION);
Optional<OnapCommandSchemaInfo> optSchemaInfo = schemaInfos.stream()
.filter(e -> e.getCmdName().equals(cmdName) && e.getProduct().equals(version))
.findFirst();
if (optSchemaInfo.isPresent()) {
OnapCommandSchemaInfo onapCommandSchemaInfo = optSchemaInfo.get();
onapCommandSchemaInfo.getSampleFiles().add(sampleResourse.getFilename());
}
}
/**
* Discover the Oclip commands.
*
* @return list
*/
public static List<Class<OnapCommand>> discoverCommandPlugins() {
ServiceLoader<OnapCommand> loader = ServiceLoader.load(OnapCommand.class);
List<Class<OnapCommand>> clss = new ArrayList<>();
for (OnapCommand implClass : loader) {
clss.add((Class<OnapCommand>) implClass.getClass());
}
return clss;
}
/**
* Instantiate command plugin
* @throws OnapCommandInstantiationFailed
*/
public static OnapCommand loadCommandClass(Class <? extends OnapCommand> cls) throws OnapCommandInstantiationFailed {
try {
Constructor<?> constr = cls.getConstructor();
return (OnapCommand) constr.newInstance();
} catch (NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new OnapCommandInstantiationFailed(cls.getName(), e);
}
}
public static List<Map<String, ?>> createTestSuite(String cmd, String version) throws OnapCommandException {
ArrayList<Map<String, ?>> testSamples = new ArrayList();
List<Resource> resources = new ArrayList();
OnapCommandSchemaInfo schemaInfo = getSchemaInfo(cmd, version);
List<String> sampleFiles = new ArrayList();
if (schemaInfo != null && !schemaInfo.getSampleFiles().isEmpty()) {
sampleFiles.addAll(schemaInfo.getSampleFiles());
}
for (String sampleFile : sampleFiles) {
try {
Resource resource = OnapCommandDiscoveryUtils.findResource(sampleFile,
OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN);
resources.add(resource);
} catch (IOException e) {
throw new OnapCommandInvalidSample("Sample file does not exist : " + sampleFile , e);
}
}
for (Resource resource : resources) {
Map<String, ?> stringMap = OnapCommandDiscoveryUtils.loadYaml(resource);
Map<String, Map<String, String>> samples = (Map<String, Map<String, String>>) stringMap
.get(OnapCommandConstants.VERIFY_SAMPLES);
for (String sampleId : samples.keySet()) {
Map<String, String> sample = samples.get(sampleId);
List<String> inputArgs = new ArrayList();
inputArgs.add(cmd);
if (sample.get(OnapCommandConstants.VERIFY_INPUT) != null) {
inputArgs.addAll(Arrays.asList(sample.get(OnapCommandConstants.VERIFY_INPUT).trim().split(" ")));
}
inputArgs.add(OnapCommandConstants.VERIFY_LONG_OPTION);
HashMap map = new HashMap();
map.put(OnapCommandConstants.VERIFY_INPUT, inputArgs);
map.put(OnapCommandConstants.VERIFY_OUPUT, sample.get(OnapCommandConstants.VERIFY_OUPUT));
map.put(OnapCommandConstants.VERIFY_MOCO, sample.get(OnapCommandConstants.VERIFY_MOCO));
map.put(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID, resource.getFilename());
map.put(OnapCommandConstants.VERIFY_SAMPLE_ID, sampleId);
testSamples.add(map);
}
}
return testSamples;
}
/**
* Get schema map.
*
* @param resource
* resource obj
* @return map
* @throws OnapCommandInvalidSchema
* exception
*/
public static Map<String, ?> loadYaml(Resource resource) throws OnapCommandInvalidSchema {
Map<String, ?> values = null;
try {
values = (Map<String, ?>) new Yaml().load(resource.getInputStream());
} catch (Exception e) {
throw new OnapCommandInvalidSchema(resource.getFilename(), e);
}
return values;
}
/**
* Get schema map.
*
* @param filePath
* @return map
* @throws OnapCommandInvalidSchema
* exception
*/
public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema {
Map<String, ?> values = null;
try {
values = (Map<String, Object>) new Yaml().load(FileUtils.readFileToString(new File(filePath)));
} catch (Exception e) {
throw new OnapCommandInvalidSchema(filePath, e);
}
return values;
}
}
|