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.util.ArrayList;
 
  23 import org.slf4j.Logger;
 
  24 import org.slf4j.LoggerFactory;
 
  25 import org.yaml.snakeyaml.Yaml;
 
  26 import org.yaml.snakeyaml.parser.ParserException;
 
  28 import net.sf.json.JSON;
 
  29 import net.sf.json.JSONArray;
 
  30 import net.sf.json.JSONObject;
 
  37  * @author              quanzhong@huawei.com
 
  38  * @version     NFVO 0.5  Oct 25, 2016
 
  40 public class YamlUtil {
 
  41     private static Logger log = LoggerFactory.getLogger(YamlUtil.class);
 
  53     public static JSON yamlToJson(String yamlName){
 
  54         Object res = parseYaml(yamlName);
 
  55         if(res instanceof ArrayList){
 
  56             return JSONArray.fromObject(res);
 
  58         return  JSONObject.fromObject(res);
 
  69     public static String loadYaml(String yamlName){
 
  72             Yaml yaml = new Yaml();
 
  73             File file =new File(yamlName);
 
  75             Object obj = yaml.load(new FileInputStream(file));
 
  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);
 
  96     public static Object parseYaml(String yamlName){
 
  99             File file =new File(yamlName);
 
 100             obj = new Yaml().load(new FileInputStream(file));
 
 101         }catch(ParserException e){
 
 102             log.error("error format:",e);
 
 104         }catch(FileNotFoundException e) {
 
 105             log.error("the yaml file not exist {}",yamlName,e);
 
 110     public static void main(String[] args) throws IOException {
 
 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         try(Writer w = new FileWriter(new File("C:\\Users\\z00292420\\Desktop\\juju"))){
 
 122          log.error("Write Yaml Error: ",e);
 
 123                 throw new IOException("Write Yaml Error" + e);
 
 125         //System.out.println(newYaml);