From cd08571c1061eb88bea65c8eca72678661c2f586 Mon Sep 17 00:00:00 2001 From: Pierre Rioux Date: Thu, 20 Dec 2018 17:50:49 -0500 Subject: [LOG-404] updating built-in rule for data-dict API Change-Id: I8b6f79c015507facbd785fed9d9658218f7ae9ac Signed-off-by: Pierre Rioux Issue-ID: LOG-404 --- .../ruledriven/rule/builtin/DataDictionary.java | 104 +++++++++++++-------- 1 file changed, 67 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java b/src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java index cddeb85..dc5cdc2 100644 --- a/src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java +++ b/src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java @@ -22,7 +22,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import com.sun.jersey.core.util.MultivaluedMapImpl; -import groovy.lang.GString; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; @@ -46,6 +45,12 @@ public class DataDictionary { private static String urlTemplate; private static org.onap.aai.restclient.client.RestClient restClient; + public enum COMMON_MODEL_ELEMENT_TYPE { + INSTANCE, + ATTRIBUTE, + UNSUPPORTED; + } + private DataDictionary() { // intentionally empty @@ -72,33 +77,60 @@ public class DataDictionary { /** - * Generates a REST request to data-dictionary to validate the given entities. - * URI: /commonModelElements/[entityType]~[entityName]~1.0/validateInstance - * Currently only "instance" is supported for entityType. + * Generates a REST request to data-dictionary to validate the given attributes. + * + * URI: /commonModelElements/[commonModelElementId]/validateInstance + * where commonModelElementId is defined as: + * [commonModelElementType]~[commonModelElementName]~[commonModelElementVersion] + * + * Supported commonModelElementType: + * instance + * attribute * - * @param entityType - * @param entityName - * @param entities + * Examples: + * /commonModelElements/instance~nfValuesCatalog~1.0/validateInstance + * /commonModelElements/attribute~nfRole~1.0/validateInstance + * + * @param commonModelElementType "instance" or "attribute" + * @param commonModelElementName name of common model element + * @param attributeName + * @param attributeValue * @return */ - public static String validate(String entityType, String entityName, List> entities) { - - logger.debug("Executing built-in rule with..."); - for(Map attributes : entities) { - logger.debug(" " + attributes.toString()); + public static String validate(String commonModelElementType, String commonModelElementName, + String attributeName, String attributeValue) { + + COMMON_MODEL_ELEMENT_TYPE cmeType = COMMON_MODEL_ELEMENT_TYPE.UNSUPPORTED; + try { + cmeType = COMMON_MODEL_ELEMENT_TYPE.valueOf(commonModelElementType.toUpperCase()); + } catch(IllegalArgumentException e) { + final String error = "unsupported commonModelElementType: " + commonModelElementType; + logger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, error); + return error; } - if(entities == null || entities.isEmpty()) { - final String error = "list of instances missing"; + if(attributeValue == null || attributeValue.isEmpty()) { + final String error = "element value missing"; logger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, error); return error; } - String url = MessageFormat.format(urlTemplate, entityType, entityName); + logger.debug("Executing built-in rule with: '" + commonModelElementType + "', '" + commonModelElementName + + "'; attribute: " + attributeName + "=" + attributeValue); + Gson gson = new GsonBuilder().create(); - String payload = gson.toJson(new Request(entities)); + String payload = gson.toJson(new Request(cmeType, attributeName, attributeValue)); + + String url = MessageFormat.format(urlTemplate, commonModelElementType, commonModelElementName); OperationResult result = post(url, payload); + if(result.getResultCode() == 500) { + // network unreachable; log a warning and return success + logger.warn(ApplicationMsgs.EVENT_CLIENT_CLOSE_UNSENT_MESSAGE, + ValidationServiceError.REST_CLIENT_RESPONSE_ERROR.getMessage(result.getResultCode(), result.getFailureCause())); + return ""; + } + if(result.getResultCode() != 200 && result.getResultCode() != 204) { String error = ValidationServiceError.REST_CLIENT_RESPONSE_ERROR.getMessage(result.getResultCode(), result.getFailureCause()); logger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, error); @@ -127,30 +159,28 @@ public class DataDictionary { public static class Request { @Expose @SerializedName("instance") - private List instances; - - public Request(List> groovyInstances) { - instances = new ArrayList<>(); - for(Map groovyEntry : groovyInstances) { - instances.add(createInstance(groovyEntry)); + private Object instance; + + public Request(COMMON_MODEL_ELEMENT_TYPE cmeType, String attributeName, String attributeValue) { + switch(cmeType) { + case INSTANCE: + Map map = new HashMap<>(); + map.put(attributeName, attributeValue); + List list = new ArrayList<>(); + list.add(map); + instance = list; + break; + case ATTRIBUTE: + instance = attributeValue; + break; + default: + logger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, "unsupported commonModelElementType"); + break; } } - public List getInstance() { - return instances; - } - - /** - * Creates an instance entry; converts Groovy's GString into a java String - * @param groovyMap - * @return - */ - private Map createInstance(Map groovyMap) { - Map newMap = new HashMap<>(); - for(Map.Entry groovyEntry : groovyMap.entrySet()) { - newMap.put(groovyEntry.getKey().toString(), groovyEntry.getValue().toString()); - } - return newMap; + public Object getInstance() { + return instance; } } } -- cgit 1.2.3-korg