From d8d4c7a637e70a75a59cac53e0385d1029c5d4d5 Mon Sep 17 00:00:00 2001 From: Pierre Rioux Date: Wed, 12 Dec 2018 14:29:14 -0500 Subject: [PIE-797] adding data-dictionary builtin rule Change-Id: I08565199e34a5c88790e6879cf1e89bc9c513e92 Signed-off-by: Pierre Rioux Issue-ID: LOG-404 --- .../ruledriven/rule/builtin/DataDictionary.java | 156 +++++++++++++++++++++ src/main/resources/validation-service-beans.xml | 15 +- 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java (limited to 'src') 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 new file mode 100644 index 0000000..cddeb85 --- /dev/null +++ b/src/main/java/org/onap/aai/validation/ruledriven/rule/builtin/DataDictionary.java @@ -0,0 +1,156 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================================== + */ +package org.onap.aai.validation.ruledriven.rule.builtin; + +import com.google.gson.Gson; +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; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import org.onap.aai.cl.api.Logger; +import org.onap.aai.restclient.client.OperationResult; +import org.onap.aai.validation.exception.ValidationServiceError; +import org.onap.aai.validation.logging.ApplicationMsgs; +import org.onap.aai.validation.logging.LogHelper; + +public class DataDictionary { + + private static final Logger logger = LogHelper.INSTANCE; + + private static String credentials; + private static String urlTemplate; + private static org.onap.aai.restclient.client.RestClient restClient; + + + private DataDictionary() { + // intentionally empty + } + + /** + * Initializes this class' static variables using MethodInvokingFactoryBean + * @param props + */ + public static void setProperties(Properties props) { + + String hostport = props.getProperty("rule.datadictionary.hostport"); + String connectTimeout = props.getProperty("rule.datadictionary.connect.timeout"); + String readTimeout = props.getProperty("rule.datadictionary.read.timeout"); + String uriTemplate = props.getProperty("rule.datadictionary.uri"); + + credentials = "Basic " + props.getProperty("rule.datadictionary.credentials"); + urlTemplate = hostport + uriTemplate; + restClient = new org.onap.aai.restclient.client.RestClient() + .validateServerHostname(false) + .connectTimeoutMs(Integer.parseInt(connectTimeout)) + .readTimeoutMs(Integer.parseInt(readTimeout)); + } + + + /** + * 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. + * + * @param entityType + * @param entityName + * @param entities + * @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()); + } + + if(entities == null || entities.isEmpty()) { + final String error = "list of instances missing"; + logger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, error); + return error; + } + + String url = MessageFormat.format(urlTemplate, entityType, entityName); + Gson gson = new GsonBuilder().create(); + String payload = gson.toJson(new Request(entities)); + OperationResult result = post(url, payload); + + 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); + return result.getFailureCause(); + } + return ""; + } + + + /** + * Posts the payload to the URL + * @param url + * @param payload + * @return + */ + private static OperationResult post(String url, String payload) { + MultivaluedMap headers = new MultivaluedMapImpl(); + headers.put("x-authorization", Arrays.asList(credentials)); + return restClient.post(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + } + + + /** + * JSON serializable class representing an instance sent to data-dictionary. + */ + 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)); + } + } + + 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; + } + } +} diff --git a/src/main/resources/validation-service-beans.xml b/src/main/resources/validation-service-beans.xml index 4482c6e..862fd13 100644 --- a/src/main/resources/validation-service-beans.xml +++ b/src/main/resources/validation-service-beans.xml @@ -17,9 +17,11 @@ limitations under the License. ============LICENSE_END===================================================== --> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> @@ -37,6 +39,8 @@ limitations under the License. + + @@ -100,6 +104,15 @@ limitations under the License. + + + + + + + + + -- cgit 1.2.3-korg