added schema validation tools
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / CousinDefaultingValidationModule.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.Set;
30
31 import org.onap.aai.edges.EdgeRuleQuery;
32 import org.onap.aai.edges.EdgeRuleQuery.Builder;
33 import org.onap.aai.edges.enums.EdgeField;
34 import org.onap.aai.edges.enums.EdgeType;
35
36 import com.jayway.jsonpath.DocumentContext;
37
38 /**
39  * Validates that in the collection of cousin rules between a given node type pair,
40  * there is exactly 1 set default=true. 
41  */
42 public class CousinDefaultingValidationModule {
43
44         /**
45          * Validates that in the collection of cousin rules between a given node type pair,
46          * there is exactly 1 set default=true. 
47          * 
48          * @param String nodeTypePair - pair of A&AI node types in the form "typeA|typeB"
49          * @param List<DocumentContext> ctxs - the ingested json schema to validate
50          * @return empty string if ok, appropriate error message otherwise
51          */
52         public String validate(String nodeTypePair, List<DocumentContext> ctxs) {
53                 String[] types = nodeTypePair.split("\\|");
54                 EdgeRuleQuery lookup = new EdgeRuleQuery.Builder(types[0], types[1]).edgeType(EdgeType.COUSIN).build();
55                 List<Map<String, String>> rules = new ArrayList<>();
56                 for (DocumentContext ctx : ctxs) {
57                         rules.addAll(ctx.read("$.rules.[?]", lookup.getFilter()));
58                 }
59                 
60                 if (rules.isEmpty()) {
61                         return ""; //bc irrelevant check
62                 }
63                 
64                 int defaultCount = 0;
65                 Set<String> defLabels = new HashSet<>();
66                 for (Map<String, String> rule : rules) {
67                         if ("true".equals(rule.get(EdgeField.DEFAULT.toString()))) {
68                                 defaultCount++;
69                                 defLabels.add(rule.get(EdgeField.LABEL.toString()));
70                         }
71                 }
72                 
73                 StringBuilder errorBase = new StringBuilder().append("Pair ").append(nodeTypePair).append(" must have exactly one cousin rule set as default. ");
74                 if (defaultCount == 1) {
75                         return "";
76                 } else if (defaultCount == 0){
77                         errorBase.append("None set.");
78                         return errorBase.toString();
79                 } else {
80                         errorBase.append("Multiple set, see labels: ");
81                         for (String label : defLabels) {
82                                 errorBase.append(label).append(" ");
83                         }
84                         return errorBase.toString();
85                 }
86         }
87 }