35d74667eca43483b08cd322fa77597bf8309e5b
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / UniqueLabelValidationModule.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.EdgeRuleQuery;
25 import org.onap.aai.edges.EdgeRuleQuery.Builder;
26 import org.onap.aai.edges.enums.EdgeField;
27
28 import java.util.*;
29
30 /**
31  * Applies label validation rules
32  *
33  */
34 public class UniqueLabelValidationModule {
35         
36         /**
37          * Validates that the given pair of node types have no duplicate labels in
38          * their edge rules
39          * 
40          * @param String nodeTypePair - of the form "typeA|typeB"
41          * @param List<DocumentContext> ctxs - the edge rule json to pull rules from
42          *                      (ie all files for one version)
43          * @return empty string if no errors, else string error message
44          */
45         public String validate(String nodeTypePair, List<DocumentContext> ctxs) {
46                 String[] types = nodeTypePair.split("\\|");
47                 EdgeRuleQuery lookup = new Builder(types[0], types[1]).build();
48                 
49                 List<Map<String, String>> rules = new ArrayList<>();
50                 for (DocumentContext ctx : ctxs) {
51                         rules.addAll(ctx.read("$.rules.[?]", lookup.getFilter()));
52                 }
53                 
54                 Set<String> labelsSeen = new HashSet<>();
55                 for (Map<String, String> rule : rules) {
56                         String label = rule.get(EdgeField.LABEL.toString());
57                         if (labelsSeen.contains(label)) {
58                                 return "Pair " + nodeTypePair + " has multiple rules using the same label: " + label + 
59                                                 ". Every rule between the same node type pair must have a unique label.";
60                         } else {
61                                 labelsSeen.add(label);
62                         }
63                 }
64                 return "";
65         }
66 }