Fix: re-instantiate the k8s watcher on receiving onclose event

Change-Id: Id7c1fa3124e3cfaccad4dae5c993d2a2f76d964f
(cherry picked from commit 0201a02b937b6574d4cd916163b6256934aa87e8)
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java
index c792bef..c24356f 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java
@@ -99,6 +99,14 @@
         log.info("Stopped");
     }
 
+    private void instantiatePodWatcher() {
+        KubernetesClient client = k8sClient(kubevirtApiConfigService);
+
+        if (client != null) {
+            client.pods().inAnyNamespace().watch(internalKubevirtPodWatcher);
+        }
+    }
+
     private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener {
 
         private boolean isRelevantHelper() {
@@ -125,11 +133,7 @@
                 return;
             }
 
-            KubernetesClient client = k8sClient(kubevirtApiConfigService);
-
-            if (client != null) {
-                client.pods().inAnyNamespace().watch(internalKubevirtPodWatcher);
-            }
+            instantiatePodWatcher();
         }
     }
 
@@ -157,7 +161,11 @@
 
         @Override
         public void onClose(WatcherException e) {
-            log.warn("Pod watcher OnClose", e);
+            // due to the bugs in fabric8, pod watcher might be closed,
+            // we will re-instantiate the pod watcher in this case
+            // FIXME: https://github.com/fabric8io/kubernetes-client/issues/2135
+            log.warn("Pod watcher OnClose, re-instantiate the POD watcher...");
+            instantiatePodWatcher();
         }
 
         private void processAddition(Pod pod) {
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/NetworkAttachmentDefinitionWatcher.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/NetworkAttachmentDefinitionWatcher.java
index bae08bc..32bed58 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/NetworkAttachmentDefinitionWatcher.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/NetworkAttachmentDefinitionWatcher.java
@@ -138,6 +138,18 @@
         log.info("Stopped");
     }
 
+    private void instantiateWatcher() {
+        KubernetesClient client = k8sClient(configService);
+
+        if (client != null) {
+            try {
+                client.customResource(nadCrdCxt).watch(watcher);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener {
 
         private boolean isRelevantHelper() {
@@ -164,15 +176,7 @@
                 return;
             }
 
-            KubernetesClient client = k8sClient(configService);
-
-            if (client != null) {
-                try {
-                    client.customResource(nadCrdCxt).watch(watcher);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
+            instantiateWatcher();
         }
     }
 
@@ -201,7 +205,12 @@
 
         @Override
         public void onClose(WatcherException e) {
-            log.warn("Network-attachment-definition watcher OnClose", e);
+            // due to the bugs in fabric8, the watcher might be closed,
+            // we will re-instantiate the watcher in this case
+            // FIXME: https://github.com/fabric8io/kubernetes-client/issues/2135
+            log.warn("Network-attachment-definition watcher OnClose, re-instantiate the watcher...");
+
+            instantiateWatcher();
         }
 
         private void processAddition(String resource) {