- // Second index of dot should be returned.
- public int stringBetweenDots(String str){
- String stringToSearch=str;
- String[]ss=stringToSearch.split("\\.");
- if(ss!=null){
- int len= ss.length;
- if(len>2){
- uniqueKeys.add(ss[2]);
- }
- }
-
- return uniqueKeys.size();
- }
-
- public void stringBetweenDotsForDataFields(String str){
- String stringToSearch=str;
- String[]ss=stringToSearch.split("\\.");
- if(ss!=null){
- int len= ss.length;
-
- if(len>2){
- uniqueDataKeys.add(ss[0]+"%"+ss[2]);
- }
- }
- }
-
-
- 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<>();
- if (yamlMap == null) {
- return settings;
- }
- List<String> path = new ArrayList <>();
- 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(Arrays.toString(source));
- StringBuilder sb = new StringBuilder();
- Map<String, String> settings = new HashMap <>();
- if (yamlMap == null) {
- return settings;
- }
- List<String> path = new ArrayList <>();
- 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());
- }
-
- void parseDataAndPolicyNodes(Map<String,String> map){
- 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);
- }
- 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);
- }
- }
- }
-
- HashMap<String,String> parseDataNodes(Map<String,String> map){
- HashMap<String,String> dataMapForJson=new HashMap <>();
- for(String uniqueDataKey: uniqueDataKeys){
- if(uniqueDataKey.contains("%")){
- String[] uniqueDataKeySplit= uniqueDataKey.split("%");
- String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
- String typeValue=map.get(findType);
- LOGGER.info(typeValue);
- if(typeValue.equalsIgnoreCase(STRING)||
- typeValue.equalsIgnoreCase(INTEGER)
- )
- {
- String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
- String defaultValue= map.get(findDefault);
- LOGGER.info("defaultValue is:"+ defaultValue);
-
- String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
- String requiredValue= map.get(findRequired);
- LOGGER.info("requiredValue is:"+ requiredValue);
-
- StringBuilder attributeIndividualStringBuilder= new StringBuilder();
- attributeIndividualStringBuilder.append(typeValue+":defaultValue-");
- attributeIndividualStringBuilder.append(defaultValue+":required-");
- attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
- dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
- }
- else if(typeValue.equalsIgnoreCase(LIST)){
- String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
- String listValue=map.get(findList);
- if(listValue!=null){
- LOGGER.info("Type of list is:"+ listValue);
- //Its userdefined
- if(listValue.contains(".")){
- String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(trimValue+":MANY-true");
- dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
- }//Its string
- else{
- StringBuilder stringListItems= new StringBuilder();
- stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false");
- dataMapForJson.put(uniqueDataKey, stringListItems.toString());
- dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
- for(int i=0;i<10;i++){
- String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
- String constraintsValue=map.get(findConstraints);
- LOGGER.info(constraintsValue);
- if(constraintsValue==null){
- break;
- }
- else{
- dataConstraints.add(constraintsValue);
- dataListBuffer.append(constraintsValue+",");
- }
- }
- dataListBuffer.append("]#");
-
- LOGGER.info(dataListBuffer);
- }
- }
- }
- else{
- String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type";
- String userDefinedValue=map.get(findUserDefined);
- String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(trimValue+":MANY-false");
- dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
-
- }
- }
- }
- return dataMapForJson;
- }
-
- void constructJsonForDataFields(HashMap<String,String> dataMapForJson){
- HashMap<String,HashMap<String,String>> dataMapKey= new HashMap <>();
- HashMap<String,String> hmSub;
- for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
- String uniqueDataKey= entry.getKey();
- String[] uniqueDataKeySplit=uniqueDataKey.split("%");
- String value= dataMapForJson.get(uniqueDataKey);
- if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
- hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
- hmSub.put(uniqueDataKeySplit[1], value);
- }
- else{
- hmSub=new HashMap <>();
- hmSub.put(uniqueDataKeySplit[1], value);
- }
-
- dataMapKey.put(uniqueDataKeySplit[0], hmSub);
- }
-
- JSONObject mainObject= new JSONObject();
- JSONObject json;
- for(Map.Entry<String,HashMap<String,String>> entry: dataMapKey.entrySet()){
- String s=entry.getKey();
- json= new JSONObject();
- HashMap<String,String> jsonHm=dataMapKey.get(s);
- for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
- String key=entryMap.getKey();
- json.put(key, jsonHm.get(key));
- }
- mainObject.put(s,json);
- }
- Iterator<String> keysItr = mainObject.keys();
- while(keysItr.hasNext()) {
- String key = keysItr.next();
- String value = mainObject.get(key).toString();
- retmap.put(key, value);
- }
-
- LOGGER.info("#############################################################################");
- LOGGER.info(mainObject);
- LOGGER.info("###############################################################################");
- }
-
-
- HashMap<String,HashMap<String,String>> parsePolicyNodes(Map<String,String> map){
- HashMap<String,HashMap<String,String>> mapKey= new HashMap <>();
- for(String uniqueKey: uniqueKeys){
- HashMap<String,String> hm;
-
- for(Map.Entry<String,String> entry:map.entrySet()){
- String key=entry.getKey();
- if(key.contains(uniqueKey) && key.contains("policy.nodes")){
- if(mapKey.containsKey(uniqueKey)){
- hm = mapKey.get(uniqueKey);
- String keyStr= key.substring(key.lastIndexOf('.')+1);
- String valueStr= map.get(key);
- if(("type").equals(keyStr)){
- if(!key.contains("entry_schema"))
- {
- hm.put(keyStr,valueStr);
- }
- }else{
- hm.put(keyStr,valueStr);
- }
-
- } else {
- hm = new HashMap <>();
- String keyStr= key.substring(key.lastIndexOf('.')+1);
- String valueStr= map.get(key);
- if(("type").equals(keyStr)){
- if(!key.contains("entry_schema"))
- {
- hm.put(keyStr,valueStr);
- }
- }else{
- hm.put(keyStr,valueStr);
- }
- mapKey.put(uniqueKey, hm);
- }
- }
- }
- }
- return mapKey;
- }
-
- void createAttributes(HashMap<String,HashMap<String,String>> mapKey){
- StringBuilder attributeStringBuilder= new StringBuilder();
- StringBuilder referenceStringBuilder= new StringBuilder();
- StringBuilder listBuffer= new StringBuilder();
- List<String> constraints= new ArrayList<>();
- for(Map.Entry<String,HashMap<String,String>> entry: mapKey.entrySet()){
- String keySetString= entry.getKey();
- HashMap<String,String> keyValues=mapKey.get(keySetString);
- if(keyValues.get("type").equalsIgnoreCase(STRING)||
- keyValues.get("type").equalsIgnoreCase(INTEGER)
- ){
- StringBuilder attributeIndividualStringBuilder= new StringBuilder();
- attributeIndividualStringBuilder.append(keySetString+"=");
- attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
- attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
- attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
- attributeStringBuilder.append(attributeIndividualStringBuilder+",");
-
- }
- else if(keyValues.get("type").equalsIgnoreCase(LIST)){
- //List Datatype
- Set<String> keys= keyValues.keySet();
- Iterator<String> itr=keys.iterator();
- while(itr.hasNext()){
- String key= itr.next();
- if((!("type").equals(key) ||("required").equals(key)))
- {
- String value= keyValues.get(key);
- //The "." in the value determines if its a string or a user defined type.
- if (!value.contains(".")){
- //This is string
- constraints.add(keyValues.get(key));
- }else{
- //This is userdefined string
- String trimValue=value.substring(value.lastIndexOf('.')+1);
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
- referenceStringBuilder.append(referenceIndividualStringBuilder+",");
- }
- }
- }
-
- }else{
- //User defined Datatype.
- String value=keyValues.get("type");
- String trimValue=value.substring(value.lastIndexOf('.')+1);
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
- referenceStringBuilder.append(referenceIndividualStringBuilder+",");
-
- }
- if(constraints!=null &&constraints.isEmpty()==false){
- //List handling.
- listBuffer.append(keySetString.toUpperCase()+"=[");
- for(String str:constraints){
- listBuffer.append(str+",");
- }
- listBuffer.append("]#");
- LOGGER.info(listBuffer);
-
-
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
- referenceStringBuilder.append(referenceIndividualStringBuilder+",");
- constraints.clear();
- }
- }
-
- dataListBuffer.append(listBuffer);
-
-
- LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
- LOGGER.info("Whole attribute String is:"+attributeStringBuilder);
- LOGGER.info("Whole reference String is:"+referenceStringBuilder);
- LOGGER.info("List String is:"+listBuffer);
- LOGGER.info("Data list buffer is:"+dataListBuffer);
- LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
-
- this.listConstraints=dataListBuffer.toString();
- this.referenceAttributes=referenceStringBuilder.toString();
- this.attributeString=attributeStringBuilder.toString();
- }
-
-
-
- public void parseTosca (String fileName){
- Map<String,String> map= new HashMap<>();
-
- try {
- map=load(fileName);
-
- parseDataAndPolicyNodes(map);
-
- HashMap<String,String> dataMapForJson=parseDataNodes(map);
-
- constructJsonForDataFields(dataMapForJson);
-
- HashMap<String,HashMap<String,String>> mapKey= parsePolicyNodes(map);
-
- createAttributes(mapKey);
-
- } catch (IOException e) {
- LOGGER.error(e);
- }
-
- }
-
- private String cleanUPJson(String json) {