Merge "Fix sonar issues in SchemaGenerator"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / genxsd / EdgeRuleSet.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.util.genxsd;
21
22 import com.jayway.jsonpath.DocumentContext;
23 import com.jayway.jsonpath.JsonPath;
24 import org.onap.aai.serialization.db.EdgeProperty;
25
26 import java.io.*;
27 import java.util.*;
28
29 public class EdgeRuleSet {
30         private File edgeFile;
31         private DocumentContext jsonContext;
32
33         public EdgeRuleSet(File edgeFile) throws IOException,FileNotFoundException {
34                 this.edgeFile = edgeFile;
35                 init();
36         }
37         public EdgeRuleSet(DocumentContext jsonContext) {
38                 this.jsonContext = jsonContext;
39         }
40         
41         public Collection<EdgeDescription> getEdgeRules( String nodeName ) 
42         {               
43                 String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]";
44                 String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]";
45                 Collection<EdgeDescription> fromEdges = getEdgeRulesFromJson( fromRulesPath, false );
46                 Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true );
47                 edges.addAll(fromEdges);
48                 return edges;
49         }
50         
51         public Collection<EdgeDescription> getEdgeRulesTO( String nodeName ) 
52         {               
53                 String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]";
54                 Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true );
55                 return edges;
56         }
57         
58         public Collection<EdgeDescription> getEdgeRulesFROM( String nodeName ) 
59         {               
60                 String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]";
61                 Collection<EdgeDescription> edges = getEdgeRulesFromJson( fromRulesPath, true );
62                 return edges;
63         }
64         
65         /**
66          * Guaranteed to at least return non null but empty collection of edge descriptions
67          * @param nodeName name of the vertex whose edge relationships to return
68          * @return collection of node neighbors based on DbEdgeRules
69         **/
70         public Collection<EdgeDescription> getEdgeRulesFromJson( String path, boolean skipMatch ) 
71         {
72
73                 ArrayList<EdgeDescription> result = new ArrayList<>();
74                 Iterator<Map<String, Object>> edgeRulesIterator;
75                 try {
76                         List<Map<String, Object>> inEdges = jsonContext.read(path);
77                         
78                         edgeRulesIterator = inEdges.iterator();
79                         Map<String, Object> edgeMap;
80                         String fromNode;
81                         String toNode;
82                         String direction;
83                         String multiplicity;
84                         String isParent;
85                         String deleteOtherV;
86                         String preventDelete;
87                         String description;
88                         EdgeDescription edgeDes;
89                         
90                         while( edgeRulesIterator.hasNext() ){
91                                 edgeMap = edgeRulesIterator.next();
92                                 fromNode = (String)edgeMap.get("from");
93                                 toNode = (String)edgeMap.get("to");
94                                 if ( skipMatch ) { 
95                                         if ( fromNode.equals(toNode)) {
96                                                 continue;
97                                         }
98                                 }
99                                 edgeDes = new EdgeDescription();
100                                 edgeDes.setRuleKey(fromNode + "|" + toNode);
101                                 edgeDes.setLabel((String)edgeMap.get("label"));
102                                 edgeDes.setTo((String)edgeMap.get("to"));
103                                 edgeDes.setFrom((String)edgeMap.get("from"));
104                                 direction = (String)edgeMap.get("direction");
105                                 edgeDes.setDirection(direction);
106                                 multiplicity = (String)edgeMap.get("multiplicity");
107                                 edgeDes.setMultiplicity(multiplicity);
108                                 isParent = (String)edgeMap.get(EdgeProperty.CONTAINS.toString());
109                                 if ( "${direction}".equals(isParent))  {
110                                         edgeDes.setType(EdgeDescription.LineageType.PARENT);
111                                 } else {
112                                         edgeDes.setType(EdgeDescription.LineageType.UNRELATED);
113                                 }
114                                 deleteOtherV = (String)edgeMap.get(EdgeProperty.DELETE_OTHER_V.toString());
115                                 edgeDes.setDeleteOtherV(deleteOtherV);
116                                 preventDelete = (String)edgeMap.get(EdgeProperty.PREVENT_DELETE.toString());
117                                 edgeDes.setPreventDelete(preventDelete);
118                                 description = (String)edgeMap.get(EdgeProperty.DESCRIPTION.toString());
119                                 edgeDes.setDescription(description);
120                                 
121                                 result.add(edgeDes);
122 //                              logger.debug("Edge: "+edgeDes.getRuleKey());
123                         }
124                 } catch (Exception ex) {
125                         ex.printStackTrace();
126                 }
127                 return result;
128                 
129         }
130
131         private void init() throws FileNotFoundException, IOException {
132                 InputStream is = null;
133                 Scanner scanner = null;
134                 String jsonEdges = null;
135                 try {
136                         is = new FileInputStream(edgeFile);
137                         scanner = new Scanner(is);
138                         jsonEdges = scanner.useDelimiter("\\Z").next();
139                 } catch (Exception e) {
140                         throw e;
141                 } finally {
142                         scanner.close();
143                         if (is != null) {
144                                 try {
145                                         is.close();
146                                 } catch (IOException e) {
147                                         throw e;
148                                 }
149                         }
150                 }
151                 jsonContext = JsonPath.parse(jsonEdges);
152         }
153         
154         public String preventDeleteRules(String objectName) {
155                 Collection<EdgeDescription> toEdges = getEdgeRulesTO(objectName);
156                 toEdges.addAll(getEdgeRulesFROM(objectName));
157 //              logger.debug("TO Edges count: "+toEdges.size()+" Object: "+objectName);
158                 String prevent=null;
159                 LinkedHashSet<String> preventDelete = new LinkedHashSet<String>();
160                 for (EdgeDescription ed : toEdges) {
161 //                      logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName);
162 //                      logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName);
163                         if(ed.getPreventDelete().equals("IN") && ed.getTo().equals(objectName)) {
164                                 preventDelete.add(ed.getFrom().toUpperCase());
165                         }
166                         if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) {
167                                 preventDelete.add(ed.getTo().toUpperCase());
168                         }
169                 }
170                 if(preventDelete.size() > 0) {
171                         prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete);
172 //                      logger.debug(prevent);
173                 }
174                 return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent)+((prevent == null) ? "" : "\n");              
175 //              return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null) ? "" : "\n");
176         }
177         
178         public String fromDeleteRules(String objectName) {
179                 Collection<EdgeDescription> fromEdges = getEdgeRulesFROM(objectName);
180                 LinkedHashSet<String> preventDelete = new LinkedHashSet <String>();
181                 String prevent=null;
182                 String also=null;
183                 for (EdgeDescription ed : fromEdges) {
184 //                      logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName);
185 //                      logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName);
186                         if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) {
187                                 preventDelete.add(ed.getTo().toUpperCase());
188                         }
189                 }
190                 if(preventDelete.size() > 0) {
191                         prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete);
192 //                      logger.debug(prevent);
193                 }
194                 return String.join((prevent == null || also == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null && also == null) ? "" : "\n");
195         }
196 }