added schema validation tools
[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 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
35 import com.jayway.jsonpath.DocumentContext;
36
37 /**
38  * Applies label validation rules
39  *
40  */
41 public class UniqueLabelValidationModule {
42         
43         /**
44          * Validates that the given pair of node types have no duplicate labels in
45          * their edge rules
46          * 
47          * @param String nodeTypePair - of the form "typeA|typeB"
48          * @param List<DocumentContext> ctxs - the edge rule json to pull rules from
49          *                      (ie all files for one version)
50          * @return empty string if no errors, else string error message
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]).build();
55                 
56                 List<Map<String, String>> rules = new ArrayList<>();
57                 for (DocumentContext ctx : ctxs) {
58                         rules.addAll(ctx.read("$.rules.[?]", lookup.getFilter()));
59                 }
60                 
61                 Set<String> labelsSeen = new HashSet<>();
62                 for (Map<String, String> rule : rules) {
63                         String label = rule.get(EdgeField.LABEL.toString());
64                         if (labelsSeen.contains(label)) {
65                                 return "Pair " + nodeTypePair + " has multiple rules using the same label: " + label + 
66                                                 ". Every rule between the same node type pair must have a unique label.";
67                         } else {
68                                 labelsSeen.add(label);
69                         }
70                 }
71                 return "";
72         }
73 }