Remove the unused UT code
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / common / YamlUtil.java
1 /*
2  * Copyright (c) 2016, 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.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.util.ArrayList;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.parser.ParserException;
28
29 import net.sf.json.JSON;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
32
33 /**
34  * <br/>
35  * <p>
36  * </p>
37  * 
38  * @author quanzhong@huawei.com
39  * @version NFVO 0.5 Oct 25, 2016
40  */
41 public class YamlUtil {
42
43     private static Logger log = LoggerFactory.getLogger(YamlUtil.class);
44
45     /**
46      * <br/>
47      * 
48      * @param yamlName
49      * @return
50      * @since NFVO 0.5
51      */
52     public static JSON yamlToJson(String yamlName) {
53         Object res = parseYaml(yamlName);
54         if(res instanceof ArrayList) {
55             return JSONArray.fromObject(res);
56         }
57         return JSONObject.fromObject(res);
58     }
59
60     /**
61      * <br/>
62      * 
63      * @param yamlName
64      * @return
65      * @since NFVO 0.5
66      */
67     public static String loadYaml(String yamlName) {
68         String res = null;
69         try {
70             Yaml yaml = new Yaml();
71             File file = new File(yamlName);
72
73             Object obj = yaml.load(new FileInputStream(file));
74             if(obj != null) {
75                 res = obj.toString();
76             }
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);
82         }
83         return res;
84     }
85
86     /**
87      * <br/>
88      * 
89      * @param yamlName
90      * @return
91      * @since NFVO 0.5
92      */
93     public static Object parseYaml(String yamlName) {
94         Object obj = null;
95         try {
96             File file = new File(yamlName);
97             obj = new Yaml().load(new FileInputStream(file));
98         } catch(ParserException e) {
99             log.error("error format:", e);
100
101         } catch(FileNotFoundException e) {
102             log.error("the yaml file not exist {}", yamlName, e);
103         }
104         return obj;
105     }
106
107 }