Add utility method addPort

Change-Id: I11ec7fe63aa3115bd72c38047b8899aab4d74fa9
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
index 101f628..d96188f 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -409,13 +409,7 @@
 	}
 
 	private boolean prepareForRemovePortEvent(PortEvent portEvt) {
-		// Parent Switch must exist
-		Switch sw = getSwitch(portEvt.getDpid());
-		if ( sw ==  null ) {
-		    log.debug("Switch already removed? {}", portEvt);
-		    return false;
-		}
-		Port port = sw.getPort(portEvt.getNumber());
+		Port port = getPort(portEvt.getDpid(), portEvt.getNumber());
 		if ( port == null ) {
 		    log.debug("Port already removed? {}", portEvt);
 		    // let it pass
@@ -454,17 +448,9 @@
 	}
 
 	private boolean prepareForAddLinkEvent(LinkEvent linkEvt) {
-	    // Src/Dst Switch must exist
-	    Switch srcSw = getSwitch(linkEvt.getSrc().dpid);
-	    Switch dstSw = getSwitch(linkEvt.getDst().dpid);
-	    if ( srcSw == null || dstSw == null ) {
-	    log.warn("Dropping add link event because switch doesn't exist: {}",
-	    		linkEvt);
-		return false;
-	    }
 	    // Src/Dst Port must exist
-	    Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
-	    Port dstPort = dstSw.getPort(linkEvt.getDst().number);
+	    Port srcPort = getPort(linkEvt.getSrc().dpid, linkEvt.getSrc().number);
+	    Port dstPort = getPort(linkEvt.getDst().dpid, linkEvt.getDst().number);
 	    if ( srcPort == null || dstPort == null ) {
 	    log.warn("Dropping add link event because port doesn't exist: {}",
 	    		linkEvt);
@@ -492,16 +478,9 @@
 	}
 
 	private boolean prepareForRemoveLinkEvent(LinkEvent linkEvt) {
-	    // Src/Dst Switch must exist
-	    Switch srcSw = getSwitch(linkEvt.getSrc().dpid);
-	    Switch dstSw = getSwitch(linkEvt.getDst().dpid);
-	    if ( srcSw == null || dstSw == null ) {
-		log.warn("Dropping remove link event because switch doesn't exist: {}", linkEvt);
-		return false;
-	    }
 	    // Src/Dst Port must exist
-	    Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
-	    Port dstPort = dstSw.getPort(linkEvt.getDst().number);
+	    Port srcPort = getPort(linkEvt.getSrc().dpid, linkEvt.getSrc().number);
+	    Port dstPort = getPort(linkEvt.getDst().dpid, linkEvt.getDst().number);
 	    if ( srcPort == null || dstPort == null ) {
 		log.warn("Dropping remove link event because port doesn't exist {}", linkEvt);
 		return false;
@@ -531,15 +510,8 @@
 	    boolean preconditionBroken = false;
 	    ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>();
 	    for ( PortEvent.SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
-		// Attached Ports' Parent Switch must exist
-		Switch sw = getSwitch(swp.dpid);
-		if ( sw ==  null ) {
-		    preconditionBroken = true;
-		    failedSwitchPort.add(swp);
-		    continue;
-		}
 		// Attached Ports must exist
-		Port port = sw.getPort(swp.number);
+		Port port = getPort(swp.dpid, swp.number);
 		if ( port == null ) {
 		    preconditionBroken = true;
 		    failedSwitchPort.add(swp);
@@ -827,23 +799,7 @@
 		throw new IllegalArgumentException("Link cannot be null");
 	    }
 
-	    Switch srcSw = switches.get(linkEvt.getSrc().dpid);
-	    if (srcSw == null) {
-		throw new BrokenInvariantException(
-			String.format(
-				"Switch with dpid %s did not exist.",
-				new Dpid(linkEvt.getSrc().dpid)));
-	    }
-
-	    Switch dstSw = switches.get(linkEvt.getDst().dpid);
-	    if (dstSw == null) {
-		throw new BrokenInvariantException(
-			String.format(
-				"Switch with dpid %s did not exist.",
-				new Dpid(linkEvt.getDst().dpid)));
-	    }
-
-	    Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
+	    Port srcPort = getPort(linkEvt.getSrc().dpid, linkEvt.getSrc().number);
 	    if (srcPort == null) {
 		throw new BrokenInvariantException(
 			String.format(
@@ -851,7 +807,7 @@
 				linkEvt.getSrc() ));
 	    }
 
-	    Port dstPort = dstSw.getPort(linkEvt.getDst().number);
+	    Port dstPort = getPort(linkEvt.getDst().dpid, linkEvt.getDst().number);
 	    if (dstPort == null) {
 		throw new BrokenInvariantException(
 			String.format(
@@ -912,25 +868,13 @@
 		throw new IllegalArgumentException("Link cannot be null");
 	    }
 
-	    Switch srcSw = switches.get(linkEvt.getSrc().dpid);
-	    if (srcSw == null) {
-		log.warn("Src Switch for Link {} already removed, ignoring", linkEvt);
-		return;
-	    }
-
-	    Switch dstSw = switches.get(linkEvt.getDst().dpid);
-	    if (dstSw == null) {
-		log.warn("Dst Switch for Link {} already removed, ignoring", linkEvt);
-		return;
-	    }
-
-	    Port srcPort = srcSw.getPort(linkEvt.getSrc().number);
+	    Port srcPort = getPort(linkEvt.getSrc().dpid, linkEvt.getSrc().number);
 	    if (srcPort == null) {
 		log.warn("Src Port for Link {} already removed, ignoring", linkEvt);
 		return;
 	    }
 
-	    Port dstPort = dstSw.getPort(linkEvt.getDst().number);
+	    Port dstPort = getPort(linkEvt.getDst().dpid, linkEvt.getDst().number);
 	    if (dstPort == null) {
 		log.warn("Dst Port for Link {} already removed, ignoring", linkEvt);
 		return;
@@ -970,14 +914,8 @@
 
 	    // for each attachment point
 	    for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
-		// Attached Ports' Parent Switch must exist
-		Switch sw = getSwitch(swp.dpid);
-		if ( sw ==  null ) {
-		    log.warn("Switch for the attachment point {} did not exist. skipping mutation", swp);
-		    continue;
-		}
 		// Attached Ports must exist
-		Port port = sw.getPort(swp.number);
+		Port port = getPort(swp.dpid, swp.number);
 		if ( port == null ) {
 		    log.warn("Port for the attachment point {} did not exist. skipping mutation", swp);
 		    continue;
@@ -1036,14 +974,8 @@
 
 	    // for each attachment point
 	    for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
-		// Attached Ports' Parent Switch must exist
-		Switch sw = getSwitch(swp.dpid);
-		if ( sw ==  null ) {
-		    log.warn("Switch for the attachment point {} did not exist. skipping attachment point mutation", swp);
-		    continue;
-		}
 		// Attached Ports must exist
-		Port port = sw.getPort(swp.number);
+		Port port = getPort(swp.dpid, swp.number);
 		if ( port == null ) {
 		    log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp);
 		    continue;
@@ -1135,6 +1067,15 @@
 	    }
 	}
 
+	// we might want to include this in NetworkGraph interface
+	private Port getPort(Long dpid, Long number) {
+	    Switch sw = getSwitch(dpid);
+	    if (sw != null) {
+		return sw.getPort(number);
+	    }
+	    return null;
+	}
+
 	private SwitchImpl getSwitchImpl(Switch sw) {
 	    if (sw instanceof SwitchImpl) {
 		return (SwitchImpl) sw;
@@ -1192,15 +1133,8 @@
 
 	    for (RCLink l : RCLink.getAllLinks()) {
 		// check if src/dst switch/port exist before triggering event
-		// Src/Dst Switch must exist
-		Switch srcSw = getSwitch(l.getSrc().dpid);
-		Switch dstSw = getSwitch(l.getDst().dpid);
-		if ( srcSw == null || dstSw == null ) {
-		    continue;
-		}
-		// Src/Dst Port must exist
-		Port srcPort = srcSw.getPort(l.getSrc().number);
-		Port dstPort = dstSw.getPort(l.getDst().number);
+		Port srcPort = getPort(l.getSrc().dpid, l.getSrc().number);
+		Port dstPort = getPort(l.getDst().dpid, l.getDst().number);
 		if ( srcPort == null || dstPort == null ) {
 		    continue;
 		}