import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
+import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.controller.PolicyController;
import org.onap.policy.rest.XACMLRestProperties;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.dao.CommonClassDao;
String jsonContent = null;
try{
- jsonContent = decodeContent(root.get("policyJSON")).toString();
- constructJson(policyData, jsonContent);
+ LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
+
+ String tempJson = root.get("policyJSON").toString();
+
+ //---replace empty value with the value below before calling decodeContent method.
+ String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
+ LOGGER.info("dummyValue:" + dummyValue);
+ tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode tempJsonNode = mapper.readTree(tempJson);
+ jsonContent = decodeContent(tempJsonNode).toString();
+ constructJson(policyData, jsonContent, dummyValue);
}catch(Exception e){
LOGGER.error("Error while decoding microservice content", e);
}
return groupList;
}
- private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) {
+ private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
ObjectWriter om = new ObjectMapper().writer();
String json="";
DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
} catch (JsonProcessingException e) {
LOGGER.error("Error writing out the object", e);
}
- LOGGER.info(json);
+ LOGGER.info("input json: " + json);
+ LOGGER.info("input jsonContent: " + jsonContent);
String cleanJson = cleanUPJson(json);
- cleanJson = removeNullAttributes(cleanJson);
+ //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
+ String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""});
+ LOGGER.info("tempJson: " + tempJson);
+ cleanJson = removeNullAttributes(tempJson);
policyAdapter.setJsonBody(cleanJson);
return policyAdapter;
}
JsonNode returnNode = mapper.readTree(cleanJson);
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
boolean remove = false;
+ JsonObject removed = null;
+ boolean contentChanged = false;
while (fieldsIterator.hasNext()) {
Map.Entry<String, JsonNode> field = fieldsIterator.next();
final String key = field.getKey();
final JsonNode value = field.getValue();
+ if("content".equalsIgnoreCase(key)){
+ String contentStr = value.toString();
+ try(JsonReader reader = Json.createReader(new StringReader(contentStr))){
+ JsonObject jsonContent = reader.readObject();
+ removed = removeNull(jsonContent);
+ if(!jsonContent.toString().equals(removed.toString())){
+ contentChanged = true;
+ }
+ }
+
+ if (value==null || value.isNull()){
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove){
+ cleanJson = returnNode.toString();
+ }
if (value==null || value.isNull()){
((ObjectNode) returnNode).remove(key);
remove = true;
if (remove){
cleanJson = returnNode.toString();
}
+
+ if(contentChanged){
+ //set modified content to cleanJson
+ JSONObject jObject = new JSONObject(cleanJson);
+ jObject.put("content",removed.toString());
+ cleanJson = cleanUPJson(jObject.toString());
+ }
+
} catch (IOException e) {
LOGGER.error("Error writing out the JsonNode",e);
}
return cleanJson;
}
+ public static JsonArray removeNull(JsonArray array) {
+ JsonArrayBuilder builder = Json.createArrayBuilder();
+ int i = 0;
+ for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
+ JsonValue value = it.next();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray a = removeNull(array.getJsonArray(i));
+ if (!a.isEmpty())
+ builder.add(a);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(array.getJsonObject(i));
+ if (!object.isEmpty())
+ builder.add(object);
+ break;
+ case STRING:
+ String s = array.getString(i);
+ if (s != null && !s.isEmpty())
+ builder.add(s);
+ break;
+ case NUMBER:
+ builder.add(array.getJsonNumber(i));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(array.getBoolean(i));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public static JsonObject removeNull(JsonObject obj) {
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
+ Entry<String, JsonValue> e = it.next();
+ String key = e.getKey();
+ JsonValue value = e.getValue();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray array = removeNull(obj.getJsonArray(key));
+ if (!array.isEmpty())
+ builder.add(key, array);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(obj.getJsonObject(key));
+ if (!object.isEmpty())
+ builder.add(key, object);
+ break;
+ case STRING:
+ String s = obj.getString(key);
+ if (s != null && !s.isEmpty())
+ builder.add(key, s);
+ break;
+ case NUMBER:
+ builder.add(key, obj.getJsonNumber(key));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(key, obj.getBoolean(key));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
// Second index of dot should be returned.
public int stringBetweenDots(String str){
String stringToSearch=str;
}
+ @SuppressWarnings("unchecked")
public Map<String, String> load(String fileName) throws IOException {
File newConfiguration = new File(fileName);
- InputStream is = null;
- try {
- is = new FileInputStream(newConfiguration);
+ Yaml yaml = new Yaml();
+ Map<Object, Object> yamlMap = null;
+ try(InputStream is = new FileInputStream(newConfiguration)){
+ yamlMap = (Map<Object, Object>) yaml.load(is);
} 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) {
String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
String typeValue=map.get(findType);
LOGGER.info(typeValue);
- if(typeValue.equalsIgnoreCase(STRING)||
+ if(typeValue != null && typeValue.equalsIgnoreCase(STRING)||
typeValue.equalsIgnoreCase(INTEGER)
)
{
attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
}
- else if(typeValue.equalsIgnoreCase(LIST)){
+ else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){
String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
String listValue=map.get(findList);
if(listValue!=null){
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)
+ if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(STRING)||
+ keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(INTEGER)
){
StringBuilder attributeIndividualStringBuilder= new StringBuilder();
attributeIndividualStringBuilder.append(keySetString+"=");
attributeStringBuilder.append(attributeIndividualStringBuilder+",");
}
- else if(keyValues.get("type").equalsIgnoreCase(LIST)){
+ else if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(LIST)){
//List Datatype
Set<String> keys= keyValues.keySet();
Iterator<String> itr=keys.iterator();
}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(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()==false){
}
jsonArray.put(decodeContent(node));
jsonResult.put(arryKey, jsonArray);
- isArray = false;;
+ isArray = false;
}else{
jsonResult.put(nodeKey, decodeContent(node));
}
}
MicroServiceModels returnModel = getAttributeObject(servicename, version);
- String jsonModel = createMicroSeriveJson(returnModel);
+
+ //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());
+ allkeys = getAllKeys(json);
+ LOGGER.info("allkeys : " + allkeys);
+ }
+
+ String allManyTrueKeys = "";
+ if(allkeys != null){
+ allManyTrueKeys = 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");
List<Object> list = new ArrayList<>();
PrintWriter out = response.getWriter();
String responseString = mapper.writeValueAsString(returnModel);
- JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
+ JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}");
list.add(j);
out.write(list.toString());
return null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private String createMicroSeriveJson(MicroServiceModels returnModel) {
+ private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
Map<String, String> attributeMap = new HashMap<>();
Map<String, String> refAttributeMap = new HashMap<>();
String attribute = returnModel.getAttributes();
}
}
}
+
+
return object.toString();
}
return object;
}
+
+ //call this method to check if the key is in the many-true key set
+ private boolean isKeyFound(Set<String> allManyTruekeys, String key){
+
+ if(allManyTruekeys != null && key != null){
+ Iterator<String> iter = allManyTruekeys.iterator();
+ while(iter.hasNext()){
+ if(key.equals(iter.next())){
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ 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= null;
+ 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;
+ }
+ // this method returns a set of keys with "MANY-true" defined in their value.
+ private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
+ for (String key : json.keySet()) {
+ Object obj = json.get(key);
+ if(obj instanceof String && ((String) obj).contains("MANY-true")){
+ LOGGER.info("key : " + key);
+ LOGGER.info("obj : " + obj);
+ keys.addAll(json.keySet());
+ }
+ if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
+ if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
+ }
+
+ return keys;
+ }
@RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
policyAdapter.setGuard(value);
}
if (attributeId.equals("TTLDate") && !value.contains("NA")){
- String newDate = convertDate(value, true);
+ PolicyController controller = new PolicyController();
+ String newDate = controller.convertDate(value);
policyAdapter.setTtlDate(newDate);
}
}
}
}
}
-
- private String convertDate(String dateTTL, boolean portalType) {
- String formateDate = null;
- String[] date = dateTTL.split("T");
- String[] parts = date[0].split("-");
-
- formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
- return formateDate;
- }
public static Map<String, String> convert(String str, String split) {
Map<String, String> map = new HashMap<>();
@RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
+ modelList = new ArrayList<>();
+ dirDependencyList = new ArrayList<>();
+ classMap = new HashMap<>();
+ retmap = new HashMap<>();
+ uniqueKeys= new HashSet<>();
+ uniqueDataKeys= new HashSet<>();
+ dataListBuffer=new StringBuilder();
+ dataConstraints= new ArrayList <>();
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
boolean zip = false;
boolean yml= false;
+ String errorMsg = "";
for (FileItem item : items) {
if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
this.newModel = new MicroServiceModels();
else {
this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
}
+ }else{
+ errorMsg = "Upload error: The file name should contain '-v', such as xxx-v1802.yml";
}
}catch(Exception e){
- LOGGER.error("Upload error : " + e);
+ LOGGER.error("Upload error : ", e);
+ errorMsg = "Upload error:" + e.getMessage();
}
}
}
+
+ if(!errorMsg.isEmpty()){
+
+ PrintWriter out = response.getWriter();
+
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType("application / json");
+ request.setCharacterEncoding("UTF-8");
+
+ ObjectMapper mapper = new ObjectMapper();
+ JSONObject j = new JSONObject();
+ j.put("errorMsg", errorMsg);
+ out.write(j.toString());
+ return;
+ }
+
List<File> fileList = new ArrayList<>();;
this.directory = "model";
if (zip){