2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.util.genxsd;
22 import com.jayway.jsonpath.DocumentContext;
23 import com.jayway.jsonpath.JsonPath;
24 import org.onap.aai.serialization.db.EdgeProperty;
29 public class EdgeRuleSet {
30 private File edgeFile;
31 private DocumentContext jsonContext;
33 public EdgeRuleSet(File edgeFile) throws IOException,FileNotFoundException {
34 this.edgeFile = edgeFile;
37 public EdgeRuleSet(DocumentContext jsonContext) {
38 this.jsonContext = jsonContext;
41 public Collection<EdgeDescription> getEdgeRules( String nodeName )
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);
51 public Collection<EdgeDescription> getEdgeRulesTO( String nodeName )
53 String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]";
54 Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true );
58 public Collection<EdgeDescription> getEdgeRulesFROM( String nodeName )
60 String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]";
61 Collection<EdgeDescription> edges = getEdgeRulesFromJson( fromRulesPath, true );
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
70 public Collection<EdgeDescription> getEdgeRulesFromJson( String path, boolean skipMatch )
73 ArrayList<EdgeDescription> result = new ArrayList<>();
74 Iterator<Map<String, Object>> edgeRulesIterator;
76 List<Map<String, Object>> inEdges = jsonContext.read(path);
78 edgeRulesIterator = inEdges.iterator();
79 Map<String, Object> edgeMap;
88 EdgeDescription edgeDes;
90 while( edgeRulesIterator.hasNext() ){
91 edgeMap = edgeRulesIterator.next();
92 fromNode = (String)edgeMap.get("from");
93 toNode = (String)edgeMap.get("to");
95 if ( fromNode.equals(toNode)) {
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);
112 edgeDes.setType(EdgeDescription.LineageType.UNRELATED);
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);
122 // logger.debug("Edge: "+edgeDes.getRuleKey());
124 } catch (Exception ex) {
125 ex.printStackTrace();
131 private void init() throws FileNotFoundException, IOException {
132 InputStream is = null;
133 Scanner scanner = null;
134 String jsonEdges = null;
136 is = new FileInputStream(edgeFile);
137 scanner = new Scanner(is);
138 jsonEdges = scanner.useDelimiter("\\Z").next();
139 } catch (Exception e) {
146 } catch (IOException e) {
151 jsonContext = JsonPath.parse(jsonEdges);
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);
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());
166 if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) {
167 preventDelete.add(ed.getTo().toUpperCase());
170 if(preventDelete.size() > 0) {
171 prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete);
172 // logger.debug(prevent);
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");
178 public String fromDeleteRules(String objectName) {
179 Collection<EdgeDescription> fromEdges = getEdgeRulesFROM(objectName);
180 LinkedHashSet<String> preventDelete = new LinkedHashSet <String>();
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());
190 if(preventDelete.size() > 0) {
191 prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete);
192 // logger.debug(prevent);
194 return String.join((prevent == null || also == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null && also == null) ? "" : "\n");