309d894569162dc5fb46fdef22ad8537d3a14810
[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 java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
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;
37
38 import com.jayway.jsonpath.DocumentContext;
39
40 /**
41  * Runs all validations against the ingested schema
42  */
43 @Component
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;
52         
53         @Autowired
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());
59                 this.strat = strat;
60                 this.fieldValidator = fieldValidator;
61                 this.labelValidator = labelValidator;
62                 this.containsValidator = containsValidator;
63                 this.defaultsValidator = defaultsValidator;
64                 this.typeValidator = typeValidator;
65         }
66         
67         public boolean validate() {
68                 
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();
75                         
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())));
79                         }
80                         
81                         for (String nodeTypePair : nodeTypePairs) {
82                                 handleResult(labelValidator.validate(nodeTypePair, ctxs));
83                                 handleResult(containsValidator.validate(nodeTypePair, ctxs)); 
84                                 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
85                         }
86                         
87                         handleResult(typeValidator.validate(nodeTypePairs, v));
88                 }
89
90                 return strat.isOK();
91         }
92         
93         private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
94                 List<Map<String, String>> rules = new ArrayList<>();
95                 
96                 for (DocumentContext ctx : ctxs) {
97                         rules.addAll(ctx.read("$.rules.*"));
98                 }
99                 
100                 return rules;
101         }
102         
103         private void handleResult(String result) {
104                 if (!"".equals(result)) {
105                         strat.notifyOnError(result);
106                 }
107         }
108         
109         public String getErrorMsg() {
110                 return strat.getErrorMsg();
111         }
112 }