added schema validation tools
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / EdgeRuleValidator.java
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java
new file mode 100644 (file)
index 0000000..34c603a
--- /dev/null
@@ -0,0 +1,115 @@
+/** 
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017 AT&T 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=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ */
+
+package org.onap.aai.validation.edges;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.onap.aai.edges.JsonIngestor;
+import org.onap.aai.edges.TypeAlphabetizer;
+import org.onap.aai.edges.enums.EdgeField;
+import org.onap.aai.setup.ConfigTranslator;
+import org.onap.aai.setup.Version;
+import org.onap.aai.validation.SchemaErrorStrategy;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import com.jayway.jsonpath.DocumentContext;
+
+/**
+ * Runs all validations against the ingested schema
+ */
+@Component
+public class EdgeRuleValidator {
+       private Map<Version, List<DocumentContext>> versionJsonFilesMap;
+       private final SchemaErrorStrategy strat;
+       protected final EdgeFieldsValidationModule fieldValidator;
+       protected final UniqueLabelValidationModule labelValidator;
+       protected final SingleContainmentValidationModule containsValidator;
+       protected final CousinDefaultingValidationModule defaultsValidator;
+       protected final NodeTypesValidationModule typeValidator;
+       
+       @Autowired
+       public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat,
+                       EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator, 
+                       SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator, 
+                       NodeTypesValidationModule typeValidator) {
+               this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles());
+               this.strat = strat;
+               this.fieldValidator = fieldValidator;
+               this.labelValidator = labelValidator;
+               this.containsValidator = containsValidator;
+               this.defaultsValidator = defaultsValidator;
+               this.typeValidator = typeValidator;
+       }
+       
+       public boolean validate() {
+               
+               for (Entry<Version, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) {
+                       Version v = verEntry.getKey();
+                       List<DocumentContext> ctxs = verEntry.getValue();
+                       List<Map<String, String>> rules = collectRules(ctxs);
+                       Set<String> nodeTypePairs = new HashSet<>();
+                       TypeAlphabetizer alpher = new TypeAlphabetizer();
+                       
+                       for (Map<String, String> rule : rules) {
+                               handleResult(fieldValidator.verifyFields(rule));
+                               nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()), rule.get(EdgeField.TO.toString())));
+                       }
+                       
+                       for (String nodeTypePair : nodeTypePairs) {
+                               handleResult(labelValidator.validate(nodeTypePair, ctxs));
+                               handleResult(containsValidator.validate(nodeTypePair, ctxs)); 
+                               handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
+                       }
+                       
+                       handleResult(typeValidator.validate(nodeTypePairs, v));
+               }
+               
+               return strat.isOK();
+       }
+       
+       private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
+               List<Map<String, String>> rules = new ArrayList<>();
+               
+               for (DocumentContext ctx : ctxs) {
+                       rules.addAll(ctx.read("$.rules.*"));
+               }
+               
+               return rules;
+       }
+       
+       private void handleResult(String result) {
+               if (!"".equals(result)) {
+                       strat.notifyOnError(result);
+               }
+       }
+       
+       public String getErrorMsg() {
+               return strat.getErrorMsg();
+       }
+}