added schema validation tools
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.validation.edges;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31
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;
40
41 import com.jayway.jsonpath.DocumentContext;
42
43 /**
44  * Runs all validations against the ingested schema
45  */
46 @Component
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;
55         
56         @Autowired
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());
62                 this.strat = strat;
63                 this.fieldValidator = fieldValidator;
64                 this.labelValidator = labelValidator;
65                 this.containsValidator = containsValidator;
66                 this.defaultsValidator = defaultsValidator;
67                 this.typeValidator = typeValidator;
68         }
69         
70         public boolean validate() {
71                 
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();
78                         
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())));
82                         }
83                         
84                         for (String nodeTypePair : nodeTypePairs) {
85                                 handleResult(labelValidator.validate(nodeTypePair, ctxs));
86                                 handleResult(containsValidator.validate(nodeTypePair, ctxs)); 
87                                 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
88                         }
89                         
90                         handleResult(typeValidator.validate(nodeTypePairs, v));
91                 }
92                 
93                 return strat.isOK();
94         }
95         
96         private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
97                 List<Map<String, String>> rules = new ArrayList<>();
98                 
99                 for (DocumentContext ctx : ctxs) {
100                         rules.addAll(ctx.read("$.rules.*"));
101                 }
102                 
103                 return rules;
104         }
105         
106         private void handleResult(String result) {
107                 if (!"".equals(result)) {
108                         strat.notifyOnError(result);
109                 }
110         }
111         
112         public String getErrorMsg() {
113                 return strat.getErrorMsg();
114         }
115 }