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