2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.validation.edges;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.Map.Entry;
32 import org.onap.aai.edges.JsonIngestor;
33 import org.onap.aai.edges.TypeAlphabetizer;
34 import org.onap.aai.edges.enums.EdgeField;
35 import org.onap.aai.setup.ConfigTranslator;
36 import org.onap.aai.setup.Version;
37 import org.onap.aai.validation.SchemaErrorStrategy;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
41 import com.jayway.jsonpath.DocumentContext;
44 * Runs all validations against the ingested schema
47 public class EdgeRuleValidator {
48 private Map<Version, List<DocumentContext>> versionJsonFilesMap;
49 private final SchemaErrorStrategy strat;
50 protected final EdgeFieldsValidationModule fieldValidator;
51 protected final UniqueLabelValidationModule labelValidator;
52 protected final SingleContainmentValidationModule containsValidator;
53 protected final CousinDefaultingValidationModule defaultsValidator;
54 protected final NodeTypesValidationModule typeValidator;
57 public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat,
58 EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator,
59 SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator,
60 NodeTypesValidationModule typeValidator) {
61 this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles());
63 this.fieldValidator = fieldValidator;
64 this.labelValidator = labelValidator;
65 this.containsValidator = containsValidator;
66 this.defaultsValidator = defaultsValidator;
67 this.typeValidator = typeValidator;
70 public boolean validate() {
72 for (Entry<Version, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) {
73 Version v = verEntry.getKey();
74 List<DocumentContext> ctxs = verEntry.getValue();
75 List<Map<String, String>> rules = collectRules(ctxs);
76 Set<String> nodeTypePairs = new HashSet<>();
77 TypeAlphabetizer alpher = new TypeAlphabetizer();
79 for (Map<String, String> rule : rules) {
80 handleResult(fieldValidator.verifyFields(rule));
81 nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()), rule.get(EdgeField.TO.toString())));
84 for (String nodeTypePair : nodeTypePairs) {
85 handleResult(labelValidator.validate(nodeTypePair, ctxs));
86 handleResult(containsValidator.validate(nodeTypePair, ctxs));
87 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
90 handleResult(typeValidator.validate(nodeTypePairs, v));
96 private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
97 List<Map<String, String>> rules = new ArrayList<>();
99 for (DocumentContext ctx : ctxs) {
100 rules.addAll(ctx.read("$.rules.*"));
106 private void handleResult(String result) {
107 if (!"".equals(result)) {
108 strat.notifyOnError(result);
112 public String getErrorMsg() {
113 return strat.getErrorMsg();