2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.openecomp.mso.openstack.utils;
 
  25 import java.io.ByteArrayInputStream;
 
  26 import java.io.InputStream;
 
  27 import java.util.HashSet;
 
  28 import java.util.Iterator;
 
  31 import java.util.Map.Entry;
 
  32 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
 
  33 import org.codehaus.jackson.map.ObjectMapper;
 
  34 import java.util.LinkedHashMap;
 
  36 import org.yaml.snakeyaml.Yaml;
 
  39 public class MsoYamlEditorWithEnvt {
 
  41     private Map <String, Object> yml;
 
  42     private Yaml yaml = new Yaml ();
 
  43     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
 
  45         public MsoYamlEditorWithEnvt() {
 
  48         public MsoYamlEditorWithEnvt(byte[] b) {
 
  52     @SuppressWarnings("unchecked")
 
  53     private synchronized void init (byte[] body) {
 
  54         InputStream input = new ByteArrayInputStream (body);
 
  55         yml = (Map <String, Object>) yaml.load (input);
 
  58     @SuppressWarnings("unchecked")
 
  59         public synchronized Set <MsoHeatEnvironmentParameter> getParameterListFromEnvt() {
 
  60         // In an environment entry, the parameters section can only contain the name:value - 
 
  61         // not other attributes.
 
  62         Set <MsoHeatEnvironmentParameter> paramSet = new HashSet<MsoHeatEnvironmentParameter>();
 
  63         Map<String, Object> resourceMap = null;
 
  65                 resourceMap = (Map<String,Object>) yml.get("parameters");
 
  66         } catch (Exception e) {
 
  69         if (resourceMap == null) {
 
  72         Iterator <Entry <String, Object>> it = resourceMap.entrySet().iterator();
 
  74         while (it.hasNext()) {
 
  75                 MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter();
 
  76                 Map.Entry <String, Object> pair = it.next();
 
  77                 //Map<String, String> resourceEntry = (Map <String, String>) pair.getValue();
 
  78                 //String value = null;
 
  80                 Object obj = pair.getValue();
 
  81                 if (obj instanceof java.lang.String) {
 
  82                         //value = (String) pair.getValue();
 
  83                         // handle block scalar - literals and folded:
 
  84                         value = yaml.dump(obj);
 
  85                         // but this adds an extra '\n' at the end - which won't hurt - but we don't need it
 
  86                         value = value.substring(0, value.length() - 1);
 
  87                 } else if (obj instanceof java.util.LinkedHashMap) {
 
  88                         //Handle that it's json
 
  90                                 value = JSON_MAPPER.writeValueAsString(obj);
 
  91                         } catch (Exception e) {
 
  92                                 value = "_BAD_JSON_MAPPING";
 
  95                         //this handles integers/longs/floats/etc.
 
  96                         value = String.valueOf(obj);
 
  98                 hep.setName((String) pair.getKey());
 
 104     public synchronized Set <MsoHeatEnvironmentResource> getResourceListFromEnvt() {
 
 106                 Set<MsoHeatEnvironmentResource> resourceList = new HashSet<MsoHeatEnvironmentResource>();
 
 107                 @SuppressWarnings("unchecked")
 
 108                 Map<String, Object> resourceMap = (Map<String,Object>) yml.get("resource_registry");
 
 109                 Iterator<Entry <String,Object>> it = resourceMap.entrySet().iterator();
 
 111                 while (it.hasNext()) {
 
 112                         MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource();
 
 113                         Map.Entry<String, Object> pair = it.next();
 
 114                         her.setName((String) pair.getKey());
 
 115                         her.setValue((String) pair.getValue());
 
 116                         resourceList.add(her);
 
 119         } catch (Exception e) {
 
 124     public synchronized Set <HeatTemplateParam> getParameterList () {
 
 125         Set <HeatTemplateParam> paramSet = new HashSet <HeatTemplateParam> ();
 
 126         @SuppressWarnings("unchecked")
 
 127         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
 
 128         Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
 
 130         while (it.hasNext ()) {
 
 131             HeatTemplateParam param = new HeatTemplateParam ();
 
 132             Map.Entry <String, Object> pair = it.next ();
 
 133             @SuppressWarnings("unchecked")
 
 134             Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
 
 137                 value = resourceEntry.get ("default");
 
 138             } catch (java.lang.ClassCastException cce) {
 
 139                 // This exception only - the value is an integer. For what we're doing
 
 140                 // here - we don't care - so set value to something - and it will 
 
 141                 // get marked as not being required - which is correct.
 
 142                 //System.out.println("cce exception!");
 
 146             param.setParamName ((String) pair.getKey ());
 
 148                 param.setRequired (false);
 
 150                 param.setRequired (true);
 
 152             value = resourceEntry.get ("type");
 
 153             param.setParamType (value);
 
 155             paramSet.add (param);