2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.util.ArrayList;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.parser.ParserException;
29 import net.sf.json.JSON;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
38 * @author quanzhong@huawei.com
39 * @version NFVO 0.5 Oct 25, 2016
41 public class YamlUtil {
43 private static Logger log = LoggerFactory.getLogger(YamlUtil.class);
52 public static JSON yamlToJson(String yamlName) {
53 Object res = parseYaml(yamlName);
54 if(res instanceof ArrayList) {
55 return JSONArray.fromObject(res);
57 return JSONObject.fromObject(res);
67 public static String loadYaml(String yamlName) {
70 Yaml yaml = new Yaml();
71 File file = new File(yamlName);
73 Object obj = yaml.load(new FileInputStream(file));
77 log.debug("yaml-> " + res);
78 } catch(ParserException e) {
79 log.error("error format:", e);
80 } catch(FileNotFoundException e) {
81 log.error("the yaml file not exist {}", yamlName, e);
93 public static Object parseYaml(String yamlName) {
96 File file = new File(yamlName);
97 obj = new Yaml().load(new FileInputStream(file));
98 } catch(ParserException e) {
99 log.error("error format:", e);
101 } catch(FileNotFoundException e) {
102 log.error("the yaml file not exist {}", yamlName, e);