2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.validation.edges;
23 import com.jayway.jsonpath.DocumentContext;
24 import org.onap.aai.edges.EdgeRuleQuery;
25 import org.onap.aai.edges.EdgeRuleQuery.Builder;
26 import org.onap.aai.edges.enums.EdgeField;
27 import org.onap.aai.edges.enums.EdgeType;
32 * Validates that in the collection of cousin rules between a given node type pair,
33 * there is exactly 1 set default=true.
35 public class CousinDefaultingValidationModule {
38 * Validates that in the collection of cousin rules between a given node type pair,
39 * there is exactly 1 set default=true.
41 * @param String nodeTypePair - pair of A&AI node types in the form "typeA|typeB"
42 * @param List<DocumentContext> ctxs - the ingested json schema to validate
43 * @return empty string if ok, appropriate error message otherwise
45 public String validate(String nodeTypePair, List<DocumentContext> ctxs) {
46 String[] types = nodeTypePair.split("\\|");
47 EdgeRuleQuery lookup = new Builder(types[0], types[1]).edgeType(EdgeType.COUSIN).build();
48 List<Map<String, String>> rules = new ArrayList<>();
49 for (DocumentContext ctx : ctxs) {
50 rules.addAll(ctx.read("$.rules.[?]", lookup.getFilter()));
53 if (rules.isEmpty()) {
54 return ""; //bc irrelevant check
58 Set<String> defLabels = new HashSet<>();
59 for (Map<String, String> rule : rules) {
60 if ("true".equals(rule.get(EdgeField.DEFAULT.toString()))) {
62 defLabels.add(rule.get(EdgeField.LABEL.toString()));
66 StringBuilder errorBase = new StringBuilder().append("Pair ").append(nodeTypePair).append(" must have exactly one cousin rule set as default. ");
67 if (defaultCount == 1) {
69 } else if (defaultCount == 0){
70 errorBase.append("None set.");
71 return errorBase.toString();
73 errorBase.append("Multiple set, see labels: ");
74 for (String label : defLabels) {
75 errorBase.append(label).append(" ");
77 return errorBase.toString();