Make Validation groovy rules easier for testing 17/89217/1
authorburdziak <olaf.burdziakowski@nokia.com>
Tue, 4 Jun 2019 08:22:20 +0000 (10:22 +0200)
committerburdziak <olaf.burdziakowski@nokia.com>
Tue, 4 Jun 2019 08:59:45 +0000 (10:59 +0200)
Issue-ID: AAI-2470
Change-Id: Ie28390f4ab3c9bd4f8e469812d6b19ecd82c2879
Signed-off-by: burdziak <olaf.burdziakowski@nokia.com>
.gitignore
pom.xml
src/main/java/org/onap/aai/validation/ValidationCmd.java [new file with mode: 0644]
src/main/resources/validation-cmd-beans.xml [new file with mode: 0644]

index aef0495..5e13100 100644 (file)
@@ -1,11 +1,14 @@
-logs/
-debug-logs/
-appconfig-local/
-bundleconfig-local/etc/auth/tomcat_keystore
-.classpath
+.settings
 .project
-.settings/
-staticContent/
-target/
-src/main/java/META-INF/
+.classpath
 .checkstyle
+target/
+**/logs/
+**/debug-logs/
+/.pydevproject
+/bin/
+appconfig-local/
+**/.idea/
+*/.idea
+*.iml
+.idea/
diff --git a/pom.xml b/pom.xml
index 6822fc0..c75f0e3 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                <artifactId>spring-boot-maven-plugin</artifactId>
                                <version>${spring-boot.version}</version>
                                <configuration>
+                                       <mainClass>${start-class}</mainClass>
                                        <executable>true</executable>
                                </configuration>
                                <executions>
diff --git a/src/main/java/org/onap/aai/validation/ValidationCmd.java b/src/main/java/org/onap/aai/validation/ValidationCmd.java
new file mode 100644 (file)
index 0000000..92ed5ad
--- /dev/null
@@ -0,0 +1,175 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import org.onap.aai.validation.config.RuleIndexingConfig;
+import org.onap.aai.validation.exception.ValidationServiceException;
+import org.onap.aai.validation.reader.EventReader;
+import org.onap.aai.validation.result.ValidationResult;
+import org.onap.aai.validation.result.Violation;
+import org.onap.aai.validation.ruledriven.RuleDrivenValidator;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.web.support.SpringBootServletInitializer;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.ImportResource;
+
+@Configuration
+@EnableAutoConfiguration
+@ImportResource("classpath:validation-cmd-beans.xml")
+public class ValidationCmd extends SpringBootServletInitializer {
+
+  private static final String EVENT_FILE_PATH = "event.json";
+
+  private List<Path> rulesFoldersPaths =new ArrayList<>();
+  private String eventFilePath = EVENT_FILE_PATH;
+
+  private void printUsage() {
+    System.out.println("ValidationCmd is designed to test defined rules from command line. Usage:");
+    System.out.println("ValidationCmd [-e eventFilePath] [-r rulesFolderPath][]");
+    System.out
+        .println("-e eventFilePath - path to file with json event. Default: " + EVENT_FILE_PATH);
+    System.out.println(
+        "-r rulesFolderPath - path to foler with rules definition - *.groovy fils. If more than one then each requires -r");
+  }
+
+  private boolean parseCmd(String[] args) {
+    if (args == null || args.length <= 0) {
+      return true;
+    }
+
+    for (int i = 0; i < args.length; i++) {
+      String s = args[i];
+      if (s == null || s.isEmpty()) {
+        continue;
+      }
+
+      if (0 == "-e".compareTo(s)) {
+        i++;
+        if (i >= args.length) {
+          System.out.println("After -e missing eventFilePath");
+          printUsage();
+          return false;
+        }
+        eventFilePath = args[i];
+      } else if (0 == "-r".compareTo(s)) {
+        i++;
+        if (i >= args.length) {
+          System.out.println("After -r missing rulesFoldersPath");
+          printUsage();
+          return false;
+        }
+        rulesFoldersPaths.add(Paths.get(args[i]));
+      } else {
+        System.out.println("Unknown parameter: " + s);
+        printUsage();
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  private RuleDrivenValidator createRuleDrivenValidator(ApplicationContext ctx) {
+
+    EventReader eventReader = ctx.getBean(EventReader.class);
+    RuleIndexingConfig ruleIndexingConfig = ctx.getBean(RuleIndexingConfig.class);
+
+    RuleDrivenValidator validator = new RuleDrivenValidator(rulesFoldersPaths, null, eventReader,
+        ruleIndexingConfig);
+
+    return validator;
+  }
+
+  private void validate(RuleDrivenValidator validator)
+      throws IOException, ValidationServiceException {
+
+    String event = new String(Files.readAllBytes(Paths.get(eventFilePath)));
+
+    List<ValidationResult> resultList = validator.validate(event);
+
+    printResults(resultList);
+  }
+
+  private void printResults(List<ValidationResult> resultList) {
+    if (resultList == null) {
+      return;
+    }
+
+    System.out.println("--RESULT---------------------");
+    for (ValidationResult vr : resultList) {
+      System.out.println("--VIOLATIONS---------------------");
+      for(Violation violation:vr.getViolations()){
+        System.out.println("- ");
+        System.out.println(violation.toString());
+      }
+      System.out.println("--JSON---------------------");
+      System.out.println(vr.getViolations());
+      String s = vr.toJson();
+      System.out.println(s);
+    }
+    System.out.println("--RESULT-END---------------------");
+  }
+
+  private void printConfig() {
+
+    System.out.print("rulesFoldersPaths= " );
+    for(Path path: rulesFoldersPaths)
+      System.out.print(path+",");
+    System.out.println();
+    System.out.println("eventFilePath= " + eventFilePath);
+  }
+
+  /**
+   * Create and run the Application.
+   *
+   * @param args the command line arguments
+   */
+  public static void main(String[] args) {
+
+    System.out.println("user.dir=" + System.getProperty("user.dir"));
+    ApplicationContext ctx = new AnnotationConfigApplicationContext(ValidationCmd.class);
+
+    ValidationCmd validationCmd = new ValidationCmd();
+    try {
+      if (!validationCmd.parseCmd(args)) {
+        return;
+      }
+
+      validationCmd.printConfig();
+
+      RuleDrivenValidator validator = validationCmd.createRuleDrivenValidator(ctx);
+
+      validationCmd.validate(validator);
+
+    } catch (Exception e) {
+      System.out.println(e.getMessage());
+      e.printStackTrace();
+    }
+  }
+}
diff --git a/src/main/resources/validation-cmd-beans.xml b/src/main/resources/validation-cmd-beans.xml
new file mode 100644 (file)
index 0000000..8c317d4
--- /dev/null
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:context="http://www.springframework.org/schema/context"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
+
+  <context:component-scan base-package="org.onap.aai.nodes,org.onap.aai.setup">
+    <!-- Exclude EdgesConfiguration since edge rules are not required -->
+    <context:exclude-filter type="assignable" expression="org.onap.aai.config.EdgesConfiguration" />
+  </context:component-scan>
+
+  <bean id="configTranslator" class="org.onap.aai.setup.AAIConfigTranslator" />
+
+  <bean id="nodeIngestor" class="org.onap.aai.nodes.NodeIngestor">
+    <constructor-arg ref="configTranslator" />
+  </bean>
+
+  <context:property-placeholder location="file:${CONFIG_HOME}/rule-indexing.properties"
+    ignore-unresolvable="true" ignore-resource-not-found="true" />
+
+  <context:property-placeholder
+    location="
+        classpath:event-reader.properties,
+        file:${CONFIG_HOME}/aai-environment.properties,
+        file:${CONFIG_HOME}/validation-service.properties,
+        file:${CONFIG_HOME}/validation-service-auth.properties,
+        file:${CONFIG_HOME}/schemaIngest.properties"
+    ignore-unresolvable="true" />
+
+  <bean id="eventReaderConfig" class="org.onap.aai.validation.config.EventReaderConfig">
+    <property name="eventDomainPath" value="${event.domain.path}" />
+    <property name="eventActionPath" value="${event.action.path}" />
+    <property name="eventTypePath" value="${event.type.path}" />
+    <property name="entityTypePath" value="${event.entity.type.path}" />
+    <property name="topEntityTypePath" value="${event.entity.type.top.path}" />
+    <property name="entityLinkPath" value="${event.entity.link.path}" />
+    <property name="entityLinkDelimiter" value="${event.entity.link.delimiter}" />
+    <property name="entityPath" value="${event.entity.path}" />
+    <property name="nestedEntityPath" value="${event.entity.nested.path}" />
+    <property name="entityIdPath" value="${entity.id.path}" />
+    <property name="entityResourceVersionPath" value="${entity.resource.version.path}" />
+  </bean>
+
+  <bean id="jsonReader" class="org.onap.aai.validation.reader.JsonReader" />
+
+  <bean id="schemaVersionBean" class="org.onap.aai.setup.SchemaVersion">
+    <constructor-arg value="${schema.version.api.default}" />
+  </bean>
+
+  <bean id="ruleIndexingConfig" class="org.onap.aai.validation.config.RuleIndexingConfig">
+    <property name="indexedEvents" value="#{'${rule.indexing.events}'.split(',')}" />
+    <property name="excludedOxmValidationEvents" value="#{'${rule.indexing.exclude.oxm.validation}'.split(',')}" />
+    <property name="indexAttributes" value="#{'${rule.indexing.key.attributes}'.split(',')}" />
+    <property name="defaultIndexKey" value="${rule.indexing.default.key}" />
+  </bean>
+
+  <bean id="eventReader" class="org.onap.aai.validation.reader.EventReader">
+    <constructor-arg ref="eventReaderConfig" />
+    <constructor-arg ref="jsonReader" />
+    <constructor-arg ref="oxmReader" />
+  </bean>
+
+  <bean id="oxmReader" class="org.onap.aai.validation.reader.OxmReader" init-method="init">
+    <constructor-arg ref="nodeIngestor" />
+    <constructor-arg ref="schemaVersionBean" />
+  </bean>
+
+  <bean id="rulesConfigurationPath" class="java.nio.file.Paths" factory-method="get">
+    <constructor-arg value="${APP_HOME}/bundleconfig/etc/rules/" />
+    <constructor-arg>
+      <array />
+    </constructor-arg>
+  </bean>
+
+  <bean id="ruleDrivenValidator" class="org.onap.aai.validation.ruledriven.RuleDrivenValidator">
+    <constructor-arg ref="rulesConfigurationPath" />
+    <constructor-arg ref="oxmReader" />
+    <constructor-arg ref="eventReader" />
+    <constructor-arg ref="ruleIndexingConfig" />
+  </bean>
+
+</beans>
\ No newline at end of file