1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.aai.validation.edges;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.onap.aai.edges.JsonIngestor;
import org.onap.aai.edges.TypeAlphabetizer;
import org.onap.aai.edges.enums.EdgeField;
import org.onap.aai.setup.ConfigTranslator;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.validation.SchemaErrorStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.jayway.jsonpath.DocumentContext;
/**
* Runs all validations against the ingested schema
*/
@Component
public class EdgeRuleValidator {
private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap;
private final SchemaErrorStrategy strat;
protected final EdgeFieldsValidationModule fieldValidator;
protected final UniqueLabelValidationModule labelValidator;
protected final SingleContainmentValidationModule containsValidator;
protected final CousinDefaultingValidationModule defaultsValidator;
protected final NodeTypesValidationModule typeValidator;
@Autowired
public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat,
EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator,
SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator,
NodeTypesValidationModule typeValidator) {
this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles());
this.strat = strat;
this.fieldValidator = fieldValidator;
this.labelValidator = labelValidator;
this.containsValidator = containsValidator;
this.defaultsValidator = defaultsValidator;
this.typeValidator = typeValidator;
}
public boolean validate() {
for (Map.Entry<SchemaVersion, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) {
SchemaVersion v = verEntry.getKey();
List<DocumentContext> ctxs = verEntry.getValue();
List<Map<String, String>> rules = collectRules(ctxs);
Set<String> nodeTypePairs = new HashSet<>();
TypeAlphabetizer alpher = new TypeAlphabetizer();
for (Map<String, String> rule : rules) {
handleResult(fieldValidator.verifyFields(rule));
nodeTypePairs.add(alpher.buildAlphabetizedKey(rule.get(EdgeField.FROM.toString()), rule.get(EdgeField.TO.toString())));
}
for (String nodeTypePair : nodeTypePairs) {
handleResult(labelValidator.validate(nodeTypePair, ctxs));
handleResult(containsValidator.validate(nodeTypePair, ctxs));
handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
}
handleResult(typeValidator.validate(nodeTypePairs, v));
}
return strat.isOK();
}
private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
List<Map<String, String>> rules = new ArrayList<>();
for (DocumentContext ctx : ctxs) {
rules.addAll(ctx.read("$.rules.*"));
}
return rules;
}
private void handleResult(String result) {
if (!"".equals(result)) {
strat.notifyOnError(result);
}
}
public String getErrorMsg() {
return strat.getErrorMsg();
}
}
|