- String value=keyValues.get("type");
- if(value != null && !value.isEmpty()){
- String trimValue=value.substring(value.lastIndexOf('.')+1);
- StringBuilder referenceIndividualStringBuilder= new StringBuilder();
- referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
- referenceStringBuilder.append(referenceIndividualStringBuilder+",");
- }else{
- LOGGER.info("keyValues.get(type) is null/empty");
- }
-
- }
- if(constraints!=null && ! constraints.isEmpty()){
- //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) {
- String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
-
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
- cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
- return cleanJson;
- }
-
- private JSONObject decodeContent(JsonNode jsonNode){
- Iterator<JsonNode> jsonElements = jsonNode.elements();
- Iterator<String> jsonKeys = jsonNode.fieldNames();
- Map<String,String> element = new TreeMap<>();
- while(jsonElements.hasNext() && jsonKeys.hasNext()){
- element.put(jsonKeys.next(), jsonElements.next().toString());
- }
- JSONObject jsonResult = new JSONObject();
- JSONArray jsonArray = null;
- String oldValue = null;
- String nodeKey = null;
- String arryKey = null;
- Boolean isArray = false;
- JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
- ObjectNode node = nodeFactory.objectNode();
- String prevKey = null;
- String presKey;
- for(Entry<String, String> entry: element.entrySet()){
- String key = entry.getKey();
- String value = entry.getValue();
- if(key.contains(".")){
- presKey = key.substring(0,key.indexOf('.'));
- }else if(key.contains("@")){
- presKey = key.substring(0,key.indexOf('@'));
- }else{
- presKey = key;
- }
- // first check if we are different from old.
- LOGGER.info(key+"\n");
- if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
- if(!oldValue.equals(key.substring(0,key.indexOf('@')))){
- jsonResult.put(oldValue, jsonArray);
- jsonArray = new JSONArray();
- }
- }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){
- jsonResult.put(oldValue, jsonArray);
- isArray = false;
- jsonArray = new JSONArray();
- }
-
- prevKey = presKey;
- //
- if(key.contains(".")){
- if(nodeKey==null){
- nodeKey = key.substring(0,key.indexOf('.'));
- }
- if(nodeKey.equals(key.substring(0,key.indexOf('.')))){
- node.put(key.substring(key.indexOf('.')+1), value);
- }else{
- if(node.size()!=0){
- if(nodeKey.contains("@")){
- if(arryKey==null){
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }
- if(nodeKey.endsWith("@0")){
- isArray = true;
- jsonArray = new JSONArray();
- }
- if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
- jsonArray.put(decodeContent(node));
- }
- if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf('@')))) || !key.contains("@")){
- jsonResult.put(arryKey, jsonArray);
- jsonArray = new JSONArray();
- }
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }else{
- isArray = false;
- jsonResult.put(nodeKey, decodeContent(node));
- }
- node = nodeFactory.objectNode();
- }
- nodeKey = key.substring(0,key.indexOf('.'));
- if(nodeKey.contains("@")){
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }
- node.put(key.substring(key.indexOf('.')+1), value);
- }
- }else {
- if(node.size()!=0){
- if(nodeKey.contains("@")){
- if(arryKey==null){
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }
- if(nodeKey.endsWith("@0")){
- isArray = true;
- jsonArray = new JSONArray();
- }
- if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
- jsonArray.put(decodeContent(node));
- }
- jsonResult.put(arryKey, jsonArray);
- jsonArray = new JSONArray();
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }else{
- isArray = false;
- jsonResult.put(nodeKey, decodeContent(node));
- }
- node = nodeFactory.objectNode();
- }
- if(key.contains("@")){
- isArray = true;
- if(key.endsWith("@0")|| jsonArray==null){
- jsonArray = new JSONArray();
- }
- }else if(!key.contains("@")){
- isArray = false;
- }
- if(isArray){
- if(oldValue==null){
- oldValue = key.substring(0,key.indexOf('@'));
- }
- if(oldValue!=prevKey){
- oldValue = key.substring(0,key.indexOf('@'));
- }
- if(oldValue.equals(key.substring(0,key.indexOf('@')))){
- jsonArray.put(value);
- }else{
- jsonResult.put(oldValue, jsonArray);
- jsonArray = new JSONArray();
- }
- oldValue = key.substring(0,key.indexOf('@'));
- }else{
- jsonResult.put(key, value);
- }
- }
- }
- if(node.size()>0){
- if(nodeKey.contains("@")){
- if(jsonArray==null){
- jsonArray = new JSONArray();
- }
- if(arryKey==null){
- arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
- }
- jsonArray.put(decodeContent(node));
- jsonResult.put(arryKey, jsonArray);
- isArray = false;
- }else{
- jsonResult.put(nodeKey, decodeContent(node));
- }
- }
- if(isArray && jsonArray.length() > 0){
- jsonResult.put(oldValue, jsonArray);
- }
- return jsonResult;
- }
-
- @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
- public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
- ObjectMapper mapper = new ObjectMapper();
- mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- JsonNode root = mapper.readTree(request.getReader());
-
- String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
- String servicename = value.split("-v")[0];
- String version = null;
- if (value.contains("-v")){
- version = value.split("-v")[1];
- }
- MicroServiceModels returnModel = getAttributeObject(servicename, version);
-
-
- //Get all keys with "MANY-true" defined in their value from subAttribute
- Set<String> allkeys = null;
- if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){
- JSONObject json = new JSONObject(returnModel.getSub_attributes());
- getAllKeys(json);
- allkeys = allManyTrueKeys;
- allManyTrueKeys = new HashSet <>();
- LOGGER.info("allkeys : " + allkeys);
- }
-
- String nameOfTrueKey = "";
- if(allkeys != null){
- nameOfTrueKey = allkeys.toString();
- }
-
- String jsonModel = createMicroSeriveJson(returnModel, allkeys);
-
- JSONObject jsonObject = new JSONObject(jsonModel);
-
- JSONObject finalJsonObject = null;
- if(allkeys != null){
- Iterator<String> iter = allkeys.iterator();
- while(iter.hasNext()){
- //Convert to array values for MANY-true keys
- finalJsonObject = convertToArrayElement(jsonObject, iter.next());
- }
- }
-
- if(finalJsonObject != null){
- LOGGER.info(finalJsonObject.toString());
- jsonModel = finalJsonObject.toString();
- }
-
- //get all properties with "MANY-true" defined in Ref_attributes
- Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
- if(manyTrueProperties != null){
- JSONObject jsonObj = new JSONObject(jsonModel);
- for (String s : manyTrueProperties) {
- LOGGER.info(s);
- //convert to array element for MANY-true properties
- finalJsonObject = convertToArrayElement(jsonObj, s.trim());
- }
-
- if(finalJsonObject != null){
- LOGGER.info(finalJsonObject.toString());
- jsonModel = finalJsonObject.toString();
- }
- }
-
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application / json");
- request.setCharacterEncoding("UTF-8");
- List<Object> list = new ArrayList<>();
- PrintWriter out = response.getWriter();
- String responseString = mapper.writeValueAsString(returnModel);
- JSONObject j;
- if("".equals(nameOfTrueKey)){
- j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
- }else{
- j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + nameOfTrueKey+ "}");
- }
- list.add(j);
- out.write(list.toString());
- return null;
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
- private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
- Map<String, String> attributeMap = new HashMap<>();
- Map<String, String> refAttributeMap = new HashMap<>();
- String attribute = returnModel.getAttributes();
- if(attribute != null){
- attribute = attribute.trim();
- }
- String refAttribute = returnModel.getRef_attributes();
- if(refAttribute != null){
- refAttribute = refAttribute.trim();
- }
- String enumAttribute = returnModel.getEnumValues();
- if(enumAttribute != null){
- enumAttribute = enumAttribute.trim();
- }
- if (!StringUtils.isEmpty(attribute)){
- attributeMap = convert(attribute, ",");
- }
- if (!StringUtils.isEmpty(refAttribute)){
- refAttributeMap = convert(refAttribute, ",");
- }
-
- Gson gson = new Gson();
-
- String subAttributes = returnModel.getSub_attributes();
- if(subAttributes != null){
- subAttributes = subAttributes.trim();
- }else{
- subAttributes = "";
- }
- Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
-
- JSONObject object = new JSONObject();
- JSONArray array;
-
- for (Entry<String, String> keySet : attributeMap.entrySet()){
- array = new JSONArray();
- String value = keySet.getValue();
- if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
- array.put(value);
- object.put(keySet.getKey().trim(), array);
- }else {
- object.put(keySet.getKey().trim(), value.trim());
- }
- }
-
- for (Entry<String, String> keySet : refAttributeMap.entrySet()){
- array = new JSONArray();
- String value = keySet.getValue().split(":")[0];
- if (gsonObject.containsKey(value)){
- if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
- array.put(recursiveReference(value, gsonObject, enumAttribute));
- object.put(keySet.getKey().trim(), array);
- }else {
- object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
- }
- }else {
- if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
- array.put(value.trim());
- object.put(keySet.getKey().trim(), array);
- }else {
- object.put(keySet.getKey().trim(), value.trim());
- }
- }
- }
-
-
-
- return object.toString();
- }
-
- @SuppressWarnings("unchecked")
- private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
- JSONObject object = new JSONObject();
- Map<String, String> map;
- Object returnClass = subAttributeMap.get(name);
- map = (Map<String, String>) returnClass;
- JSONArray array;
-
- for( Entry<String, String> m:map.entrySet()){
- String[] splitValue = m.getValue().split(":");
- array = new JSONArray();
- if (subAttributeMap.containsKey(splitValue[0])){
- if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
- array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
- object.put(m.getKey().trim(), array);
- }else {
- object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
- }
- } else{
- if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
- array.put(splitValue[0].trim());
- object.put(m.getKey().trim(), array);
- }else {
- object.put(m.getKey().trim(), splitValue[0].trim());
- }
- }
- }
-
- return object;
- }
-
-
- public static JSONObject convertToArrayElement(JSONObject json, String keyValue) {
- return convertToArrayElement(json, new HashSet<>(), keyValue);
- }
-
- private static JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
- for (String key : json.keySet()) {
- Object obj = json.get(key);
- if(key.equals(keyValue.trim())){
- if(!(obj instanceof JSONArray)){
- JSONArray newJsonArray = new JSONArray();
- newJsonArray.put(obj);
- json.put(key, newJsonArray);
- }
- LOGGER.info("key : " + key);
- LOGGER.info("obj : " + obj);
- LOGGER.info("json.get(key) : " + json.get(key));
- LOGGER.info("keyValue : " + keyValue);
- keys.addAll(json.keySet());
-
- return json;
- }
-
- if (obj instanceof JSONObject) convertToArrayElement(json.getJSONObject(key), keyValue);
- }
-
- return json;
- }
-
- // call this method to get all MANY-true properties
- public static Set<String> getManyTrueProperties(String referAttributes){
- LOGGER.info("referAttributes : " + referAttributes);
- Set<String> manyTrueProperties = new HashSet<>();
-
- if(referAttributes != null){
- String[] referAarray = referAttributes.split(",");
- String []element;
- for(int i=0; i<referAarray.length; i++){
- element = referAarray[i].split("=");
- if(element.length > 1 && element[1].contains("MANY-true")){
- manyTrueProperties.add(element[0]);
- }
- }
- }
-
- return manyTrueProperties;
- }
-
- //call this method to start the recursive
- private Set<String> getAllKeys(JSONObject json) {
- return getAllKeys(json, new HashSet<>());
- }
-
- private Set<String> getAllKeys(JSONArray arr) {
- return getAllKeys(arr, new HashSet<>());
- }
-
- private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
- for (int i = 0; i < arr.length(); i++) {
- Object obj = arr.get(i);
- if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
- if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
- }
-
- return keys;
- }