/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.aai.util.genxsd; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import org.onap.aai.serialization.db.EdgeProperty; import java.io.*; import java.util.*; public class EdgeRuleSet { private File edgeFile; private DocumentContext jsonContext; public EdgeRuleSet(File edgeFile) throws IOException,FileNotFoundException { this.edgeFile = edgeFile; init(); } public EdgeRuleSet(DocumentContext jsonContext) { this.jsonContext = jsonContext; } public Collection getEdgeRules( String nodeName ) { String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]"; String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]"; Collection fromEdges = getEdgeRulesFromJson( fromRulesPath, false ); Collection edges = getEdgeRulesFromJson( toRulesPath, true ); edges.addAll(fromEdges); return edges; } public Collection getEdgeRulesTO( String nodeName ) { String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]"; Collection edges = getEdgeRulesFromJson( toRulesPath, true ); return edges; } public Collection getEdgeRulesFROM( String nodeName ) { String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]"; Collection edges = getEdgeRulesFromJson( fromRulesPath, true ); return edges; } /** * Guaranteed to at least return non null but empty collection of edge descriptions * @param nodeName name of the vertex whose edge relationships to return * @return collection of node neighbors based on DbEdgeRules **/ public Collection getEdgeRulesFromJson( String path, boolean skipMatch ) { ArrayList result = new ArrayList<>(); Iterator> edgeRulesIterator; try { List> inEdges = jsonContext.read(path); edgeRulesIterator = inEdges.iterator(); Map edgeMap; String fromNode; String toNode; String direction; String multiplicity; String isParent; String deleteOtherV; String preventDelete; String description; EdgeDescription edgeDes; while( edgeRulesIterator.hasNext() ){ edgeMap = edgeRulesIterator.next(); fromNode = (String)edgeMap.get("from"); toNode = (String)edgeMap.get("to"); if ( skipMatch ) { if ( fromNode.equals(toNode)) { continue; } } edgeDes = new EdgeDescription(); edgeDes.setRuleKey(fromNode + "|" + toNode); edgeDes.setLabel((String)edgeMap.get("label")); edgeDes.setTo((String)edgeMap.get("to")); edgeDes.setFrom((String)edgeMap.get("from")); direction = (String)edgeMap.get("direction"); edgeDes.setDirection(direction); multiplicity = (String)edgeMap.get("multiplicity"); edgeDes.setMultiplicity(multiplicity); isParent = (String)edgeMap.get(EdgeProperty.CONTAINS.toString()); if ( "${direction}".equals(isParent)) { edgeDes.setType(EdgeDescription.LineageType.PARENT); } else { edgeDes.setType(EdgeDescription.LineageType.UNRELATED); } deleteOtherV = (String)edgeMap.get(EdgeProperty.DELETE_OTHER_V.toString()); edgeDes.setDeleteOtherV(deleteOtherV); preventDelete = (String)edgeMap.get(EdgeProperty.PREVENT_DELETE.toString()); edgeDes.setPreventDelete(preventDelete); description = (String)edgeMap.get(EdgeProperty.DESCRIPTION.toString()); edgeDes.setDescription(description); result.add(edgeDes); // logger.debug("Edge: "+edgeDes.getRuleKey()); } } catch (Exception ex) { ex.printStackTrace(); } return result; } private void init() throws FileNotFoundException, IOException { InputStream is = null; Scanner scanner = null; String jsonEdges = null; try { is = new FileInputStream(edgeFile); scanner = new Scanner(is); jsonEdges = scanner.useDelimiter("\\Z").next(); } catch (Exception e) { throw e; } finally { scanner.close(); if (is != null) { try { is.close(); } catch (IOException e) { throw e; } } } jsonContext = JsonPath.parse(jsonEdges); } public String preventDeleteRules(String objectName) { Collection toEdges = getEdgeRulesTO(objectName); toEdges.addAll(getEdgeRulesFROM(objectName)); // logger.debug("TO Edges count: "+toEdges.size()+" Object: "+objectName); String prevent=null; LinkedHashSet preventDelete = new LinkedHashSet(); for (EdgeDescription ed : toEdges) { // logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName); // logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName); if(ed.getPreventDelete().equals("IN") && ed.getTo().equals(objectName)) { preventDelete.add(ed.getFrom().toUpperCase()); } if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) { preventDelete.add(ed.getTo().toUpperCase()); } } if(preventDelete.size() > 0) { prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); // logger.debug(prevent); } return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent)+((prevent == null) ? "" : "\n"); // return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null) ? "" : "\n"); } public String fromDeleteRules(String objectName) { Collection fromEdges = getEdgeRulesFROM(objectName); LinkedHashSet preventDelete = new LinkedHashSet (); String prevent=null; String also=null; for (EdgeDescription ed : fromEdges) { // logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName); // logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName); if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) { preventDelete.add(ed.getTo().toUpperCase()); } } if(preventDelete.size() > 0) { prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); // logger.debug(prevent); } return String.join((prevent == null || also == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null && also == null) ? "" : "\n"); } }