Fix bug 659: Don't use static member variables
[osm/N2VC.git] / n2vc / vnf.py
index 2830571..a486f27 100644 (file)
@@ -57,13 +57,12 @@ logging.getLogger('juju.machine').setLevel(logging.WARN)
 class VCAMonitor(ModelObserver):
     """Monitor state changes within the Juju Model."""
     log = None
-    ns_name = None
-    applications = {}
 
     def __init__(self, ns_name):
         self.log = logging.getLogger(__name__)
 
         self.ns_name = ns_name
+        self.applications = {}
 
     def AddApplication(self, application_name, callback, *callback_args):
         if application_name not in self.applications:
@@ -805,6 +804,10 @@ class N2VC:
             except JujuError as e:
                 if "already exists" not in e.message:
                     raise e
+
+            # Create an observer for this model
+            await self.create_model_monitor(ns_uuid)
+
         return True
 
     async def DestroyNetworkService(self, ns_uuid):
@@ -1061,10 +1064,20 @@ class N2VC:
             self.refcount['model'] += 1
 
             # Create an observer for this model
+            await self.create_model_monitor(model_name)
+
+        return self.models[model_name]
+
+    async def create_model_monitor(self, model_name):
+        """Create a monitor for the model, if none exists."""
+        if not self.authenticated:
+            await self.login()
+
+        if model_name not in self.monitors:
             self.monitors[model_name] = VCAMonitor(model_name)
             self.models[model_name].add_observer(self.monitors[model_name])
 
-        return self.models[model_name]
+        return True
 
     async def login(self):
         """Login to the Juju controller."""