VOL-514 ASFVOLT Reconcillation feature

Change-Id: I63821a4052e4ec88d9854b9a7411935688691a99
(cherry picked from commit b24810379b2fb79da82c070e4facf99fc9b4a382)
diff --git a/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py b/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
index b589398..f991efc 100644
--- a/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
+++ b/voltha/adapters/asfvolt16_olt/asfvolt16_device_handler.py
@@ -346,6 +346,64 @@
         device.oper_status = OperStatus.ACTIVATING
         self.adapter_agent.update_device(device)
 
+    def reconcile(self, device):
+
+        self.log.info('reconciling-asfvolt16-starts',device=device)
+
+        if not device.host_and_port:
+            device.oper_status = OperStatus.FAILED
+            device.reason = 'No host_and_port field provided'
+            self.adapter_agent.update_device(device)
+            return
+
+        try:
+            # Establishing connection towards OLT
+            self.bal.connect_olt(device.host_and_port, self.device_id,is_init=False)
+            device.connect_status = ConnectStatus.REACHABLE
+            device.oper_status = OperStatus.ACTIVE
+            self.adapter_agent.update_device(device)
+            reactor.callInThread(self.bal.get_indication_info, self.device_id)
+
+        except Exception as e:
+            self.log.exception('device-unreachable', error=e)
+            device.connect_status = ConnectStatus.UNREACHABLE
+            device.oper_status = OperStatus.UNKNOWN
+            self.adapter_agent.update_device(device)
+            return
+
+        if self.is_heartbeat_started == 0:
+            self.log.info('heart-beat-is-not-yet-started-starting-now')
+            self.heartbeat(device)
+
+            # Now set the initial PM configuration for this device
+            self.pm_metrics=Asfvolt16OltPmMetrics(device)
+            pm_config = self.pm_metrics.make_proto()
+            self.log.info("initial-pm-config", pm_config=pm_config)
+            self.adapter_agent.update_device_pm_config(pm_config,init=True)
+
+
+            # Apply the PM configuration
+            self.update_pm_config(device, pm_config)
+
+
+            # Request PM counters from OLT device.
+            self._handle_pm_counter_req_towards_device(device)
+
+        # Set the logical device id
+        device = self.adapter_agent.get_device(device.id)
+        if device.parent_id:
+            self.logical_device_id = device.parent_id
+            self.log.info("reconcile-logical-device")
+            self.adapter_agent.reconcile_logical_device(device.parent_id)
+        else:
+            self.log.info('no-logical-device-set')
+
+        # Reconcile child devices
+        self.log.info("reconcile-all-child-devices")
+        self.adapter_agent.reconcile_child_devices(device.id)
+        self.log.info('reconciling-asfvolt16-device-ends',device=device)
+
+
     @inlineCallbacks
     def heartbeat(self, device, state = 'run'):
         self.log.debug('olt-heartbeat', device=device, state=state,
diff --git a/voltha/adapters/asfvolt16_olt/bal.py b/voltha/adapters/asfvolt16_olt/bal.py
index 9412c83..5573027 100644
--- a/voltha/adapters/asfvolt16_olt/bal.py
+++ b/voltha/adapters/asfvolt16_olt/bal.py
@@ -43,7 +43,7 @@
         self.ind_obj = Asfvolt16IndHandler(log)
 
     @inlineCallbacks
-    def connect_olt(self, host_and_port, device_id):
+    def connect_olt(self, host_and_port, device_id, is_init=True):
         self.device_id = device_id
         self.grpc_client.connect(host_and_port)
         self.stub = bal_pb2.BalStub(self.grpc_client.channel)
@@ -53,23 +53,24 @@
         # Right now Bi-Directional GRPC support is not there in grpc-c.
         # This code may be needed when bidirectional supported added
         # in GRPC-C
-        init = bal_pb2.BalInit()
-        try:
-            os.environ["SERVICE_HOST_IP"]
-            adapter_ip = os.environ["SERVICE_HOST_IP"]
-        except Exception as e:
-            self.log.info('voltha is running in non docker container environment')
-            adapter_ip = get_my_primary_local_ipv4()
+        if is_init == True:
+            init = bal_pb2.BalInit()
+            try:
+                os.environ["SERVICE_HOST_IP"]
+                adapter_ip = os.environ["SERVICE_HOST_IP"]
+            except Exception as e:
+                self.log.info('voltha is running in non docker container environment')
+                adapter_ip = get_my_primary_local_ipv4()
 
-        ip_port = []
-        ip_port.append(str(adapter_ip))
-        ip_port.append(":")
-        ip_port.append(str(ADAPTER_PORT))
-        init.voltha_adapter_ip_port ="".join(ip_port)
-        self.log.info('Adapter port Ip', init.voltha_adapter_ip_port)
-        self.log.info('connecting-olt', host_and_port=host_and_port,
-                      init_details=init)
-        yield self.stub.BalApiInit(init)
+            ip_port = []
+            ip_port.append(str(adapter_ip))
+            ip_port.append(":")
+            ip_port.append(str(ADAPTER_PORT))
+            init.voltha_adapter_ip_port ="".join(ip_port)
+            self.log.info('Adapter port Ip', init.voltha_adapter_ip_port)
+            self.log.info('connecting-olt', host_and_port=host_and_port,
+                          init_details=init)
+            yield self.stub.BalApiInit(init)
 
     def activate_olt(self):
         self.log.info('activating-olt')