Filled out the stubs previously submitted for getting the pm
configuration from a device. Also added the functionality to the
simulated OLT to set the initial configuration for PMs such that it can
be queried.

Amendment to address reviewer comments and a few other comment changes.

Change-Id: I9d39c8882c2af9c7b2798238918912b0d088d634
diff --git a/voltha/adapters/simulated_olt/simulated_olt.py b/voltha/adapters/simulated_olt/simulated_olt.py
index 0ab3962..28ece21 100644
--- a/voltha/adapters/simulated_olt/simulated_olt.py
+++ b/voltha/adapters/simulated_olt/simulated_olt.py
@@ -304,6 +304,7 @@
 
         # first we pretend that we were able to contact the device and obtain
         # additional information about it
+        #log.info("device-activation")
         device.root = True
         device.vendor = 'simulated'
         device.model = 'n/a'
@@ -312,6 +313,54 @@
         device.software_version = '1.0'
         device.serial_number = uuid4().hex
         device.connect_status = ConnectStatus.REACHABLE
+        #log.info("device-config",device=device)
+        device.pm_configs.default_freq=150
+        device.pm_configs.grouped = False
+        device.pm_configs.freq_override = False
+        #log.info("device-config",device=device)
+        device.pm_configs.metrics.extend([PmConfig(name='tx_64',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_65_127',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_128_255',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_256_511',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_512_1023',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_1024_1518',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='tx_1519_9k',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_64',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_65_127',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_128_255',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_256_511',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_512_1023',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_1024_1518',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        device.pm_configs.metrics.extend([PmConfig(name='rx_1519_9k',
+                                                   type=PmConfig.COUNTER,
+                                                   enabled=True)])
+        #log.info("device-config",device=device)
         self.adapter_agent.update_device(device)
 
         # then shortly after we create some ports for the device
@@ -332,49 +381,6 @@
             oper_status=OperStatus.ACTIVE
         ))
 
-        # then shortly after, add the supported pms for the device
-        yield asleep(0.05)
-        try:
-            log.info("Setting p")
-            p = PmConfigs(
-                default_freq=150,
-                grouped=False,
-                freq_override=False)
-            p.metrics.extend([PmConfig(name='tx_64',type=PmConfig.COUNTER,
-                                       enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_65_127',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_128_255',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_256_511',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_512_1023',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_1024_1518',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='tx_1519_9k',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_64',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_65_127',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_128_255',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_256_511',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_512_1023',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_1024_1518',
-                                       type=PmConfig.COUNTER,enabled=True)])
-            p.metrics.extend([PmConfig(name='rx_1519_9k',
-                                       type=PmConfig.COUNTER,enabled=True)])
-
-
-            #TODO Call the adapter agend to update the pm config
-            #self.adapter_agent.update_pms_config(device.id,p)
-        except:
-            e = sys.exec_info()
-            log.error("error", error=e)
 
         # then shortly after we create the logical device with one port
         # that will correspond to the NNI port
diff --git a/voltha/core/global_handler.py b/voltha/core/global_handler.py
index eff422e..03a443a 100644
--- a/voltha/core/global_handler.py
+++ b/voltha/core/global_handler.py
@@ -372,7 +372,25 @@
     #TODO: create the global PM config query function
     @twisted_async
     def ListDevicePmConfigs(self, request, context):
-        raise NotImplementedError('Method not implemented!')
+        #raise NotImplementedError('Method not implemented!')
+        log.info('grpc-request', request=request)
+
+        try:
+            instance_id = self.dispatcher.instance_id_by_device_id(
+                request.id
+            )
+        except KeyError:
+            context.set_details(
+                'Device \'{}\' not found'.format(request.id))
+            context.set_code(StatusCode.NOT_FOUND)
+            return PmConfigs()
+
+        return self.dispatcher.dispatch(
+            instance_id,
+            VolthaLocalServiceStub,
+            'ListDevicePmConfigs',
+            request,
+            context)
 
     #TODO: create the global PM config update function.
     @twisted_async
diff --git a/voltha/core/local_handler.py b/voltha/core/local_handler.py
index a8800b9..e494bd6 100644
--- a/voltha/core/local_handler.py
+++ b/voltha/core/local_handler.py
@@ -29,6 +29,7 @@
     VolthaInstance, Adapters, LogicalDevices, LogicalDevice, Ports, \
     LogicalPorts, Devices, Device, DeviceType, \
     DeviceTypes, DeviceGroups, DeviceGroup, AdminState, OperStatus, ChangeEvent
+from voltha.protos.device_pb2 import PmConfigs
 from voltha.registry import registry
 
 log = structlog.get_logger()
@@ -420,12 +421,28 @@
             context.set_code(StatusCode.NOT_FOUND)
             return Ports()
 
-    #TODO: create the global PM config query function
     @twisted_async
     def ListDevicePmConfigs(self, request, context):
-        raise NotImplementedError('Method not implemented!')
+        #raise NotImplementedError('Method not implemented!')
+        log.info('grpc-request', request=request)
 
-    #TODO: create the global PM config update function.
+        if '/' in request.id:
+            context.set_details(
+                'Malformed device id \'{}\''.format(request.id))
+            context.set_code(StatusCode.INVALID_ARGUMENT)
+            return PmConfigs()
+
+        try:
+            device = self.root.get('/devices/{}'.format(request.id))
+            log.info('device-for-pms',device=device)
+            return device.pm_configs
+        except KeyError:
+            context.set_details(
+                'Device \'{}\' not found'.format(request.id))
+            context.set_code(StatusCode.NOT_FOUND)
+            return PmConfigs()
+
+    #TODO: create the local PM config update function.
     @twisted_async
     def UpdateDevicePmConfigs(self, request, context):
         raise NotImplementedError('Method not implemented!')
diff --git a/voltha/protos/device.proto b/voltha/protos/device.proto
index 8cc22d4..d13b812 100644
--- a/voltha/protos/device.proto
+++ b/voltha/protos/device.proto
@@ -164,7 +164,9 @@
     repeated Port ports = 128  [(child_node) = {key: "port_no"}];
     openflow_13.Flows flows = 129 [(child_node) = {}];
     openflow_13.FlowGroups flow_groups = 130 [(child_node) = {}];
-    PmConfigs kpis = 131 [(child_node) = {}];
+    // PmConfigs will eventually converted to a child node of the
+    // device to falicitata callbacks and to simplify manipulation.
+    PmConfigs pm_configs = 131;
 
 }