Update schema ingest library call schema service
[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 import org.onap.aai.edges.JsonIngestor;
25 import org.onap.aai.edges.TypeAlphabetizer;
26 import org.onap.aai.edges.enums.EdgeField;
27
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;
33
34 import java.util.*;
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()), rule.get(EdgeField.TO.toString())));
76                         }
77                         
78                         for (String nodeTypePair : nodeTypePairs) {
79                                 handleResult(labelValidator.validate(nodeTypePair, ctxs));
80                                 handleResult(containsValidator.validate(nodeTypePair, ctxs)); 
81                                 handleResult(defaultsValidator.validate(nodeTypePair, ctxs));
82                         }
83                         
84                         handleResult(typeValidator.validate(nodeTypePairs, v));
85                 }
86
87                 return strat.isOK();
88         }
89         
90         private List<Map<String, String>> collectRules(List<DocumentContext> ctxs) {
91                 List<Map<String, String>> rules = new ArrayList<>();
92                 
93                 for (DocumentContext ctx : ctxs) {
94                         rules.addAll(ctx.read("$.rules.*"));
95                 }
96                 
97                 return rules;
98         }
99         
100         private void handleResult(String result) {
101                 if (!"".equals(result)) {
102                         strat.notifyOnError(result);
103                 }
104         }
105         
106         public String getErrorMsg() {
107                 return strat.getErrorMsg();
108         }
109 }