5f6a9646497f5238e8b9eb4227164a54d57f043e
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / SingleContainmentValidationModule.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.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.onap.aai.edges.EdgeRuleQuery;
30 import org.onap.aai.edges.enums.EdgeType;
31
32 /**
33  * Validates that the given node type pair has at most one containment relationship
34  * in their edge rules.
35  * 
36  */
37 public class SingleContainmentValidationModule {
38
39     /**
40      * Validates that the given node type pair has at most one containment relationship
41      * in their edge rules.
42      * 
43      * @param String nodeTypePair - pair of A&AI node types in the form "typeA|typeB"
44      * @param List<DocumentContext> ctxs - the ingested json to validate
45      * @return empty string if passes, else appropriate error message
46      */
47     public String validate(String nodeTypePair, List<DocumentContext> ctxs) {
48         String[] types = nodeTypePair.split("\\|");
49         EdgeRuleQuery lookup = new EdgeRuleQuery.Builder(types[0], types[1]).edgeType(EdgeType.TREE).build();
50         List<Map<String, String>> rules = new ArrayList<>();
51         for (DocumentContext ctx : ctxs) {
52             rules.addAll(ctx.read("$.rules.[?]", lookup.getFilter()));
53         }
54
55         if (rules.isEmpty() || rules.size() == 1) {
56             return "";
57         } else { // had more than one containment relationship for the pair
58             return "Pair " + nodeTypePair + " has multiple containment rules. Only one allowed.";
59         }
60     }
61 }