VOL-1403:
a) Read Tech-Profile ID coming as part of write metadata action to
   configure the OLTs schedulers and queues
b) Use the meter-id information coming as part of the flow to do
   shaping on the schedulers
c) Generate a tech-profile instance and share it with the ONU so that
   ONU can also do the necessary OMCI configuration as per the
   tech-profile.
d) Other miscellaneous changes

Change-Id: I0939d7f022aff98c23ac9a60f468cc134d3c1bf3
diff --git a/common/tech_profile/tech_profile.py b/common/tech_profile/tech_profile.py
index abea364..6055b0b 100644
--- a/common/tech_profile/tech_profile.py
+++ b/common/tech_profile/tech_profile.py
@@ -22,7 +22,7 @@
 from voltha.core.config.config_backend import ConsulStore
 from voltha.core.config.config_backend import EtcdStore
 from voltha.registry import registry
-from voltha.adapters.openolt.protos import openolt_pb2
+from voltha.protos import tech_profile_pb2
 
 # logger
 log = structlog.get_logger()
@@ -65,7 +65,7 @@
     DEFAULT_ADDITIONAL_BW = 'auto'
     DEFAULT_PRIORITY = 0
     DEFAULT_WEIGHT = 0
-    DEFAULT_Q_SCHED_POLICY = 'hybrid'
+    DEFAULT_Q_SCHED_POLICY = 'Hybrid'
 
     def __init__(self, direction, additional_bw=DEFAULT_ADDITIONAL_BW,
                  priority=DEFAULT_PRIORITY,
@@ -173,13 +173,13 @@
                 host, port = self.args.etcd.split(':', 1)
                 self._kv_store = EtcdStore(
                     host, port, TechProfile.
-                    KV_STORE_TECH_PROFILE_PATH_PREFIX)
+                        KV_STORE_TECH_PROFILE_PATH_PREFIX)
             elif self.args.backend == 'consul':
                 # KV store's IP Address and PORT
                 host, port = self.args.consul.split(':', 1)
                 self._kv_store = ConsulStore(
                     host, port, TechProfile.
-                    KV_STORE_TECH_PROFILE_PATH_PREFIX)
+                        KV_STORE_TECH_PROFILE_PATH_PREFIX)
 
             # self.tech_profile_instance_store = dict()
         except Exception as e:
@@ -220,6 +220,8 @@
                 log.debug(
                     "Created-tech-profile-instance-with-values-from-kvstore")
             else:
+                # Create a default Tech-Profile.
+                # The default profile is a 1 TCONT, 1 GEM port model.
                 tech_profile = self._default_tech_profile()
                 log.debug(
                     "Created-tech-profile-instance-with-default-values")
@@ -427,7 +429,7 @@
     @staticmethod
     def get_us_scheduler(tech_profile_instance):
         # upstream scheduler
-        us_scheduler = openolt_pb2.Scheduler(
+        us_scheduler = tech_profile_pb2.SchedulerConfig(
             direction=TechProfile.get_parameter(
                 'direction', tech_profile_instance.us_scheduler.
                     direction),
@@ -444,7 +446,7 @@
 
     @staticmethod
     def get_ds_scheduler(tech_profile_instance):
-        ds_scheduler = openolt_pb2.Scheduler(
+        ds_scheduler = tech_profile_pb2.SchedulerConfig(
             direction=TechProfile.get_parameter(
                 'direction', tech_profile_instance.ds_scheduler.
                     direction),
@@ -460,20 +462,20 @@
         return ds_scheduler
 
     @staticmethod
-    def get_tconts(tech_profile_instance, us_scheduler=None, ds_scheduler=None):
+    def get_traffic_scheds(tech_profile_instance, us_scheduler=None, ds_scheduler=None):
         if us_scheduler is None:
             us_scheduler = TechProfile.get_us_scheduler(tech_profile_instance)
         if ds_scheduler is None:
             ds_scheduler = TechProfile.get_ds_scheduler(tech_profile_instance)
 
-        tconts = [openolt_pb2.Tcont(direction=TechProfile.get_parameter(
+        tconts = [tech_profile_pb2.TrafficScheduler(direction=TechProfile.get_parameter(
             'direction',
             tech_profile_instance.
                 us_scheduler.direction),
             alloc_id=tech_profile_instance.
                 us_scheduler.alloc_id,
             scheduler=us_scheduler),
-            openolt_pb2.Tcont(direction=TechProfile.get_parameter(
+            tech_profile_pb2.TrafficScheduler(direction=TechProfile.get_parameter(
                 'direction',
                 tech_profile_instance.
                     ds_scheduler.direction),
@@ -484,23 +486,88 @@
         return tconts
 
     @staticmethod
+    def get_traffic_queues(tech_profile_instance):
+        gemports = list()
+        # Upstream Gemports
+        for i in range(len(tech_profile_instance.
+                                   upstream_gem_port_attribute_list)):
+            gemports.append(tech_profile_pb2.TrafficQueue(
+                direction=TechProfile.get_parameter('direction',
+                                                    tech_profile_instance.
+                                                    us_scheduler.direction),
+                gemport_id=tech_profile_instance.
+                    upstream_gem_port_attribute_list[i].gemport_id,
+                pbit_map=tech_profile_instance.
+                    upstream_gem_port_attribute_list[i].pbit_map,
+                aes_encryption=ast.literal_eval(tech_profile_instance.
+                                                upstream_gem_port_attribute_list[i].aes_encryption),
+                sched_policy=TechProfile.get_parameter(
+                    'sched_policy', tech_profile_instance.
+                        upstream_gem_port_attribute_list[i].
+                        scheduling_policy),
+                priority=tech_profile_instance.
+                    upstream_gem_port_attribute_list[i].priority_q,
+                weight=tech_profile_instance.
+                    upstream_gem_port_attribute_list[i].weight,
+                discard_policy=TechProfile.get_parameter(
+                    'discard_policy', tech_profile_instance.
+                        upstream_gem_port_attribute_list[i].
+                        discard_policy)))
+
+        # Downstream Gemports
+        for i in range(len(tech_profile_instance.
+                                   downstream_gem_port_attribute_list)):
+            gemports.append(tech_profile_pb2.TrafficQueue(
+                direction=TechProfile.get_parameter('direction',
+                                                    tech_profile_instance.
+                                                    ds_scheduler.direction),
+                gemport_id=tech_profile_instance.
+                    downstream_gem_port_attribute_list[i].gemport_id,
+                pbit_map=tech_profile_instance.
+                    downstream_gem_port_attribute_list[i].pbit_map,
+                aes_encryption=ast.literal_eval(tech_profile_instance.
+                                                downstream_gem_port_attribute_list[i].aes_encryption),
+                sched_policy=TechProfile.get_parameter(
+                    'sched_policy', tech_profile_instance.
+                        downstream_gem_port_attribute_list[i].
+                        scheduling_policy),
+                priority=tech_profile_instance.
+                    downstream_gem_port_attribute_list[i].priority_q,
+                weight=tech_profile_instance.
+                    downstream_gem_port_attribute_list[i].weight,
+                discard_policy=TechProfile.get_parameter(
+                    'discard_policy', tech_profile_instance.
+                        downstream_gem_port_attribute_list[i].
+                        discard_policy)))
+        return gemports
+
+    @staticmethod
+    def get_us_traffic_scheduler(tech_profile_instance):
+        us_scheduler = TechProfile.get_us_scheduler(tech_profile_instance)
+        return tech_profile_pb2.TrafficScheduler(direction=TechProfile.get_parameter(
+            'direction',
+            us_scheduler.direction),
+            alloc_id=us_scheduler.alloc_id,
+            scheduler=us_scheduler)
+
+    @staticmethod
     def get_parameter(param_type, param_value):
         parameter = None
         try:
             if param_type == 'direction':
-                for direction in openolt_pb2.Direction.keys():
+                for direction in tech_profile_pb2.Direction.keys():
                     if param_value == direction:
                         parameter = direction
             elif param_type == 'discard_policy':
-                for discard_policy in openolt_pb2.DiscardPolicy.keys():
+                for discard_policy in tech_profile_pb2.DiscardPolicy.keys():
                     if param_value == discard_policy:
                         parameter = discard_policy
-            elif param_type == 'sched_policy':
-                for sched_policy in openolt_pb2.SchedulingPolicy.keys():
+            elif param_type == 'q_sched_policy':
+                for sched_policy in tech_profile_pb2.SchedulingPolicy.keys():
                     if param_value == sched_policy:
                         parameter = sched_policy
             elif param_type == 'additional_bw':
-                for bw_component in openolt_pb2.AdditionalBW.keys():
+                for bw_component in tech_profile_pb2.AdditionalBW.keys():
                     if param_value == bw_component:
                         parameter = bw_component
         except BaseException as e:
@@ -510,26 +577,17 @@
 
 class TechProfileInstance(object):
     def __init__(self, subscriber_identifier, tech_profile, resource_mgr,
-                 intf_id, num_of_tconts=1):
+                 intf_id):
         if tech_profile is not None:
             self.subscriber_identifier = subscriber_identifier
-            self.num_of_tconts = num_of_tconts
-            self.num_of_gem_ports = tech_profile.num_gem_ports
-            self.name = tech_profile.name
-            self.profile_type = tech_profile.profile_type
-            self.version = tech_profile.version
-            self.instance_control = tech_profile.instance_control
 
-            # TODO: Fixed num_of_tconts to 1 per TP Instance.
-            # This may change in future
-            assert (num_of_tconts == 1)
             # Get alloc id and gemport id using resource manager
             alloc_id = resource_mgr.get_resource_id(intf_id,
                                                     'ALLOC_ID',
-                                                    num_of_tconts)
+                                                    1)
             gem_ports = resource_mgr.get_resource_id(intf_id,
                                                      'GEMPORT_ID',
-                                                     self.num_of_gem_ports)
+                                                     tech_profile.num_gem_ports)
 
             gemport_list = list()
             if isinstance(gem_ports, int):
@@ -547,7 +605,7 @@
 
             self.upstream_gem_port_attribute_list = list()
             self.downstream_gem_port_attribute_list = list()
-            for i in range(self.num_of_gem_ports):
+            for i in range(tech_profile.num_gem_ports):
                 self.upstream_gem_port_attribute_list.append(
                     TechProfileInstance.IGemPortAttribute(
                         gemport_list[i],