[CORD-3084] Attaching a subscriber to an ONU

Change-Id: Id73c2ba3238b51615e541139b0e7ad0de30a0ba5
diff --git a/xos/synchronizer/models/convenience/voltserviceinstance.py b/xos/synchronizer/models/convenience/voltserviceinstance.py
index 48f52dc..fdae09e 100644
--- a/xos/synchronizer/models/convenience/voltserviceinstance.py
+++ b/xos/synchronizer/models/convenience/voltserviceinstance.py
@@ -17,10 +17,7 @@
 from xosapi.orm import ORMWrapper, register_convenience_wrapper
 from xosapi.convenience.serviceinstance import ORMWrapperServiceInstance
 
-from xosconfig import Config
-from multistructlog import create_logger
-
-log = create_logger(Config().get('logging'))
+import logging as log
 
 class ORMWrapperVOLTServiceInstance(ORMWrapperServiceInstance):
 
@@ -64,32 +61,25 @@
         return self.subscriber.c_tag
 
     def get_olt_device_by_subscriber(self):
+        pon_port = self.get_pon_port_by_subscriber()
+        return pon_port.olt_device
+
+    def get_pon_port_by_subscriber(self):
         si = self.stub.ServiceInstance.objects.get(id=self.id)
-
-        olt_device_name = si.get_westbound_service_instance_properties("olt_device")
-
-        olt_device = self.stub.OLTDevice.objects.get(name=olt_device_name)
-        return olt_device
-
-    def get_olt_port_by_subscriber(self):
-        si = self.stub.ServiceInstance.objects.get(id=self.id)
-
-        olt_port_name = si.get_westbound_service_instance_properties("olt_port")
-
-        olt_device = self.get_olt_device_by_subscriber()
-        olt_port = self.stub.PONPort.objects.get(name=olt_port_name, olt_device_id=olt_device.id)
-        return olt_port
+        onu_sn = si.get_westbound_service_instance_properties("onu_device")
+        onu = self.stub.ONUDevice.objects.get(serial_number=onu_sn)
+        return onu.pon_port
 
     @property
     def s_tag(self):
         try:
-            olt_port = self.get_olt_port_by_subscriber()
+            pon_port = self.get_pon_port_by_subscriber()
 
-            if olt_port:
-                return olt_port.s_tag
+            if pon_port:
+                return pon_port.s_tag
             return None
         except Exception, e:
-            log.warning('Error while reading s_tag: %s' % e.message)
+            log.exception('Error while reading s_tag: %s' % e.message)
             return None
 
     @property
@@ -100,7 +90,7 @@
                 return olt_device.switch_datapath_id
             return None
         except Exception, e:
-            log.warning('Error while reading switch_datapath_id: %s' % e.message)
+            log.exception('Error while reading switch_datapath_id: %s' % e.message)
             return None
 
     @property
@@ -111,7 +101,7 @@
                 return olt_device.switch_port
             return None
         except Exception, e:
-            log.warning('Error while reading switch_port: %s' % e.message)
+            log.exception('Error while reading switch_port: %s' % e.message)
             return None
 
     @property
@@ -122,7 +112,7 @@
                 return olt_device.outer_tpid
             return None
         except Exception, e:
-            log.warning('Error while reading outer_tpid: %s' % e.message)
+            log.exception('Error while reading outer_tpid: %s' % e.message)
             return None
 
 
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
new file mode 100644
index 0000000..eeb5e74
--- /dev/null
+++ b/xos/synchronizer/models/models.py
@@ -0,0 +1,54 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from core.models.xosbase import *
+
+from models_decl import VOLTService_decl
+from models_decl import VOLTServiceInstance_decl
+from models_decl import OLTDevice_decl
+from models_decl import PONPort_decl
+from models_decl import ONUDevice_decl
+
+class VOLTService(VOLTService_decl):
+    class Meta:
+        proxy = True
+
+    @staticmethod
+    def has_access_device(serial_number):
+        try:
+            ONUDevice.objects.get(serial_number=serial_number)
+            return True
+        except IndexError, e:
+            return False
+
+
+class VOLTServiceInstance(VOLTServiceInstance_decl):
+    class Meta:
+        proxy = True 
+
+
+class OLTDevice(OLTDevice_decl):
+    class Meta:
+        proxy = True 
+
+
+class PONPort(PONPort_decl):
+    class Meta:
+        proxy = True 
+
+
+class ONUDevice(ONUDevice_decl):
+    class Meta:
+        proxy = True 
+
diff --git a/xos/synchronizer/models/volt.xproto b/xos/synchronizer/models/volt.xproto
index a73b8ed..355ddaf 100644
--- a/xos/synchronizer/models/volt.xproto
+++ b/xos/synchronizer/models/volt.xproto
@@ -1,5 +1,6 @@
 option name = "volt";
 option app_label = "volt";
+option legacy="True";
 
 message VOLTService (Service){
     option verbose_name = "vOLT Service";
@@ -48,21 +49,6 @@
     optional string outer_tpid = 19 [help_text = "Outer VLAN id field EtherType", null = False, db_index = False, blank = False];
 }
 
-message ONUDevice (XOSBase){
-    option verbose_name = "ONU Device";
-    option description = "Represents a physical ONU device";
-
-    required manytoone olt_device->OLTDevice:onu_devices = 1 [db_index = True, null = False, blank = False];
-    required string serial_number = 2 [max_length = 254, null = False, db_index = False, blank = False, tosca_key=True];
-    required string vendor = 3 [max_length = 254, null = False, db_index = False, blank = False];
-    required string device_id = 4 [max_length = 254, null = False, db_index = False, blank = False];
-
-    required string device_type = 5 [help_text = "Device Type", default = "asfvolt16_olt", max_length = 254, null = False, db_index = False, blank = False];
-    optional string admin_state = 6 [help_text = "admin_state", null = True, db_index = False, blank = False, feedback_state = True];
-    optional string oper_status = 7 [help_text = "oper_status", null = True, db_index = False, blank = False, feedback_state = True];
-    optional string connect_status = 8 [help_text = "connect_status", null = True, db_index = False, blank = False, feedback_state = True];
-}
-
 message PONPort (XOSBase){
     option verbose_name = "PON Port";
 
@@ -71,3 +57,18 @@
     required string port_id = 3 [help_text = "Port ID", max_length = 254, null = False, db_index = False, blank = False];
     required int32 s_tag = 4 [help_text = "S Tag", null = False, db_index = False, blank = False];
 }
+
+message ONUDevice (XOSBase){
+    option verbose_name = "ONU Device";
+    option description = "Represents a physical ONU device";
+
+    required manytoone pon_port->PONPort:onu_devices = 1 [db_index = True, null = False, blank = False];
+    required string serial_number = 2 [max_length = 254, null = False, db_index = False, blank = False, tosca_key=True, unique = True];
+    required string vendor = 3 [max_length = 254, null = False, db_index = False, blank = False];
+    required string device_type = 4 [help_text = "Device Type", default = "asfvolt16_olt", max_length = 254, null = False, db_index = False, blank = False];
+
+    optional string device_id = 5 [max_length = 254, null = True, db_index = False, blank = False, feedback_state = True];
+    optional string admin_state = 6 [help_text = "admin_state", null = True, db_index = False, blank = False, feedback_state = True];
+    optional string oper_status = 7 [help_text = "oper_status", null = True, db_index = False, blank = False, feedback_state = True];
+    optional string connect_status = 8 [help_text = "connect_status", null = True, db_index = False, blank = False, feedback_state = True];
+}
\ No newline at end of file