Remove duplicate service-instance to l3-network edge rule
[aai/schema-service.git] / aai-schema / src / test / java / org / onap / aai / schema / ValidateEdgeRulesTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.schema;
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.filefilter.DirectoryFileFilter;
24 import org.apache.commons.io.filefilter.RegexFileFilter;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.parser.JSONParser;
28 import org.json.simple.parser.ParseException;
29 import org.junit.Test;
30
31 import java.io.File;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.*;
37 import java.util.stream.Collectors;
38
39 import static org.junit.Assert.fail;
40
41 public class ValidateEdgeRulesTest {
42
43     private static final String DBEDGERULES_RULES = "rules";
44     private static final String DBEDGERULES_FROM = "from";
45     private static final String DBEDGERULES_TO = "to";
46     private static final String DBEDGERULES_DIRECTION = "direction";
47     private static final String DBEDGERULES_LABEL = "label";
48     private static final String DBEDGERULES_CONTAINS_OTHER_V = "contains-other-v";
49     private static final String DBEDGERULES_OUT = "OUT";
50     private static final String DBEDGERULES_IN = "IN";
51
52     @Test
53     public void testOnlyOneDefaultPerEdgeRuleBetweenTwoNodetypes() throws IOException, ParseException {
54         Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
55         List<File> subDirs = Arrays.asList(currentRelativePath.toFile().listFiles(File::isDirectory));
56         List<String> multipleDefaultsPerList = new ArrayList<>();
57         for (File subDir : subDirs) {
58
59             String edgeRule = subDir.getAbsolutePath() + "/dbedgerules";
60             File[] edgeRules = new File(edgeRule).listFiles(File::isDirectory);
61             List<String> dbEdgeRulesList = Arrays.stream(edgeRules).map(File::getAbsolutePath).collect(Collectors.toList());
62
63
64             List<File> dbEdgeRulesFileList = new ArrayList<>();
65
66             dbEdgeRulesList.forEach(s ->
67                 FileUtils.listFiles(new File(s), new RegexFileFilter(".*\\.json"), DirectoryFileFilter.DIRECTORY)
68                     .stream()
69                     .filter(file -> file.getAbsolutePath().contains("DbEdgeRules"))
70                     .forEach(dbEdgeRulesFileList::add));
71
72             JSONParser jsonParser = new JSONParser();
73
74             for (File file : dbEdgeRulesFileList) {
75                 FileReader reader = new FileReader(file);
76                 // Map the dbEdgeRules json file into a HashMap for reference
77                 Map<String, Integer> dbEdgeRules = new HashMap<>();
78                 // Read JSON file. Expecting JSON file to read an object with a JSONArray names "rules"
79                 JSONObject jsonObj = (JSONObject) jsonParser.parse(reader);
80                 JSONArray rules = (JSONArray) jsonObj.get(DBEDGERULES_RULES);
81                 for (int i = 0; i < rules.size(); i++) {
82                     JSONObject rule = (JSONObject) rules.get(i);
83                     String fromNode = rule.get(DBEDGERULES_FROM).toString();
84                     String toNode = rule.get(DBEDGERULES_TO).toString();
85                     String direction = rule.get(DBEDGERULES_DIRECTION).toString();
86                     String label = rule.get(DBEDGERULES_LABEL).toString();
87                     String containsOtherV = rule.get(DBEDGERULES_CONTAINS_OTHER_V).toString();
88                     String isDefault = (rule.get("default") != null) ? rule.get("default").toString() : "false";
89
90                     // special case - cvlan-tag should be replaced with cvlan-tag-entry
91                     if (fromNode.equals("cvlan-tag"))
92                         fromNode = "cvlan-tag-entry";
93                     if (toNode.equals("cvlan-tag"))
94                         toNode = "cvlan-tag-entry";
95                     if (containsOtherV.equals("!${direction}")) {
96                         if (direction.equals(DBEDGERULES_IN)) {
97                             direction = DBEDGERULES_OUT;
98                         } else if (direction.equals(DBEDGERULES_OUT)) {
99                             direction = DBEDGERULES_IN;
100                         }
101                     }
102                     dbEdgeRulesMapPut(dbEdgeRules, fromNode, toNode, direction, label, isDefault);
103                 }
104
105                 for(Map.Entry<String, Integer> entry : dbEdgeRules.entrySet()){
106                     String key = entry.getKey();
107                     if(entry.getValue() > 1){
108                         multipleDefaultsPerList.add("\n" + file.getAbsoluteFile() + " " + key + " count: " + entry.getValue());
109                     }
110                 }
111             }
112
113         }
114
115         if(!multipleDefaultsPerList.isEmpty()){
116             fail(multipleDefaultsPerList.stream().collect(Collectors.joining()));
117         }
118     }
119
120     /**
121      * Creating a hashmap to map what child nodes are associated to which parent nodes
122      * according to the dbEdgeRules json files. A HashMap was chosen for the value of the map for O(1) lookup time.
123      * @param from this variable will act as the key or value depending on the direction
124      * @param to this variable will act as the key or value depending on the direction
125      * @param direction dictates the direction of which vertex is dependent on which
126      * @return The map returned will act as a dictionary to keep track of the parent nodes. Value of the map is a hashmap to help handle collision of multiple children to one parent
127      */
128     private Map<String, Integer> dbEdgeRulesMapPut(Map<String, Integer> dbEdgeRules, String from, String to, String direction, String label, String isDefault) {
129         if (isStringEmpty(from) || isStringEmpty(to) || isStringEmpty(direction))
130             return dbEdgeRules;
131
132
133         String temp;
134
135         if(from.compareTo(to) > 0){
136             temp = from;
137             from = to;
138             to = temp;
139         }
140
141         String edgeInfo = String.format("%s%s%s[%s]%s", from, ((direction.equals("OUT")) ? "->": "<-"), to, label, isDefault);
142
143         if ("false".equals(isDefault)) {
144             return dbEdgeRules;
145         }
146
147         if(dbEdgeRules.containsKey(edgeInfo)){
148             dbEdgeRules.put(edgeInfo, dbEdgeRules.get(edgeInfo) + 1);
149         } else {
150             dbEdgeRules.put(edgeInfo, 1);
151         }
152
153         return dbEdgeRules;
154     }
155
156     private boolean isStringEmpty(String s) {
157         return (s == null || s.isEmpty()) ? true : false;
158     }
159 }