53cea2b617bfcaadbf6f7736d98a722eb9b286b8
[cli.git] / framework / src / main / java / org / onap / cli / fw / utils / OnapCommandDiscoveryUtils.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.fw.utils;
18
19 import static org.onap.cli.fw.conf.OnapCommandConstants.ATTRIBUTES;
20 import static org.onap.cli.fw.conf.OnapCommandConstants.DEAFULT_INPUT_PARAMETERS_NAME;
21 import static org.onap.cli.fw.conf.OnapCommandConstants.DEFAULT_SCHEMA_PATH_PATERN;
22 import static org.onap.cli.fw.conf.OnapCommandConstants.DESCRIPTION;
23 import static org.onap.cli.fw.conf.OnapCommandConstants.DISCOVERY_FILE;
24 import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_PARAM;
25 import static org.onap.cli.fw.conf.OnapCommandConstants.NAME;
26 import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SAMPLE_VERSION;
27 import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION;
28 import static org.onap.cli.fw.conf.OnapCommandConstants.PARAMETERS;
29 import static org.onap.cli.fw.conf.OnapCommandConstants.RESULTS;
30 import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_DIRECTORY;
31 import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_PATH_PATERN;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.lang.reflect.Constructor;
36 import java.lang.reflect.InvocationTargetException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Optional;
43 import java.util.ServiceLoader;
44
45 import org.apache.commons.io.FileUtils;
46 import org.onap.cli.fw.cmd.OnapCommand;
47 import org.onap.cli.fw.conf.OnapCommandConfig;
48 import org.onap.cli.fw.conf.OnapCommandConstants;
49 import org.onap.cli.fw.error.OnapCommandDiscoveryFailed;
50 import org.onap.cli.fw.error.OnapCommandException;
51 import org.onap.cli.fw.error.OnapCommandInstantiationFailed;
52 import org.onap.cli.fw.error.OnapCommandInvalidSample;
53 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
54 import org.onap.cli.fw.error.OnapCommandNotFound;
55 import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
56 import org.springframework.core.io.Resource;
57 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
58 import org.springframework.core.io.support.ResourcePatternResolver;
59 import com.esotericsoftware.yamlbeans.YamlReader;
60 import com.google.gson.Gson;
61 import com.google.gson.GsonBuilder;
62 import com.google.gson.stream.JsonReader;
63 import java.io.FileReader;
64 import java.io.Writer;
65 import java.io.FileWriter;
66 import java.io.InputStream;
67 import java.io.InputStreamReader;
68 import java.io.FileInputStream;
69
70 public class OnapCommandDiscoveryUtils {
71     private OnapCommandDiscoveryUtils() {
72         throw new IllegalStateException("Utility class");
73     }
74
75     private static Gson gson = new GsonBuilder().serializeNulls().create();
76
77     /**
78      * Fetch a particular schema details.
79      *
80      * @param cmd
81      *            command name
82      * @return ExternalSchema obj
83      * @throws OnapCommandInvalidSchema
84      *             exception
85      * @throws OnapCommandDiscoveryFailed
86      *             exception
87      */
88     public static OnapCommandSchemaInfo getSchemaInfo(String cmd, String version) throws OnapCommandException {
89         List<OnapCommandSchemaInfo> list = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(false);
90         OnapCommandSchemaInfo schemaInfo = null;
91         if (list != null) { //NOSONAR
92             for (OnapCommandSchemaInfo schema : list) {
93                 if (cmd.equals(schema.getCmdName()) && version.equals(schema.getProduct())) {
94                     schemaInfo = schema;
95                     break;
96                 }
97             }
98         }
99
100         if (schemaInfo == null)
101              throw new OnapCommandNotFound(cmd, version);
102
103         return schemaInfo;
104    }
105
106     /**
107     * Load the previous discovered json file.
108      *
109      * @return list
110      * @throws OnapCommandInvalidSchema
111      *             exception
112      * @throws OnapCommandDiscoveryFailed
113      *             exception
114      */
115     public static List<OnapCommandSchemaInfo> discoverOrLoadSchemas(boolean forceRefresh) throws OnapCommandException {
116         List<OnapCommandSchemaInfo> schemas = new ArrayList<>(); //NOSONAR
117         if (forceRefresh || Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.DISCOVER_ALWAYS))
118                 || !OnapCommandDiscoveryUtils.isAlreadyDiscovered()) {
119             schemas = OnapCommandDiscoveryUtils.discoverSchemas();
120             if (!schemas.isEmpty()) {
121                 //merge the existing RPC schema with discovered ones
122                 List<OnapCommandSchemaInfo> schemasExisting = OnapCommandDiscoveryUtils.loadSchemas();
123
124                 Map<String, OnapCommandSchemaInfo> rpcCommands = new HashMap<>();
125                 for (OnapCommandSchemaInfo info: schemasExisting) {
126                     if (info.isRpc()) {
127                         rpcCommands.put(info.getProduct() + ":" + info.getCmdName(), info);
128                     }
129                 }
130
131                 //mrkanag: Enable clustering for keeping command in more than one OCLIP engine
132                 //Remove if already an same command exists with RPC
133                 for (OnapCommandSchemaInfo info: schemas) {
134                     OnapCommandSchemaInfo infoExisting = rpcCommands.get(info.getProduct() + ":" + info.getCmdName());
135                     if (infoExisting != null) {
136                         rpcCommands.remove(info.getProduct() + ":" + info.getCmdName());
137                     }
138                 }
139
140                 //Add all RPC ones
141                 schemas.addAll(rpcCommands.values());
142
143                 OnapCommandDiscoveryUtils.persistSchemaInfo(schemas);
144             }
145         } else {
146             schemas = OnapCommandDiscoveryUtils.loadSchemas();
147         }
148
149         return schemas;
150     }
151
152     public static String getDataStorePath() {
153         return OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_DATA_DIR);
154     }
155
156     public static List<OnapCommandSchemaInfo> loadSchemas() throws OnapCommandException {
157         List<OnapCommandSchemaInfo> schemas = new ArrayList<>();
158
159         if (!OnapCommandDiscoveryUtils.isAlreadyDiscovered()) return schemas;
160
161         String dataDir = OnapCommandDiscoveryUtils.getDataStorePath();
162         File file = new File(dataDir + File.separator + DISCOVERY_FILE);
163         try (JsonReader jsonReader = new JsonReader(new FileReader(file))){
164             OnapCommandSchemaInfo[] list = gson.fromJson(jsonReader, OnapCommandSchemaInfo[].class);
165             schemas.addAll(Arrays.asList(list));
166         } catch (Exception e) { // NOSONAR
167             throw new OnapCommandDiscoveryFailed(dataDir,
168                     DISCOVERY_FILE, e);
169         }
170
171         return schemas;
172     }
173
174     /**
175      * Check if json file discovered or not.
176      *
177      * @return boolean
178      * @throws OnapCommandDiscoveryFailed
179      *             exception
180      */
181     public static boolean isAlreadyDiscovered() throws OnapCommandDiscoveryFailed { //NOSONAR
182         String dataDir = OnapCommandDiscoveryUtils.getDataStorePath();
183         return new File(dataDir + File.separator + DISCOVERY_FILE).exists();
184     }
185
186     /**
187      * Persist the external schema details.
188      *
189      * @param schemas
190      *            list
191      * @throws OnapCommandDiscoveryFailed
192      *             exception
193      */
194     public static void persistSchemaInfo(List<OnapCommandSchemaInfo> schemas) throws OnapCommandDiscoveryFailed {
195         if (schemas != null) {
196             String dataDir = OnapCommandDiscoveryUtils.getDataStorePath();
197
198             try {
199                 FileUtils.forceMkdir(new File(dataDir));
200
201                 File file = new File(dataDir + File.separator + DISCOVERY_FILE);
202                 try(Writer writer = new FileWriter(file)){
203                     gson.toJson(schemas,writer);
204                 }
205             } catch (Exception e1) { // NOSONAR
206                 throw new OnapCommandDiscoveryFailed(dataDir,
207                         DISCOVERY_FILE, e1);
208             }
209         }
210     }
211
212     /**
213      * Get schema map.
214      *
215      * @param resource
216      *            resource obj
217      * @return map
218      * @throws OnapCommandInvalidSchema
219      *             exception
220      */
221     public static Map<String, Object> loadSchema(Resource resource) throws OnapCommandInvalidSchema {
222         return loadYaml(resource);
223     }
224
225     /**
226      * Returns a resource available under certain directory in class-path.
227      *
228      * @param pattern
229      *            search pattern
230      * @return found resource
231      * @throws IOException
232      *             exception
233      */
234     public static Resource findResource(String fileName, String pattern) throws IOException {
235         Resource[] resources = OnapCommandDiscoveryUtils.findResources(pattern);
236         if (resources != null && resources.length > 0) { //NOSONAR
237             for (Resource res : resources) {
238                 if (res.getFilename().equals(fileName)) {
239                     return res;
240                 }
241             }
242         }
243
244         return null;
245     }
246
247     /**
248      * Returns all resources available under certain directory in class-path.
249      *
250      * @param pattern
251      *            search pattern
252      * @return resources found resources
253      * @throws IOException
254      *             exception
255      */
256     public static Resource[] findResources(String pattern) throws IOException {
257         ClassLoader cl = OnapCommandUtils.class.getClassLoader();
258         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
259         return resolver.getResources("classpath*:" + pattern);
260     }
261
262     public static String identitySchemaProfileType(Map<String, ?> schemaYamlMap) {
263
264         for (String schemeType : OnapCommandConfig.getCommaSeparatedList(OnapCommandConstants.SCHEMA_TYPES_SUPPORTED)) {
265             if (schemaYamlMap.get(schemeType) != null) {
266                 return schemeType;
267             }
268         }
269
270         return OnapCommandConstants.BASIC_SCHEMA_PROFILE;
271     }
272
273     /**
274      * Find external schema files.
275      *
276      * @return list ExternalSchema
277      * @throws OnapCommandDiscoveryFailed
278      *             exception
279      * @throws OnapCommandInvalidSchema
280      *             exception
281      */
282     public static List<OnapCommandSchemaInfo> discoverSchemas() throws OnapCommandException {
283         List<OnapCommandSchemaInfo> extSchemas = new ArrayList<>();
284         try {
285             //collect default input parameters for every profile
286             Resource[] deafultRres = findResources(DEFAULT_SCHEMA_PATH_PATERN);
287             Map <String, List<Object>> defaultInputs = new HashMap<>();
288
289             if (deafultRres != null && deafultRres.length > 0) {
290                 Map<String, ?> deafultResourceMap;
291
292                 for (Resource resource : deafultRres) {
293                     deafultResourceMap = loadYaml(resource, true);
294
295                     if (deafultResourceMap != null && deafultResourceMap.size() > 0) {
296                         //default_input_parameters_http.yaml
297                         String profileName = resource.getFilename().substring(
298                                 DEAFULT_INPUT_PARAMETERS_NAME.length() + 1,
299                                 resource.getFilename().indexOf('.'));
300                         if (deafultResourceMap.containsKey(PARAMETERS)) {
301                             List<Object> params = new ArrayList<>();
302                             for (Map<String, ?> p: (List<Map<String, ?>>) deafultResourceMap.get(PARAMETERS)) {
303                                 if (p.keySet().contains(IS_DEFAULT_PARAM) && ! (Boolean.getBoolean(String.valueOf(p.get(IS_DEFAULT_PARAM))))) {
304                                     params.add(p);
305                                 }
306                             }
307
308                             defaultInputs.put(profileName, params);
309                         }
310                     }
311                 }
312             }
313
314             Resource[] res = findResources(SCHEMA_PATH_PATERN);
315             if (res != null && res.length > 0) {
316                 for (Resource resource : res) {
317                     Map<String, ?> resourceMap;
318                     try { //NOSONAR
319                         resourceMap = loadYaml(resource);
320                     } catch (OnapCommandException e) {
321                         String resourceURI = resource.getURI().toString();
322                         OnapCommandUtils.log.error("Ignores invalid schema {} {}", resourceURI, e);
323                         continue;
324                     }
325
326                     if (resourceMap != null && resourceMap.size() > 0) {
327                         OnapCommandSchemaInfo schema = new OnapCommandSchemaInfo();
328
329                         schema.setSchemaURI(resource.getURI().toString());
330
331                         Object obj = resourceMap.get(OPEN_CLI_SCHEMA_VERSION);
332                         if (obj == null) {
333                             String schemaURI = schema.getSchemaURI();
334                             OnapCommandUtils.log.info("Invalid Schema yaml {}", schemaURI);
335                         }
336                         else{
337                             schema.setVersion(obj.toString());
338
339                             if (!schema.getVersion().equalsIgnoreCase(OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0)) {
340                                 String schemaURI = schema.getSchemaURI();
341                                 OnapCommandUtils.log.info("Unsupported Schema version found {} " + schemaURI);
342                             }
343                             else{
344
345                                 //There are schema like default input parameters and does not have command name
346                                 if (resourceMap.get(NAME) != null) {
347                                     schema.setSchemaName(resource.getFilename());
348                                     schema.setCmdName((String) resourceMap.get(NAME));
349
350                                     schema.setDescription((String) resourceMap.get(DESCRIPTION));
351
352                                     Map<String, ?> infoMap = (Map<String, ?>) resourceMap.get(OnapCommandConstants.INFO);
353                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_TYPE) != null) {
354                                         schema.setType(infoMap.get(OnapCommandConstants.INFO_TYPE).toString());
355                                     }
356
357                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_PRODUCT) != null) {
358                                         schema.setProduct(infoMap.get(OnapCommandConstants.INFO_PRODUCT).toString());
359                                     }
360
361                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_IGNORE) != null) {
362                                         schema.setIgnore(infoMap.get(OnapCommandConstants.INFO_IGNORE).toString());
363                                     }
364
365                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_STATE) != null) {
366                                         schema.setState(infoMap.get(OnapCommandConstants.INFO_STATE).toString());
367                                     }
368
369                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_SERVICE) != null) {
370                                         schema.setService(infoMap.get(OnapCommandConstants.INFO_SERVICE).toString());
371                                     }
372
373                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_AUTHOR) != null) {
374                                         schema.setAuthor(infoMap.get(OnapCommandConstants.INFO_AUTHOR).toString());
375                                     }
376
377                                     if (infoMap != null && infoMap.get(OnapCommandConstants.INFO_METADATA) != null) {
378                                         schema.setMetadata((Map<String, String>)infoMap.get(OnapCommandConstants.INFO_METADATA));
379                                     }
380
381                                     schema.setSchemaProfile(identitySchemaProfileType(resourceMap));
382
383                                     if (resourceMap.containsKey(PARAMETERS)) {
384                                        schema.setInputs((List<Object>)resourceMap.get(PARAMETERS));
385                                        if (defaultInputs.get(schema.getSchemaProfile()) != null) {
386                                           schema.getInputs().addAll(defaultInputs.get(schema.getSchemaProfile()));
387                                        }
388                                     }
389
390                                     if (resourceMap.containsKey(RESULTS)) {
391                                         schema.setOutputs((List<Object>)((Map<String, Object>)resourceMap.get(RESULTS)).get(ATTRIBUTES));
392                                      }
393
394                                      extSchemas.add(schema);
395                                 }
396                             }
397                         }
398                     }
399                 }
400             }
401         } catch (IOException e) {
402             throw new OnapCommandDiscoveryFailed(SCHEMA_DIRECTORY, e);
403         }
404
405         try {
406             Resource[] samples = findResources(OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN);
407             for (Resource sample : samples) {
408                     updateSchemaInfoWithSample(sample, extSchemas);
409             }
410         } catch (IOException e) {
411             throw new OnapCommandDiscoveryFailed(OnapCommandConstants.VERIFY_SAMPLES_DIRECTORY, e);
412         }
413
414         return extSchemas;
415     }
416
417     private static void updateSchemaInfoWithSample(Resource sampleResourse,
418                                                       List<OnapCommandSchemaInfo> schemaInfos) throws OnapCommandInvalidSchema, IOException {
419         Map<String, ?> infoMap = loadSchema(sampleResourse);
420
421         if (infoMap == null) {
422             return;
423         }
424
425         Object sampleVersion = infoMap.get(OPEN_CLI_SAMPLE_VERSION);
426         if (sampleVersion == null) {
427             OnapCommandUtils.log.info("Invalid Sample yaml {}", sampleResourse.getURI());
428             return;
429         }
430
431         if (!sampleVersion.toString().equalsIgnoreCase(OnapCommandConstants.OPEN_CLI_SAMPLE_VERSION_VALUE_1_0)) {
432             OnapCommandUtils.log.info("Unsupported Sample version found {}", sampleResourse.getURI());
433             return;
434         }
435
436         String cmdName = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_NAME);
437         String version = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_VERSION);
438
439         Optional<OnapCommandSchemaInfo> optSchemaInfo = schemaInfos.stream()
440                 .filter(e -> e.getCmdName().equals(cmdName) && e.getProduct().equals(version))
441                 .findFirst();
442
443         if (optSchemaInfo.isPresent()) {
444             OnapCommandSchemaInfo onapCommandSchemaInfo = optSchemaInfo.get();
445             onapCommandSchemaInfo.getSampleFiles().add(sampleResourse.getFilename());
446         }
447     }
448
449     /**
450      * Discover the Oclip commands.
451      *
452      * @return list
453      */
454     public static List<Class<OnapCommand>> discoverCommandPlugins() {
455         ServiceLoader<OnapCommand> loader = ServiceLoader.load(OnapCommand.class);
456         List<Class<OnapCommand>> clss = new ArrayList<>();
457         for (OnapCommand implClass : loader) {
458             clss.add((Class<OnapCommand>) implClass.getClass());
459         }
460
461         return clss;
462     }
463
464     /**
465      * Instantiate command plugin
466      * @throws OnapCommandInstantiationFailed
467      */
468     public static OnapCommand loadCommandClass(Class <? extends OnapCommand> cls) throws OnapCommandInstantiationFailed {
469         try {
470             Constructor<?> constr = cls.getConstructor();
471             return (OnapCommand) constr.newInstance();
472         } catch (NoSuchMethodException | SecurityException | InstantiationException
473                 | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
474             throw new OnapCommandInstantiationFailed(cls.getName(), e);
475         }
476
477     }
478
479     public static List<Map<String, Object>> createTestSuite(String cmd, String version) throws OnapCommandException {
480
481         ArrayList<Map<String, Object>> testSamples = new ArrayList<>();
482
483
484         List<Resource> resources = new ArrayList<>();
485         OnapCommandSchemaInfo schemaInfo =  getSchemaInfo(cmd, version);
486
487         List<String> sampleFiles = new ArrayList<>();
488         if (schemaInfo != null && !schemaInfo.getSampleFiles().isEmpty()) { //NOSONAR
489             sampleFiles.addAll(schemaInfo.getSampleFiles());
490         }
491
492         for (String sampleFile : sampleFiles) {
493             try {
494                 Resource resource = OnapCommandDiscoveryUtils.findResource(sampleFile,
495                         OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN);
496                 resources.add(resource);
497             } catch (IOException e) {
498                 throw new OnapCommandInvalidSample("Sample file does not exist : " + sampleFile , e);
499             }
500         }
501
502         for (Resource resource : resources) {
503
504             Map<String, ?> stringMap = OnapCommandDiscoveryUtils.loadYaml(resource);
505             Map<String, Map<String, String>> samples = (Map<String, Map<String, String>>) stringMap
506                     .get(OnapCommandConstants.VERIFY_SAMPLES);
507
508             for (Map.Entry<String,Map<String, String>> entry : samples.entrySet()) {
509                 String sampleId=entry.getKey();
510                 Map<String, String> sample = entry.getValue();
511
512                 List<String> inputArgs = new ArrayList<>();
513                 if (sample.get(OnapCommandConstants.VERIFY_INPUT) != null) {
514                     inputArgs.addAll(Arrays.asList(sample.get(OnapCommandConstants.VERIFY_INPUT).trim().split(" ")));
515                 }
516                 inputArgs.add(OnapCommandConstants.VERIFY_LONG_OPTION);
517
518                 HashMap<String, Object> map = new HashMap<>();
519                 map.put(OnapCommandConstants.VERIFY_INPUT, inputArgs);
520                 map.put(OnapCommandConstants.VERIFY_OUPUT, sample.get(OnapCommandConstants.VERIFY_OUPUT));
521                 map.put(OnapCommandConstants.VERIFY_MOCO, sample.get(OnapCommandConstants.VERIFY_MOCO));
522                 map.put(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID, resource.getFilename());
523                 map.put(OnapCommandConstants.VERIFY_SAMPLE_ID, sampleId);
524                 testSamples.add(map);
525             }
526         }
527         return testSamples;
528     }
529
530     /**
531      * Get schema map.
532      *
533      * @param resource
534      *            resource obj
535      * @return map
536      * @throws OnapCommandInvalidSchema
537      *             exception
538      */
539     public static Map<String, Object> loadYaml(Resource resource) throws OnapCommandInvalidSchema {
540         Map<String, Object> values = null;
541         try {
542             values = loadYaml(resource.getInputStream());
543         } catch (Exception e) {
544             throw new OnapCommandInvalidSchema(resource.getFilename(), e);
545         }
546         return values;
547     }
548
549     /**
550      * Get schema map.
551      *
552      * @param resource
553      *            resource obj
554      *            ignoreInvalidSchema boolean
555      * @return map
556      * @throws OnapCommandInvalidSchema
557      *             exception
558      */
559     public static Map<String, ?> loadYaml(Resource resource, boolean ignoreInvalidSchema) throws OnapCommandInvalidSchema, IOException {
560         Map<String, ?> values = null;
561         try {
562             values = loadYaml(resource.getInputStream());
563         } catch (OnapCommandException | IOException e) {
564             OnapCommandUtils.log.error("Ignores invalid schema {} {}", resource.getURI(), e);
565         }
566         return values;
567     }
568
569     /**
570      * Get schema map.
571      *
572      * @param filePath
573      * @return map
574      * @throws OnapCommandInvalidSchema
575      *             exception
576      */
577     public static Map<String, Object> loadYaml(String filePath) throws OnapCommandInvalidSchema {
578         Map<String, Object> values = null;
579         try {
580             values = loadYaml(new FileInputStream(new File(filePath)));
581         } catch (Exception e) {
582             throw new OnapCommandInvalidSchema(filePath, e);
583         }
584         return values;
585     }
586
587
588     /**
589      * Get schema map.
590      *
591      * @param inputStream
592      * @return map
593      * @throws OnapCommandInvalidSchema
594      *             exception
595      */
596     public static Map<String, Object> loadYaml(InputStream inputStream) throws OnapCommandInvalidSchema {
597         Map<String, Object> values = null;
598         try(InputStreamReader inputStreamReader = new InputStreamReader(inputStream);){
599             YamlReader reader = new YamlReader(inputStreamReader);
600             values = (Map<String, Object>) reader.read();
601             } catch (IOException e) {
602                 throw new OnapCommandInvalidSchema(inputStream.getClass().getName(),e.getMessage());
603             }
604         return values;
605     }
606 }
607