+
+ public Map<String, String> load(String fileName) throws IOException {
+ File newConfiguration = new File(fileName);
+ InputStream is = null;
+ try {
+ is = new FileInputStream(newConfiguration);
+ } catch (FileNotFoundException e) {
+ logger.error(e);
+ }
+
+ Yaml yaml = new Yaml();
+ @SuppressWarnings("unchecked")
+ Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(is);
+ StringBuilder sb = new StringBuilder();
+ Map<String, String> settings = new HashMap<String, String>();
+ if (yamlMap == null) {
+ return settings;
+ }
+ List<String> path = new ArrayList<String>();
+ serializeMap(settings, sb, path, yamlMap);
+ return settings;
+ }
+
+ public Map<String, String> load(byte[] source) throws IOException {
+ Yaml yaml = new Yaml();
+ @SuppressWarnings("unchecked")
+ Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(source.toString());
+ StringBuilder sb = new StringBuilder();
+ Map<String, String> settings = new HashMap<String, String>();
+ if (yamlMap == null) {
+ return settings;
+ }
+ List<String> path = new ArrayList<String>();
+ serializeMap(settings, sb, path, yamlMap);
+ return settings;
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
+ for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
+ if (entry.getValue() instanceof Map) {
+ path.add((String) entry.getKey());
+ serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
+ path.remove(path.size() - 1);
+ } else if (entry.getValue() instanceof List) {
+ path.add((String) entry.getKey());
+ serializeList(settings, sb, path, (List) entry.getValue());
+ path.remove(path.size() - 1);
+ } else {
+ serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
+ int counter = 0;
+ for (Object listEle : yamlList) {
+ if (listEle instanceof Map) {
+ path.add(Integer.toString(counter));
+ serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
+ path.remove(path.size() - 1);
+ } else if (listEle instanceof List) {
+ path.add(Integer.toString(counter));
+ serializeList(settings, sb, path, (List<String>) listEle);
+ path.remove(path.size() - 1);
+ } else {
+ serializeValue(settings, sb, path, Integer.toString(counter), listEle);
+ }
+ counter++;
+ }
+ }
+
+ private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
+ if (value == null) {
+ return;
+ }
+ sb.setLength(0);
+ for (String pathEle : path) {
+ sb.append(pathEle).append('.');
+ }
+ sb.append(name);
+ settings.put(sb.toString(), value.toString());
+ }
+
+
+ public void parseTosca (String fileName){
+ Map<String,String> map= new HashMap<String, String>();
+ try {
+ map=load(fileName);
+ for(String key:map.keySet()){
+ if(key.contains("policy.nodes.Root"))
+ {
+ continue;
+ }
+ else if(key.contains("policy.nodes")){
+ String wordToFind = "policy.nodes.";
+ int indexForPolicyNode=key.indexOf(wordToFind);
+ String subNodeString= key.substring(indexForPolicyNode+13, key.length());
+
+ stringBetweenDots(subNodeString,map.get(key));
+ }
+ else if(key.contains("policy.data")){
+ String wordToFind="policy.data.";
+ int indexForPolicyNode=key.indexOf(wordToFind);
+ String subNodeString= key.substring(indexForPolicyNode+12, key.length());
+
+ stringBetweenDotsForDataFields(subNodeString,map.get(key));
+ Iterator<String> itr= uniqueDataKeys.iterator();
+ while(itr.hasNext()){
+ logger.info(itr.next());
+ }
+ }
+ }
+
+ String attributeIndividualString="";
+ String userDefinedIndividualString="";
+ String referenceIndividualAttributes="";
+
+ String attributeString="";
+ String userDefinedString="";
+ String referenceAttributes="";
+ String listConstraints="";
+
+ for(String uniqueDataKey: uniqueDataKeys){
+ String[] uniqueDataKeySplit= uniqueDataKey.split("%");
+ userDefinedIndividualString=userDefinedIndividualString+uniqueDataKey+"=";
+ userDefinedIndividualString=userDefinedIndividualString+"#A:defaultValue-#B:required-#C:MANY-false";
+ for(String key:map.keySet()){
+ if(key.contains("policy.data")){
+ String containsKey= uniqueDataKeySplit[1]+".type";
+ if(key.contains(uniqueDataKeySplit[0])){
+ if(key.contains("default")){
+ userDefinedIndividualString=userDefinedIndividualString.replace("#B", map.get(key));
+ }
+ else if(key.contains("required")){
+ userDefinedIndividualString=userDefinedIndividualString.replace("#C", map.get(key));
+ }
+ else if(key.contains(containsKey)){
+ String typeValue= map.get(key);
+ userDefinedIndividualString=userDefinedIndividualString.replace("#A", typeValue);
+ }
+ }
+ }
+
+ }
+ if(userDefinedString!=""){
+ userDefinedString=userDefinedString+","+userDefinedIndividualString;
+ }else{
+ userDefinedString=userDefinedString+userDefinedIndividualString;
+ }
+ userDefinedIndividualString="";
+ }
+ logger.info("userDefinedString :"+userDefinedString);
+
+ HashMap<String,ArrayList<String>> mapKey= new HashMap<String,ArrayList<String>>();
+ String secondPartString="";
+ String firstPartString="";
+ for(String value: userDefinedString.split(",")){
+ String[] splitWithEquals= value.split("=");
+ secondPartString=splitWithEquals[0].substring(splitWithEquals[0].indexOf("%")+1);
+ firstPartString=splitWithEquals[0].substring(0, splitWithEquals[0].indexOf("%"));
+ ArrayList<String> list;
+ if(mapKey.containsKey(firstPartString)){
+ list = mapKey.get(firstPartString);
+ list.add(secondPartString+"<"+splitWithEquals[1]);
+ } else {
+ list = new ArrayList<String>();
+ list.add(secondPartString+"<"+splitWithEquals[1]);
+ mapKey.put(firstPartString, list);
+ }
+ }
+
+ JSONObject mainObject= new JSONObject();;
+ JSONObject json;
+ for(String s: mapKey.keySet()){
+ json= new JSONObject();
+ List<String> value=mapKey.get(s);
+ for(String listValue:value){
+ String[] splitValue=listValue.split("<");
+ json.put(splitValue[0], splitValue[1]);
+ }
+ mainObject.put(s,json);
+ }
+
+ logger.info(mainObject);
+
+ Iterator<String> keysItr = mainObject.keys();
+ while(keysItr.hasNext()) {
+ String key = keysItr.next();
+ String value = mainObject.get(key).toString();
+ retmap.put(key, value);
+ }
+
+ for(String str:retmap.keySet()){
+ logger.info(str+":"+retmap.get(str));
+ }
+
+ String typeValueFromKey="";
+ boolean userDefinedDataType=false;
+ boolean isList=false;
+ for(String uniqueKey: uniqueKeys){
+ List<String> constraints= new ArrayList<String>();
+ logger.info("====================");
+ attributeIndividualString=attributeIndividualString+uniqueKey+"=";
+ attributeIndividualString=attributeIndividualString+"#A:defaultValue-#B:required-#C:MANY-false";
+
+ logger.info("UniqueStrings: "+uniqueKey);
+ for(String key:map.keySet()){
+ if(key.contains("policy.nodes.Root")||
+ key.contains("policy.data"))
+ {
+ continue;
+ }
+ else if(key.contains("policy.nodes")){
+ if(key.contains(uniqueKey)){
+ int p=key.lastIndexOf(".");
+ String firstLastOccurance=key.substring(0,p);
+ int p1=firstLastOccurance.lastIndexOf(".");
+ String secondLastOccurance= firstLastOccurance.substring(p1+1,firstLastOccurance.length());
+ if(secondLastOccurance.equals(uniqueKey)){
+ String checkTypeString= firstLastOccurance+".type";
+ typeValueFromKey= map.get(checkTypeString);
+ }//Its a list.
+ else if (key.contains("entry_schema")){
+ if(key.contains("constraints")){
+ constraints.add(map.get(key));
+ }
+ if(key.contains("type")){
+ isList=true;
+ String value= map.get(key);
+ if(! (value.contains("string")) ||
+ (value.contains("integer")) ||
+ (value.contains("boolean")) )
+ {
+ if(!key.contains("valid_values")){
+ String trimValue=value.substring(value.lastIndexOf(".")+1);
+ referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+trimValue+":MANY-true";
+ attributeIndividualString="";
+ }
+
+ }
+ }
+ }
+
+ if(!(typeValueFromKey.equals("string")||
+ typeValueFromKey.equals("integer") ||
+ typeValueFromKey.equals("boolean")))
+ {
+ if(typeValueFromKey.equals("list")){
+ isList=true;
+ userDefinedDataType=false;
+ }
+ else{
+ userDefinedDataType=true;
+ }
+ }
+ if(userDefinedDataType==false && isList==false){
+ if(key.contains("default")){
+ attributeIndividualString=attributeIndividualString.replace("#B", map.get(key));
+ }
+ else if(key.contains("required")){
+ attributeIndividualString=attributeIndividualString.replace("#C", map.get(key));
+ }
+ else if(key.contains("type")){
+ String typeValue= map.get(key);
+ attributeIndividualString=attributeIndividualString.replace("#A", typeValue);
+ }
+ }
+ else if(userDefinedDataType==true){
+ String checkTypeAndUpdate=key.substring(p+1);
+ if(checkTypeAndUpdate.equals("type")){
+ String value=map.get(key);
+ String trimValue=value.substring(value.lastIndexOf(".")+1);
+ referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+trimValue+":MANY-false";
+ }
+ attributeIndividualString="";
+ }
+ }
+ }
+ }
+
+ if(constraints!=null &&constraints.isEmpty()==false){
+ //List handling.
+ listConstraints=uniqueKey.toUpperCase()+"=[";
+ isList=true;
+ for(String str:constraints){
+ listConstraints=listConstraints+str+",";
+ }
+ listConstraints+="],";
+ logger.info(listConstraints);
+ attributeIndividualString="";
+ referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+uniqueKey.toUpperCase()+":MANY-false";
+ constraints=null;
+
+ }
+ if(userDefinedDataType==false && isList==false){
+ if(attributeString!=""){
+ attributeString=attributeString+","+attributeIndividualString;
+ }else{
+ attributeString=attributeString+attributeIndividualString;
+ }
+ }
+ if(isList==true || userDefinedDataType==true){
+ if(referenceAttributes!=""){
+ referenceAttributes=referenceAttributes+","+referenceIndividualAttributes;
+ }else{
+ referenceAttributes=referenceAttributes+referenceIndividualAttributes;
+ }
+ logger.info("ReferenceAttributes: "+referenceAttributes);
+ }
+
+ logger.info("AttributeString: "+ attributeString);
+ logger.info("ListConstraints is: "+listConstraints);
+
+ attributeIndividualString="";
+ referenceIndividualAttributes="";
+ userDefinedDataType=false;
+ isList=false;
+
+ }
+ this.listConstraints=listConstraints;
+ this.referenceAttributes=referenceAttributes;
+ this.attributeString=attributeString;
+ } catch (IOException e) {
+ logger.error(e);
+ }
+ }
+