Fix: refactor network policy handler remove code duplication

Change-Id: I2980bb16aa2c80d7484f0b0f4e39df6b5e9bf886
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkPolicyHandler.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkPolicyHandler.java
index ce31f04..1044e39 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkPolicyHandler.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNetworkPolicyHandler.java
@@ -348,30 +348,7 @@
                 }
 
                 // POD selector
-                Set<Pod> pods = Sets.newConcurrentHashSet();
-                if (peer.getPodSelector() != null) {
-                    Map<String, String> podLabels = peer.getPodSelector().getMatchLabels();
-                    List<LabelSelectorRequirement> matchExps = peer.getPodSelector().getMatchExpressions();
-
-                    if (podLabels == null && matchExps.size() == 0) {
-                        k8sPodService.pods().stream()
-                                .filter(pod -> pod.getMetadata().getNamespace().equals(
-                                        policy.getMetadata().getNamespace()))
-                                .forEach(pods::add);
-                    } else {
-                        k8sPodService.pods().stream()
-                                .filter(pod -> pod.getMetadata().getNamespace().equals(
-                                        policy.getMetadata().getNamespace()))
-                                .forEach(pod -> {
-                                    pod.getMetadata().getLabels().forEach((k, v) -> {
-                                        if (podLabels != null && podLabels.get(k) != null &&
-                                                podLabels.get(k).equals(v)) {
-                                            pods.add(pod);
-                                        }
-                                    });
-                                });
-                    }
-                }
+                Set<Pod> pods = podsFromPolicyPeer(peer, policy.getMetadata().getNamespace());
 
                 pods.forEach(pod -> {
                     white.compute(shiftIpDomain(pod.getStatus().getPodIP(),
@@ -429,33 +406,8 @@
                     }
                 }
 
-                Set<Pod> pods = Sets.newConcurrentHashSet();
-
                 // POD selector
-                if (peer.getPodSelector() != null) {
-                    Map<String, String> podLabels = peer.getPodSelector().getMatchLabels();
-
-                    List<LabelSelectorRequirement> matchExps = peer.getPodSelector().getMatchExpressions();
-
-                    if (podLabels == null && matchExps.size() == 0) {
-                        k8sPodService.pods().stream()
-                                .filter(pod -> pod.getMetadata().getNamespace().equals(
-                                        policy.getMetadata().getNamespace()))
-                                .forEach(pods::add);
-                    } else {
-                        k8sPodService.pods().stream()
-                                .filter(pod -> pod.getMetadata().getNamespace().equals(
-                                        policy.getMetadata().getNamespace()))
-                                .forEach(pod -> {
-                                    pod.getMetadata().getLabels().forEach((k, v) -> {
-                                        if (podLabels != null && podLabels.get(k) != null &&
-                                                podLabels.get(k).equals(v)) {
-                                            pods.add(pod);
-                                        }
-                                    });
-                                });
-                    }
-                }
+                Set<Pod> pods = podsFromPolicyPeer(peer, policy.getMetadata().getNamespace());
 
                 pods.forEach(pod -> {
                     white.compute(shiftIpDomain(pod.getStatus().getPodIP(),
@@ -490,6 +442,34 @@
         setBlackToRouteRules(true);
     }
 
+    private Set<Pod> podsFromPolicyPeer(NetworkPolicyPeer peer, String namespace) {
+        Set<Pod> pods = Sets.newConcurrentHashSet();
+        if (peer.getPodSelector() != null) {
+            Map<String, String> podLabels = peer.getPodSelector().getMatchLabels();
+            List<LabelSelectorRequirement> matchExps = peer.getPodSelector().getMatchExpressions();
+
+            if (podLabels == null && matchExps.size() == 0) {
+                k8sPodService.pods().stream()
+                        .filter(pod -> pod.getMetadata().getNamespace().equals(
+                                namespace))
+                        .forEach(pods::add);
+            } else {
+                k8sPodService.pods().stream()
+                        .filter(pod -> pod.getMetadata().getNamespace().equals(
+                                namespace))
+                        .forEach(pod -> {
+                            pod.getMetadata().getLabels().forEach((k, v) -> {
+                                if (podLabels != null && podLabels.get(k) != null &&
+                                        podLabels.get(k).equals(v)) {
+                                    pods.add(pod);
+                                }
+                            });
+                        });
+            }
+        }
+        return pods;
+    }
+
     private void setAllowRulesByPod(Pod pod, boolean install) {
         Map<String, Map<String, List<NetworkPolicyPort>>>
                 white = Maps.newConcurrentMap();