2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.validation.edges;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
29 import org.onap.aai.edges.JsonIngestor;
30 import org.onap.aai.edges.TypeAlphabetizer;
31 import org.onap.aai.edges.enums.EdgeField;
32 import org.onap.aai.setup.ConfigTranslator;
33 import org.onap.aai.setup.SchemaVersion;
34 import org.onap.aai.validation.SchemaErrorStrategy;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
38 import com.jayway.jsonpath.DocumentContext;
41 * Runs all validations against the ingested schema
44 public class EdgeRuleValidator {
45 private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap;
46 private final SchemaErrorStrategy strat;
47 protected final EdgeFieldsValidationModule fieldValidator;
48 protected final UniqueLabelValidationModule labelValidator;
49 protected final SingleContainmentValidationModule containsValidator;
50 protected final CousinDefaultingValidationModule defaultsValidator;
51 protected final NodeTypesValidationModule typeValidator;
54 public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat,
55 EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator,
56 SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator,
57 NodeTypesValidationModule typeValidator) {
58 this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles());
60 this.fieldValidator = fieldValidator;
61 this.labelValidator = labelValidator;
62 this.containsValidator = containsValidator;
63 this.defaultsValidator = defaultsValidator;
64 this.typeValidator = typeValidator;
67 public boolean validate() {
69 for (Map.Entry<SchemaVersion, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) {
70 SchemaVersion v = verEntry.getKey();
71 List<DocumentContext> ctxs = verEntry.getValue();
72 List<Map<String, String>> rules = collectRules(ctxs);
73 Set<String> nodeTypePairs = new HashSet<>();
74 TypeAlphabetizer alpher = new TypeAlphabetizer();
76 for (Map<String, String> rule : rules) {
77 handleResult(fieldValidator.verifyFields(rule));
78 nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()), rule.get(EdgeField.TO.toString())));
81 for (String nodeTypePair : nodeTypePairs) {
82 handleResult(labelValidator.validate(nodeTypePair, ctxs));
83 handleResult(containsValidator.validate(nodeTypePair, ctxs));
84 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
87 handleResult(typeValidator.validate(nodeTypePairs, v));
93 private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
94 List<Map<String, String>> rules = new ArrayList<>();
96 for (DocumentContext ctx : ctxs) {
97 rules.addAll(ctx.read("$.rules.*"));
103 private void handleResult(String result) {
104 if (!"".equals(result)) {
105 strat.notifyOnError(result);
109 public String getErrorMsg() {
110 return strat.getErrorMsg();