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 com.jayway.jsonpath.DocumentContext;
24 import org.onap.aai.edges.JsonIngestor;
25 import org.onap.aai.edges.TypeAlphabetizer;
26 import org.onap.aai.edges.enums.EdgeField;
28 import org.onap.aai.setup.ConfigTranslator;
29 import org.onap.aai.setup.SchemaVersion;
30 import org.onap.aai.validation.SchemaErrorStrategy;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
37 * Runs all validations against the ingested schema
40 public class EdgeRuleValidator {
41 private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap;
42 private final SchemaErrorStrategy strat;
43 protected final EdgeFieldsValidationModule fieldValidator;
44 protected final UniqueLabelValidationModule labelValidator;
45 protected final SingleContainmentValidationModule containsValidator;
46 protected final CousinDefaultingValidationModule defaultsValidator;
47 protected final NodeTypesValidationModule typeValidator;
50 public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat,
51 EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator,
52 SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator,
53 NodeTypesValidationModule typeValidator) {
54 //TODO - Need to change this to use files/schemaservice
55 this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles());
57 this.fieldValidator = fieldValidator;
58 this.labelValidator = labelValidator;
59 this.containsValidator = containsValidator;
60 this.defaultsValidator = defaultsValidator;
61 this.typeValidator = typeValidator;
64 public boolean validate() {
66 for (Map.Entry<SchemaVersion, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) {
67 SchemaVersion v = verEntry.getKey();
68 List<DocumentContext> ctxs = verEntry.getValue();
69 List<Map<String, String>> rules = collectRules(ctxs);
70 Set<String> nodeTypePairs = new HashSet<>();
71 TypeAlphabetizer alpher = new TypeAlphabetizer();
73 for (Map<String, String> rule : rules) {
74 handleResult(fieldValidator.verifyFields(rule));
75 nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()), rule.get(EdgeField.TO.toString())));
78 for (String nodeTypePair : nodeTypePairs) {
79 handleResult(labelValidator.validate(nodeTypePair, ctxs));
80 handleResult(containsValidator.validate(nodeTypePair, ctxs));
81 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
84 handleResult(typeValidator.validate(nodeTypePairs, v));
90 private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
91 List<Map<String, String>> rules = new ArrayList<>();
93 for (DocumentContext ctx : ctxs) {
94 rules.addAll(ctx.read("$.rules.*"));
100 private void handleResult(String result) {
101 if (!"".equals(result)) {
102 strat.notifyOnError(result);
106 public String getErrorMsg() {
107 return strat.getErrorMsg();