AAI-1523 Batch reformat aai-schema-ingest
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / EdgeRuleValidator.java
1 /** 
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.validation.edges;
22
23 import com.jayway.jsonpath.DocumentContext;
24
25 import java.util.*;
26
27 import org.onap.aai.edges.JsonIngestor;
28 import org.onap.aai.edges.TypeAlphabetizer;
29 import org.onap.aai.edges.enums.EdgeField;
30 import org.onap.aai.setup.ConfigTranslator;
31 import org.onap.aai.setup.SchemaVersion;
32 import org.onap.aai.validation.SchemaErrorStrategy;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * Runs all validations against the ingested schema
38  */
39 @Component
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;
48
49     @Autowired
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());
56         this.strat = strat;
57         this.fieldValidator = fieldValidator;
58         this.labelValidator = labelValidator;
59         this.containsValidator = containsValidator;
60         this.defaultsValidator = defaultsValidator;
61         this.typeValidator = typeValidator;
62     }
63
64     public boolean validate() {
65
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();
72
73             for (Map<String, String> rule : rules) {
74                 handleResult(fieldValidator.verifyFields(rule));
75                 nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()),
76                         rule.get(EdgeField.TO.toString())));
77             }
78
79             for (String nodeTypePair : nodeTypePairs) {
80                 handleResult(labelValidator.validate(nodeTypePair, ctxs));
81                 handleResult(containsValidator.validate(nodeTypePair, ctxs));
82                 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
83             }
84
85             handleResult(typeValidator.validate(nodeTypePairs, v));
86         }
87
88         return strat.isOK();
89     }
90
91     private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
92         List<Map<String, String>> rules = new ArrayList<>();
93
94         for (DocumentContext ctx : ctxs) {
95             rules.addAll(ctx.read("$.rules.*"));
96         }
97
98         return rules;
99     }
100
101     private void handleResult(String result) {
102         if (!"".equals(result)) {
103             strat.notifyOnError(result);
104         }
105     }
106
107     public String getErrorMsg() {
108         return strat.getErrorMsg();
109     }
110 }