Hyunsun Moon
Committed by Gerrit Code Review

Avoid using exception in not exceptional logic

Change-Id: I4eb4785b2610fbb1c7e6dd67688ce7d666ec22de
...@@ -38,7 +38,6 @@ import org.onosproject.net.DeviceId; ...@@ -38,7 +38,6 @@ import org.onosproject.net.DeviceId;
38 import org.onosproject.net.Host; 38 import org.onosproject.net.Host;
39 import org.onosproject.net.HostId; 39 import org.onosproject.net.HostId;
40 import org.onosproject.net.Port; 40 import org.onosproject.net.Port;
41 -import org.onosproject.net.PortNumber;
42 import org.onosproject.net.behaviour.BridgeConfig; 41 import org.onosproject.net.behaviour.BridgeConfig;
43 import org.onosproject.net.behaviour.BridgeName; 42 import org.onosproject.net.behaviour.BridgeName;
44 import org.onosproject.net.behaviour.ControllerInfo; 43 import org.onosproject.net.behaviour.ControllerInfo;
...@@ -77,7 +76,6 @@ import java.util.ArrayList; ...@@ -77,7 +76,6 @@ import java.util.ArrayList;
77 import java.util.HashMap; 76 import java.util.HashMap;
78 import java.util.List; 77 import java.util.List;
79 import java.util.Map; 78 import java.util.Map;
80 -import java.util.NoSuchElementException;
81 import java.util.Objects; 79 import java.util.Objects;
82 import java.util.Set; 80 import java.util.Set;
83 import java.util.concurrent.ExecutorService; 81 import java.util.concurrent.ExecutorService;
...@@ -492,14 +490,9 @@ public class CordVtn implements CordVtnService { ...@@ -492,14 +490,9 @@ public class CordVtn implements CordVtnService {
492 * @return cordvtn node, null if it fails to find the node 490 * @return cordvtn node, null if it fails to find the node
493 */ 491 */
494 private CordVtnNode getNodeByOvsdbId(DeviceId ovsdbId) { 492 private CordVtnNode getNodeByOvsdbId(DeviceId ovsdbId) {
495 - try { 493 + return getNodes().stream()
496 - return getNodes().stream() 494 + .filter(node -> node.ovsdbId().equals(ovsdbId))
497 - .filter(node -> node.ovsdbId().equals(ovsdbId)) 495 + .findFirst().orElse(null);
498 - .findFirst().get();
499 - } catch (NoSuchElementException e) {
500 - log.debug("Couldn't find node information for {}", ovsdbId);
501 - return null;
502 - }
503 } 496 }
504 497
505 /** 498 /**
...@@ -509,14 +502,9 @@ public class CordVtn implements CordVtnService { ...@@ -509,14 +502,9 @@ public class CordVtn implements CordVtnService {
509 * @return cordvtn node, null if it fails to find the node 502 * @return cordvtn node, null if it fails to find the node
510 */ 503 */
511 private CordVtnNode getNodeByBridgeId(DeviceId bridgeId) { 504 private CordVtnNode getNodeByBridgeId(DeviceId bridgeId) {
512 - try { 505 + return getNodes().stream()
513 - return getNodes().stream() 506 + .filter(node -> node.intBrId().equals(bridgeId))
514 - .filter(node -> node.intBrId().equals(bridgeId)) 507 + .findFirst().orElse(null);
515 - .findFirst().get();
516 - } catch (NoSuchElementException e) {
517 - log.debug("Couldn't find node information for {}", bridgeId);
518 - return null;
519 - }
520 } 508 }
521 509
522 /** 510 /**
...@@ -620,16 +608,11 @@ public class CordVtn implements CordVtnService { ...@@ -620,16 +608,11 @@ public class CordVtn implements CordVtnService {
620 * @return true if the interface exists, false otherwise 608 * @return true if the interface exists, false otherwise
621 */ 609 */
622 private boolean checkTunnelInterface(CordVtnNode node) { 610 private boolean checkTunnelInterface(CordVtnNode node) {
623 - try { 611 + return deviceService.getPorts(node.intBrId())
624 - deviceService.getPorts(node.intBrId()) 612 + .stream()
625 - .stream() 613 + .filter(p -> getPortName(p).contains(DEFAULT_TUNNEL)
626 - .filter(p -> getPortName(p).contains(DEFAULT_TUNNEL) 614 + && p.isEnabled())
627 - && p.isEnabled()) 615 + .findAny().isPresent();
628 - .findAny().get();
629 - return true;
630 - } catch (NoSuchElementException e) {
631 - return false;
632 - }
633 } 616 }
634 617
635 /** 618 /**
...@@ -639,33 +622,11 @@ public class CordVtn implements CordVtnService { ...@@ -639,33 +622,11 @@ public class CordVtn implements CordVtnService {
639 * @return true if the interface exists, false otherwise 622 * @return true if the interface exists, false otherwise
640 */ 623 */
641 private boolean checkPhyInterface(CordVtnNode node) { 624 private boolean checkPhyInterface(CordVtnNode node) {
642 - try { 625 + return deviceService.getPorts(node.intBrId())
643 - deviceService.getPorts(node.intBrId()) 626 + .stream()
644 - .stream() 627 + .filter(p -> getPortName(p).contains(node.phyPortName())
645 - .filter(p -> getPortName(p).contains(node.phyPortName()) 628 + && p.isEnabled())
646 - && p.isEnabled()) 629 + .findAny().isPresent();
647 - .findAny().get();
648 - return true;
649 - } catch (NoSuchElementException e) {
650 - return false;
651 - }
652 - }
653 -
654 - /**
655 - * Returns tunnel port of the device.
656 - *
657 - * @param bridgeId device id
658 - * @return port number, null if no tunnel port exists on a given device
659 - */
660 - private PortNumber getTunnelPort(DeviceId bridgeId) {
661 - try {
662 - return deviceService.getPorts(bridgeId).stream()
663 - .filter(p -> getPortName(p).contains(DEFAULT_TUNNEL)
664 - && p.isEnabled())
665 - .findFirst().get().number();
666 - } catch (NoSuchElementException e) {
667 - return null;
668 - }
669 } 630 }
670 631
671 /** 632 /**
...@@ -679,6 +640,7 @@ public class CordVtn implements CordVtnService { ...@@ -679,6 +640,7 @@ public class CordVtn implements CordVtnService {
679 if (node != null) { 640 if (node != null) {
680 return node.localIp().getIp4Address(); 641 return node.localIp().getIp4Address();
681 } else { 642 } else {
643 + log.debug("Couldn't find node information for {}", bridgeId);
682 return null; 644 return null;
683 } 645 }
684 } 646 }
...@@ -897,6 +859,8 @@ public class CordVtn implements CordVtnService { ...@@ -897,6 +859,8 @@ public class CordVtn implements CordVtnService {
897 CordVtnNode node = getNodeByOvsdbId(device.id()); 859 CordVtnNode node = getNodeByOvsdbId(device.id());
898 if (node != null) { 860 if (node != null) {
899 setNodeState(node, checkNodeState(node)); 861 setNodeState(node, checkNodeState(node));
862 + } else {
863 + log.debug("Unregistered device {} connected, ignore it.", device.id());
900 } 864 }
901 } 865 }
902 866
...@@ -915,6 +879,8 @@ public class CordVtn implements CordVtnService { ...@@ -915,6 +879,8 @@ public class CordVtn implements CordVtnService {
915 CordVtnNode node = getNodeByBridgeId(device.id()); 879 CordVtnNode node = getNodeByBridgeId(device.id());
916 if (node != null) { 880 if (node != null) {
917 setNodeState(node, checkNodeState(node)); 881 setNodeState(node, checkNodeState(node));
882 + } else {
883 + log.debug("Unregistered device {} connected, ignore it.", device.id());
918 } 884 }
919 } 885 }
920 886
...@@ -938,6 +904,8 @@ public class CordVtn implements CordVtnService { ...@@ -938,6 +904,8 @@ public class CordVtn implements CordVtnService {
938 CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id()); 904 CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id());
939 if (node == null) { 905 if (node == null) {
940 return; 906 return;
907 + } else {
908 + log.debug("Port {} added to unregistered device, ignore it.", getPortName(port));
941 } 909 }
942 910
943 // TODO add host by updating network config 911 // TODO add host by updating network config
......
...@@ -33,6 +33,7 @@ import org.onosproject.mastership.MastershipService; ...@@ -33,6 +33,7 @@ import org.onosproject.mastership.MastershipService;
33 import org.onosproject.net.Device; 33 import org.onosproject.net.Device;
34 import org.onosproject.net.DeviceId; 34 import org.onosproject.net.DeviceId;
35 import org.onosproject.net.Host; 35 import org.onosproject.net.Host;
36 +import org.onosproject.net.Port;
36 import org.onosproject.net.PortNumber; 37 import org.onosproject.net.PortNumber;
37 import org.onosproject.net.behaviour.ExtensionTreatmentResolver; 38 import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
38 import org.onosproject.net.device.DeviceService; 39 import org.onosproject.net.device.DeviceService;
...@@ -75,7 +76,6 @@ import org.slf4j.Logger; ...@@ -75,7 +76,6 @@ import org.slf4j.Logger;
75 import java.util.ArrayList; 76 import java.util.ArrayList;
76 import java.util.List; 77 import java.util.List;
77 import java.util.Map; 78 import java.util.Map;
78 -import java.util.NoSuchElementException;
79 import java.util.Objects; 79 import java.util.Objects;
80 import java.util.Set; 80 import java.util.Set;
81 import java.util.stream.Collectors; 81 import java.util.stream.Collectors;
...@@ -828,13 +828,11 @@ public class CordVtnRuleInstaller { ...@@ -828,13 +828,11 @@ public class CordVtnRuleInstaller {
828 * @return tunnel port number, or null if no tunnel port exists on a given device 828 * @return tunnel port number, or null if no tunnel port exists on a given device
829 */ 829 */
830 private PortNumber getTunnelPort(DeviceId deviceId) { 830 private PortNumber getTunnelPort(DeviceId deviceId) {
831 - try { 831 + Port port = deviceService.getPorts(deviceId).stream()
832 - return deviceService.getPorts(deviceId).stream()
833 .filter(p -> p.annotations().value("portName").contains(tunnelType)) 832 .filter(p -> p.annotations().value("portName").contains(tunnelType))
834 - .findFirst().get().number(); 833 + .findFirst().orElse(null);
835 - } catch (NoSuchElementException e) { 834 +
836 - return null; 835 + return port == null ? null : port.number();
837 - }
838 } 836 }
839 837
840 /** 838 /**
...@@ -845,14 +843,12 @@ public class CordVtnRuleInstaller { ...@@ -845,14 +843,12 @@ public class CordVtnRuleInstaller {
845 * @return physical port number, or null if no physical port exists 843 * @return physical port number, or null if no physical port exists
846 */ 844 */
847 private PortNumber getPhyPort(DeviceId deviceId, String phyPortName) { 845 private PortNumber getPhyPort(DeviceId deviceId, String phyPortName) {
848 - try { 846 + Port port = deviceService.getPorts(deviceId).stream()
849 - return deviceService.getPorts(deviceId).stream()
850 .filter(p -> p.annotations().value("portName").contains(phyPortName) && 847 .filter(p -> p.annotations().value("portName").contains(phyPortName) &&
851 p.isEnabled()) 848 p.isEnabled())
852 - .findFirst().get().number(); 849 + .findFirst().orElse(null);
853 - } catch (NoSuchElementException e) { 850 +
854 - return null; 851 + return port == null ? null : port.number();
855 - }
856 } 852 }
857 853
858 /** 854 /**
......