Update gvnfm-driver .gitreview file
[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.*;
20 import java.util.ArrayList;
21 import java.util.Map;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.parser.ParserException;
27
28 import net.sf.json.JSON;
29 import net.sf.json.JSONArray;
30 import net.sf.json.JSONObject;
31
32 /**
33  * <br/>
34  * <p>
35  * </p>
36  * 
37  * @author              quanzhong@huawei.com
38  * @version     NFVO 0.5  Oct 25, 2016
39  */
40 public class YamlUtil {
41     private static Logger log = LoggerFactory.getLogger(YamlUtil.class);
42
43     
44     
45     /**
46      * 
47      * <br/>
48      * 
49      * @param yamlName
50      * @return
51      * @since  NFVO 0.5
52      */
53     public static JSON yamlToJson(String yamlName){
54         Object res = parseYaml(yamlName);
55         if(res instanceof ArrayList){
56             return JSONArray.fromObject(res);
57         }
58         return  JSONObject.fromObject(res);
59     }
60     
61     /**
62      * 
63      * <br/>
64      * 
65      * @param yamlName
66      * @return
67      * @since  NFVO 0.5
68      */
69     public static String loadYaml(String yamlName){
70         String res = null;
71         try {
72             Yaml yaml = new Yaml();
73             File file =new File(yamlName);
74             
75             Object obj = yaml.load(new FileInputStream(file));
76             if(obj != null){
77                 res = obj.toString();
78             }
79             log.debug("yaml-> "+res);
80         }catch(ParserException e){
81             log.error("error format:",e);
82         }catch(FileNotFoundException e) {
83             log.error("the yaml file not exist {}",yamlName,e);
84         }
85         return res;
86     }
87     
88     /**
89      * 
90      * <br/>
91      * 
92      * @param yamlName
93      * @return
94      * @since  NFVO 0.5
95      */
96     public static Object parseYaml(String yamlName){
97         Object obj = null;
98         try {
99             File file =new File(yamlName);
100             obj = new Yaml().load(new FileInputStream(file));
101         }catch(ParserException e){
102             log.error("error format:",e);
103             
104         }catch(FileNotFoundException e) {
105             log.error("the yaml file not exist {}",yamlName,e);
106         }
107         return obj;
108     }
109
110     public static void main(String[] args) throws IOException {
111
112
113         Map config = new Yaml().loadAs(new FileInputStream("C:\\Users\\z00292420\\Desktop\\juju\\config2.yaml"), Map.class);
114         Map options = (Map)config.get("options");
115         Map name = (Map)options.get("name");
116         name.put("default","hello,it's me");
117 //      new Yaml().dump(config, new FileWriter("C:\\Users\\z00292420\\Desktop\\juju\\config2.yaml"));
118         String newYaml = new Yaml().dumpAsMap(config);
119         Writer w = new FileWriter(new File("C:\\Users\\z00292420\\Desktop\\juju"));
120         w.write(newYaml);
121         w.flush();
122         w.close();
123         System.out.println(newYaml);
124     }
125 }