Shorten VDU names
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / rwnsmtasklet.py
index de21b5c..da9807a 100755 (executable)
@@ -1,4 +1,4 @@
-# 
+#
 #   Copyright 2016 RIFT.IO Inc
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +25,8 @@ import tempfile
 import time
 import uuid
 import yaml
+import requests
+import json
 
 
 from collections import deque
@@ -218,7 +220,6 @@ class VnffgRecord(object):
 
         if self._vnffgr_state == VnffgRecordState.INIT:
             vnffgr_dict = {"id": self._vnffgr_id,
-                           "nsd_id": self._nsr.nsd_id,
                            "vnffgd_id_ref": self._vnffgd_msg.id,
                            "vnffgd_name_ref": self._vnffgd_msg.name,
                            "sdn_account": self._sdn_account_name,
@@ -227,7 +228,6 @@ class VnffgRecord(object):
             vnffgr = NsrYang.YangData_Nsr_NsInstanceOpdata_Nsr_Vnffgr.from_dict(vnffgr_dict)
         elif self._vnffgr_state == VnffgRecordState.TERMINATED:
             vnffgr_dict = {"id": self._vnffgr_id,
-                           "nsd_id": self._nsr.nsd_id,
                            "vnffgd_id_ref": self._vnffgd_msg.id,
                            "vnffgd_name_ref": self._vnffgd_msg.name,
                            "sdn_account": self._sdn_account_name,
@@ -241,7 +241,6 @@ class VnffgRecord(object):
                 self._log.exception("Fetching VNFFGR for VNFFG with id %s failed", self._vnffgr_id)
                 self._vnffgr_state = VnffgRecordState.FAILED
                 vnffgr_dict = {"id": self._vnffgr_id,
-                               "nsd_id": self._nsr.nsd_id,
                                "vnffgd_id_ref": self._vnffgd_msg.id,
                                "vnffgd_name_ref": self._vnffgd_msg.name,
                                "sdn_account": self._sdn_account_name,
@@ -255,7 +254,6 @@ class VnffgRecord(object):
     def vnffgr_create_msg(self):
         """ Virtual Link Record message for Creating VLR in VNS """
         vnffgr_dict = {"id": self._vnffgr_id,
-                       "nsd_id": self._nsr.nsd_id,
                        "vnffgd_id_ref": self._vnffgd_msg.id,
                        "vnffgd_name_ref": self._vnffgd_msg.name,
                        "sdn_account": self._sdn_account_name,
@@ -458,9 +456,10 @@ class VnffgRecord(object):
 
 class VirtualLinkRecord(object):
     """ Virtual Link Records class"""
+    XPATH = "D,/vlr:vlr-catalog/vlr:vlr"
     @staticmethod
     @asyncio.coroutine
-    def create_record(dts, log, loop, nsr_name, vld_msg, cloud_account_name, ip_profile, nsr_id, restart_mode=False):
+    def create_record(dts, log, loop, nsr_name, vld_msg, cloud_account_name, om_datacenter, ip_profile, nsr_id, restart_mode=False):
         """Creates a new VLR object based on the given data.
 
         If restart mode is enabled, then we look for existing records in the
@@ -476,6 +475,7 @@ class VirtualLinkRecord(object):
                       nsr_name,
                       vld_msg,
                       cloud_account_name,
+                      om_datacenter,
                       ip_profile,
                       nsr_id,
                       )
@@ -498,20 +498,22 @@ class VirtualLinkRecord(object):
 
         return vlr_obj
 
-    def __init__(self, dts, log, loop, nsr_name, vld_msg, cloud_account_name, ip_profile, nsr_id):
+    def __init__(self, dts, log, loop, nsr_name, vld_msg, cloud_account_name, om_datacenter, ip_profile, nsr_id):
         self._dts = dts
         self._log = log
         self._loop = loop
         self._nsr_name = nsr_name
         self._vld_msg = vld_msg
         self._cloud_account_name = cloud_account_name
+        self._om_datacenter_name = om_datacenter
         self._assigned_subnet = None
         self._nsr_id = nsr_id
         self._ip_profile = ip_profile
         self._vlr_id = str(uuid.uuid4())
         self._state = VlRecordState.INIT
         self._prev_state = None
-        
+        self._create_time = int(time.time())
+
     @property
     def xpath(self):
         """ path for this object """
@@ -556,6 +558,11 @@ class VirtualLinkRecord(object):
         """ Cloud account that this VLR should be created in """
         return self._cloud_account_name
 
+    @property
+    def om_datacenter_name(self):
+        """ Datacenter  that this VLR should be created in """
+        return self._om_datacenter_name
+
     @staticmethod
     def vlr_xpath(vlr):
         """ Get the VLR path from VLR """
@@ -599,7 +606,9 @@ class VirtualLinkRecord(object):
                     "nsr_id_ref": self._nsr_id,
                     "vld_ref": self.vld_msg.id,
                     "name": self.name,
+                    "create_time": self._create_time,
                     "cloud_account": self.cloud_account_name,
+                    "om_datacenter": self.om_datacenter_name,
                     }
 
         if self._ip_profile and self._ip_profile.has_field('ip_profile_params'):
@@ -618,12 +627,14 @@ class VirtualLinkRecord(object):
         nsr_vlr.vlr_ref = self._vlr_id
         nsr_vlr.assigned_subnet = self.assigned_subnet
         nsr_vlr.cloud_account = self.cloud_account_name
+        nsr_vlr.om_datacenter = self.om_datacenter_name
 
         for conn in self.vld_msg.vnfd_connection_point_ref:
             for vnfr in vnfrs:
                 if (vnfr.vnfd.id == conn.vnfd_id_ref and
                         vnfr.member_vnf_index == conn.member_vnf_index_ref and
-                        self.cloud_account_name == vnfr.cloud_account_name):
+                        self.cloud_account_name == vnfr.cloud_account_name and
+                        self.om_datacenter_name == vnfr.om_datacenter_name):
                     cp_entry = nsr_vlr.vnfr_connection_point_ref.add()
                     cp_entry.vnfr_id = vnfr.id
                     cp_entry.connection_point = conn.vnfd_connection_point_ref
@@ -633,7 +644,6 @@ class VirtualLinkRecord(object):
     @asyncio.coroutine
     def instantiate(self):
         """ Instantiate this VL """
-
         self._log.debug("Instaniating VLR key %s, vld %s",
                         self.xpath, self._vld_msg)
         vlr = None
@@ -712,7 +722,7 @@ class VirtualNetworkFunctionRecord(object):
     @staticmethod
     @asyncio.coroutine
     def create_record(dts, log, loop, vnfd, const_vnfd_msg, nsd_id, nsr_name,
-                cloud_account_name, nsr_id, group_name, group_instance_id,
+                cloud_account_name, om_datacenter_name, nsr_id, group_name, group_instance_id,
                 placement_groups, restart_mode=False):
         """Creates a new VNFR object based on the given data.
 
@@ -731,6 +741,7 @@ class VirtualNetworkFunctionRecord(object):
                           nsd_id,
                           nsr_name,
                           cloud_account_name,
+                          om_datacenter_name,
                           nsr_id,
                           group_name,
                           group_instance_id,
@@ -761,6 +772,7 @@ class VirtualNetworkFunctionRecord(object):
                  nsd_id,
                  nsr_name,
                  cloud_account_name,
+                 om_datacenter_name,
                  nsr_id,
                  group_name=None,
                  group_instance_id=None,
@@ -775,10 +787,12 @@ class VirtualNetworkFunctionRecord(object):
         self._nsr_name = nsr_name
         self._nsr_id = nsr_id
         self._cloud_account_name = cloud_account_name
+        self._om_datacenter_name = om_datacenter_name
         self._group_name = group_name
         self._group_instance_id = group_instance_id
         self._placement_groups = placement_groups
         self._config_status = NsrYang.ConfigStates.INIT
+        self._create_time = int(time.time())
 
         self._prev_state = VnfRecordState.INIT
         self._state = VnfRecordState.INIT
@@ -816,7 +830,7 @@ class VirtualNetworkFunctionRecord(object):
     @property
     def const_vnfr_msg(self):
         """ VNFR message """
-        return RwNsrYang.YangData_Nsr_NsInstanceOpdata_Nsr_ConstituentVnfrRef(vnfr_id=self.id,cloud_account=self.cloud_account_name)
+        return RwNsrYang.YangData_Nsr_NsInstanceOpdata_Nsr_ConstituentVnfrRef(vnfr_id=self.id,cloud_account=self.cloud_account_name,om_datacenter=self._om_datacenter_name)
 
     @property
     def vnfd(self):
@@ -828,6 +842,11 @@ class VirtualNetworkFunctionRecord(object):
         """ Cloud account that this VNF should be created in """
         return self._cloud_account_name
 
+    @property
+    def om_datacenter_name(self):
+        """ Datacenter that this VNF should be created in """
+        return self._om_datacenter_name
+
 
     @property
     def active(self):
@@ -930,14 +949,16 @@ class VirtualNetworkFunctionRecord(object):
         vnfr_dict = {
                 "id": self.id,
                 "nsr_id_ref": self._nsr_id,
-                "vnfd_ref": self.vnfd.id,
                 "name": self.name,
                 "cloud_account": self._cloud_account_name,
+                "om_datacenter": self._om_datacenter_name,
                 "config_status": self.config_status
                 }
         vnfr_dict.update(vnfd_copy_dict)
 
         vnfr = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict(vnfr_dict)
+        vnfr.vnfd = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vnfd.from_dict(self.vnfd.as_dict(),
+                                                                          ignore_missing_keys=True)
         vnfr.member_vnf_index_ref = self.member_vnf_index
         vnfr.vnf_configuration.from_dict(self._vnfd.vnf_configuration.as_dict())
 
@@ -1051,6 +1072,9 @@ class VirtualNetworkFunctionRecord(object):
             cpr = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_ConnectionPoint()
             cpr.name = conn_p.name
             cpr.type_yang = conn_p.type_yang
+            if conn_p.has_field('port_security_enabled'):
+              cpr.port_security_enabled = conn_p.port_security_enabled
+
             vlr_ref = find_vlr_for_cp(conn_p)
             if vlr_ref is None:
                 msg = "Failed to find VLR for cp = %s" % conn_p.name
@@ -1061,7 +1085,7 @@ class VirtualNetworkFunctionRecord(object):
             cpr.vlr_ref = vlr_ref.id
             self.vnfr_msg.connection_point.append(cpr)
             self._log.debug("Connection point [%s] added, vnf id=%s vnfd id=%s",
-                            cpr, self.vnfr_msg.id, self.vnfr_msg.vnfd_ref)
+                            cpr, self.vnfr_msg.id, self.vnfr_msg.vnfd.id)
 
         if not self.restart_mode:
             yield from self._dts.query_create(self.xpath,
@@ -1210,7 +1234,8 @@ class NetworkServiceRecord(object):
     """ Network service record """
     XPATH = "D,/nsr:ns-instance-opdata/nsr:nsr"
 
-    def __init__(self, dts, log, loop, nsm, nsm_plugin, nsr_cfg_msg, sdn_account_name, restart_mode=False):
+    def __init__(self, dts, log, loop, nsm, nsm_plugin, nsr_cfg_msg, sdn_account_name, key_pairs, restart_mode=False,
+                 vlr_handler=None):
         self._dts = dts
         self._log = log
         self._loop = loop
@@ -1218,10 +1243,12 @@ class NetworkServiceRecord(object):
         self._nsr_cfg_msg = nsr_cfg_msg
         self._nsm_plugin = nsm_plugin
         self._sdn_account_name = sdn_account_name
+        self._vlr_handler = vlr_handler
 
         self._nsd = None
         self._nsr_msg = None
         self._nsr_regh = None
+        self._key_pairs = key_pairs
         self._vlrs = []
         self._vnfrs = {}
         self._vnfds = {}
@@ -1239,6 +1266,7 @@ class NetworkServiceRecord(object):
         self._is_active = False
         self._vl_phase_completed = False
         self._vnf_phase_completed = False
+        self.vlr_uptime_tasks = {}
 
 
         # Initalise the state to init
@@ -1268,6 +1296,7 @@ class NetworkServiceRecord(object):
             self._vnf_phase_completed = True
 
         self._op_status.set_state(state)
+        self._nsm_plugin.set_state(self.id, state)
 
     @property
     def id(self):
@@ -1283,6 +1312,12 @@ class NetworkServiceRecord(object):
     def cloud_account_name(self):
         return self._nsr_cfg_msg.cloud_account
 
+    @property
+    def om_datacenter_name(self):
+        if self._nsr_cfg_msg.has_field('om_datacenter'):
+            return self._nsr_cfg_msg.om_datacenter
+        return None
+
     @property
     def state(self):
         """State of this NetworkServiceRecord"""
@@ -1380,11 +1415,11 @@ class NetworkServiceRecord(object):
     def _get_vnfd_cloud_account(self, vnfd_member_index):
         """  Fetch Cloud Account for the passed vnfd id """
         if self._nsr_cfg_msg.vnf_cloud_account_map:
-           vim_accounts = [vnf.cloud_account  for vnf in self._nsr_cfg_msg.vnf_cloud_account_map \
+           vim_accounts = [(vnf.cloud_account,vnf.om_datacenter)  for vnf in self._nsr_cfg_msg.vnf_cloud_account_map \
                            if vnfd_member_index == vnf.member_vnf_index_ref]
            if vim_accounts and vim_accounts[0]:
                return vim_accounts[0]
-        return self.cloud_account_name
+        return (self.cloud_account_name,self.om_datacenter_name)
 
     def _get_constituent_vnfd_msg(self, vnf_index):
         for const_vnfd in self.nsd_msg.constituent_vnfd:
@@ -1424,6 +1459,21 @@ class NetworkServiceRecord(object):
         for vlr in self._vlrs:
             yield from self.nsm_plugin.instantiate_vl(self, vlr)
             vlr.state = VlRecordState.ACTIVE
+            self.vlr_uptime_tasks[vlr.id] = self._loop.create_task(self.vlr_uptime_update(vlr))
+
+
+    def vlr_uptime_update(self, vlr):
+        try:
+
+            vlr_ = RwVlrYang.YangData_Vlr_VlrCatalog_Vlr.from_dict({'id': vlr.id})
+            while True:
+                vlr_.uptime = int(time.time()) - vlr._create_time
+                yield from self._vlr_handler.update(None, VirtualLinkRecord.vlr_xpath(vlr), vlr_)
+                yield from asyncio.sleep(2, loop=self._loop)
+        except asyncio.CancelledError:
+            self._log.debug("Received cancellation request for vlr_uptime_update task")
+            yield from self._vlr_handler.delete(None, VirtualLinkRecord.vlr_xpath(vlr))
+
 
     @asyncio.coroutine
     def create(self, config_xact):
@@ -1644,11 +1694,11 @@ class NetworkServiceRecord(object):
                 const_vnfd_msg = self._get_constituent_vnfd_msg(vnf_index)
                 vnfd_msg = self._get_vnfd(const_vnfd_msg.vnfd_id_ref, config_xact)
 
-                cloud_account_name = self._get_vnfd_cloud_account(const_vnfd_msg.member_vnf_index)
+                cloud_account_name, om_datacenter_name = self._get_vnfd_cloud_account(const_vnfd_msg.member_vnf_index)
                 if cloud_account_name is None:
                     cloud_account_name = self.cloud_account_name
                 for _ in range(count):
-                    vnfr = yield from self.create_vnf_record(vnfd_msg, const_vnfd_msg, cloud_account_name, group_name, index)
+                    vnfr = yield from self.create_vnf_record(vnfd_msg, const_vnfd_msg, cloud_account_name, om_datacenter_name, group_name, index)
                     scale_instance.add_vnfr(vnfr)
                     vnfrs.append(vnfr)
 
@@ -1779,19 +1829,20 @@ class NetworkServiceRecord(object):
             self._vnffgrs[vnffgr.id] = vnffgr
 
     def resolve_vld_ip_profile(self, nsd_msg, vld):
+        self._log.debug("Receieved ip profile ref is %s",vld.ip_profile_ref)
         if not vld.has_field('ip_profile_ref'):
             return None
-        profile = [ profile for profile in nsd_msg.ip_profiles if profile.name == vld.ip_profile_ref ]
+        profile = [profile for profile in nsd_msg.ip_profiles if profile.name == vld.ip_profile_ref]
         return profile[0] if profile else None
 
     @asyncio.coroutine
-    def _create_vls(self, vld, cloud_account):
+    def _create_vls(self, vld, cloud_account,om_datacenter):
         """Create a VLR in the cloud account specified using the given VLD
-        
+
         Args:
             vld : VLD yang obj
             cloud_account : Cloud account name
-        
+
         Returns:
             VirtualLinkRecord
         """
@@ -1802,6 +1853,7 @@ class NetworkServiceRecord(object):
                 self.name,
                 vld,
                 cloud_account,
+                om_datacenter,
                 self.resolve_vld_ip_profile(self.nsd_msg, vld),
                 self.id,
                 restart_mode=self.restart_mode)
@@ -1826,25 +1878,28 @@ class NetworkServiceRecord(object):
             # Handle case where cloud_account is None
             vnf_cloud_map = {}
             for vnf in self._nsr_cfg_msg.vnf_cloud_account_map:
-                if vnf.cloud_account is not None:
-                    vnf_cloud_map[vnf.member_vnf_index_ref] = vnf.cloud_account
+                if vnf.cloud_account is not None or vnf.om_datacenter is not None:
+                    vnf_cloud_map[vnf.member_vnf_index_ref] = (vnf.cloud_account,vnf.om_datacenter)
 
             for vnfc in vld.vnfd_connection_point_ref:
                 cloud_account = vnf_cloud_map.get(
                         vnfc.member_vnf_index_ref,
-                        self.cloud_account_name)
+                        (self.cloud_account_name,self.om_datacenter_name))
 
                 cloud_account_list.append(cloud_account)
 
         if self._nsr_cfg_msg.vl_cloud_account_map:
             for vld_map in self._nsr_cfg_msg.vl_cloud_account_map:
                 if vld_map.vld_id_ref == vld.id:
-                    cloud_account_list.extend(vld_map.cloud_accounts)
+                    for cloud_account in vld_map.cloud_accounts:
+                        cloud_account_list.extend((cloud_account,None))
+                    for om_datacenter in vld_map.om_datacenters:
+                        cloud_account_list.extend((None,om_datacenter))
 
         # If no config has been provided then fall-back to the default
         # account
         if not cloud_account_list:
-            cloud_account_list = [self.cloud_account_name]
+            cloud_account_list = [(self.cloud_account_name,self.om_datacenter_name)]
 
         self._log.debug("VL {} cloud accounts: {}".
                         format(vld.name, cloud_account_list))
@@ -1855,10 +1910,11 @@ class NetworkServiceRecord(object):
         """ This function creates VLs for every VLD in the NSD
         associated with this NSR"""
         for vld in self.nsd_msg.vld:
+
             self._log.debug("Found vld %s in nsr id %s", vld, self.id)
             cloud_account_list = self._extract_cloud_accounts_for_vl(vld)
-            for account in cloud_account_list:
-                vlr = yield from self._create_vls(vld, account)
+            for cloud_account,om_datacenter in cloud_account_list:
+                vlr = yield from self._create_vls(vld, cloud_account,om_datacenter)
                 self._vlrs.append(vlr)
 
 
@@ -1881,8 +1937,8 @@ class NetworkServiceRecord(object):
 
         if vlr is None:
             cloud_account_list = self._extract_cloud_accounts_for_vl(vld)
-            for account in cloud_account_list:
-                vlr = yield from self._create_vls(vld, account)
+            for account,om_datacenter in cloud_account_list:
+                vlr = yield from self._create_vls(vld, account,om_datacenter)
                 self._vlrs.append(vlr)
 
         vlr.state = VlRecordState.INSTANTIATION_PENDING
@@ -1941,10 +1997,10 @@ class NetworkServiceRecord(object):
                 continue
 
             vnfd_msg = self._get_vnfd(const_vnfd.vnfd_id_ref, config_xact)
-            cloud_account_name = self._get_vnfd_cloud_account(const_vnfd.member_vnf_index)
+            cloud_account_name,om_datacenter_name = self._get_vnfd_cloud_account(const_vnfd.member_vnf_index)
             if cloud_account_name is None:
                 cloud_account_name = self.cloud_account_name
-            yield from self.create_vnf_record(vnfd_msg, const_vnfd, cloud_account_name)
+            yield from self.create_vnf_record(vnfd_msg, const_vnfd, cloud_account_name, om_datacenter_name)
 
 
     def get_placement_groups(self, vnfd_msg, const_vnfd):
@@ -1966,14 +2022,15 @@ class NetworkServiceRecord(object):
         return placement_groups
 
     @asyncio.coroutine
-    def create_vnf_record(self, vnfd_msg, const_vnfd, cloud_account_name, group_name=None, group_instance_id=None):
+    def create_vnf_record(self, vnfd_msg, const_vnfd, cloud_account_name, om_datacenter_name, group_name=None, group_instance_id=None):
         # Fetch the VNFD associated with this VNF
         placement_groups = self.get_placement_groups(vnfd_msg, const_vnfd)
         self._log.info("Cloud Account for VNF %d is %s",const_vnfd.member_vnf_index,cloud_account_name)
-        self._log.info("Launching VNF: %s (Member Index: %s) in NSD plancement Groups: %s",
+        self._log.info("Launching VNF: %s (Member Index: %s) in NSD plancement Groups: %s, restart mode self.restart_mode %s",
                        vnfd_msg.name,
                        const_vnfd.member_vnf_index,
-                       [ group.name for group in placement_groups])
+                       [ group.name for group in placement_groups],
+                       self.restart_mode)
         vnfr = yield from VirtualNetworkFunctionRecord.create_record(self._dts,
                                             self._log,
                                             self._loop,
@@ -1982,6 +2039,7 @@ class NetworkServiceRecord(object):
                                             self.nsd_id,
                                             self.name,
                                             cloud_account_name,
+                                            om_datacenter_name,
                                             self.id,
                                             group_name,
                                             group_instance_id,
@@ -2365,6 +2423,8 @@ class NetworkServiceRecord(object):
             for vlr in self.vlrs:
                 yield from self.nsm_plugin.terminate_vl(vlr)
                 vlr.state = VlRecordState.TERMINATED
+                if vlr.id in self.vlr_uptime_tasks:
+                    self.vlr_uptime_tasks[vlr.id].cancel()
 
         self._log.debug("Terminating network service id %s", self.id)
 
@@ -2438,6 +2498,7 @@ class NetworkServiceRecord(object):
         nsr.config_status = self.map_config_status()
         nsr.config_status_details = self._config_status_details
         nsr.create_time = self._create_time
+        nsr.uptime = int(time.time()) - self._create_time
 
         for cfg_prim in self.nsd_msg.service_primitive:
             cfg_prim = NsrYang.YangData_Nsr_NsInstanceOpdata_Nsr_ServicePrimitive.from_dict(
@@ -2870,8 +2931,10 @@ class NsrRpcDtsHandler(object):
     EXEC_NSR_CONF_O_XPATH = "O,/nsr:start-network-service"
     NETCONF_IP_ADDRESS = "127.0.0.1"
     NETCONF_PORT = 2022
+    RESTCONF_PORT = 8888
     NETCONF_USER = "admin"
     NETCONF_PW = "admin"
+    REST_BASE_V2_URL = 'https://{}:{}/v2/api/'.format("127.0.0.1",8888)
 
     def __init__(self, dts, log, loop, nsm):
         self._dts = dts
@@ -2883,6 +2946,7 @@ class NsrRpcDtsHandler(object):
         self._ns_regh = None
 
         self._manager = None
+        self._nsr_config_url = NsrRpcDtsHandler.REST_BASE_V2_URL + 'config/ns-instance-config'
 
         self._model = RwYang.Model.create_libncx()
         self._model.load_schema_ypbc(RwNsrYang.get_schema())
@@ -2928,6 +2992,12 @@ class NsrRpcDtsHandler(object):
         raise NsrInstantiationFailed("Failed to connect to Launchpad within %s seconds" %
                                       timeout_secs)
 
+    def _apply_ns_instance_config(self,payload_dict):
+        #self._log.debug("At apply NS instance config with payload %s",payload_dict)
+        req_hdr= {'accept':'application/vnd.yang.data+json','content-type':'application/vnd.yang.data+json'}
+        response=requests.post(self._nsr_config_url, headers=req_hdr, auth=('admin', 'admin'),data=payload_dict,verify=False)
+        return response
+
     @asyncio.coroutine
     def register(self):
         """ Register for NS monitoring read from dts """
@@ -2939,21 +3009,22 @@ class NsrRpcDtsHandler(object):
             rpc_op = NsrYang.YangOutput_Nsr_StartNetworkService.from_dict({
                     "nsr_id":str(uuid.uuid4())
                 })
-            
-            if not ('name' in rpc_ip and  'nsd_ref' in rpc_ip and 'cloud_account' in rpc_ip):
+
+            if not ('name' in rpc_ip and  'nsd_ref' in rpc_ip and ('cloud_account' in rpc_ip or 'om_datacenter' in rpc_ip)):
                 self._log.error("Mandatory parameters name or nsd_ref or cloud account not found in start-network-service {}".format(rpc_ip))
-                
+
 
             self._log.debug("start-network-service RPC input: {}".format(rpc_ip))
 
             try:
                 # Add used value to the pool
                 self._log.debug("RPC output: {}".format(rpc_op))
+
                 nsd_copy = self.nsm.get_nsd(rpc_ip.nsd_ref)
 
-                if not self._manager:
-                    self._manager = yield from self._connect()
-        
+                #if not self._manager:
+                #    self._manager = yield from self._connect()
+
                 self._log.debug("Configuring ns-instance-config with name  %s nsd-ref: %s",
                         rpc_ip.name, rpc_ip.nsd_ref)
 
@@ -2966,17 +3037,26 @@ class NsrRpcDtsHandler(object):
                 ns_instance_config.nsd = NsrYang.YangData_Nsr_NsInstanceConfig_Nsr_Nsd()
                 ns_instance_config.nsd.from_dict(nsd_copy.msg.as_dict())
 
-                xml = ns_instance_config.to_xml_v2(self._model)
-                netconf_xml = self.wrap_netconf_config_xml(xml)
-
-                self._log.debug("Sending configure ns-instance-config xml to %s: %s",
-                        netconf_xml, NsrRpcDtsHandler.NETCONF_IP_ADDRESS)
-
-                response = yield from self._manager.edit_config(
-                           target="running",
-                           config=netconf_xml,
-                           )
-                self._log.debug("Received edit config response: %s", str(response))
+                payload_dict = ns_instance_config.to_json(self._model)
+                #xml = ns_instance_config.to_xml_v2(self._model)
+                #netconf_xml = self.wrap_netconf_config_xml(xml)
+
+                #self._log.debug("Sending configure ns-instance-config xml to %s: %s",
+                #        netconf_xml, NsrRpcDtsHandler.NETCONF_IP_ADDRESS)
+                self._log.debug("Sending configure ns-instance-config json to %s: %s",
+                        self._nsr_config_url,ns_instance_config)
+
+                #response = yield from self._manager.edit_config(
+                #           target="running",
+                #           config=netconf_xml,
+                #           )
+                response = yield from self._loop.run_in_executor(
+                    None,
+                    self._apply_ns_instance_config,
+                    payload_dict
+                    )
+                response.raise_for_status()
+                self._log.debug("Received edit config response: %s", response.json())
 
                 xact_info.respond_xpath(rwdts.XactRspCode.ACK,
                                         NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH,
@@ -3002,6 +3082,7 @@ class NsrDtsHandler(object):
     """ The network service DTS handler """
     NSR_XPATH = "C,/nsr:ns-instance-config/nsr:nsr"
     SCALE_INSTANCE_XPATH = "C,/nsr:ns-instance-config/nsr:nsr/nsr:scaling-group/nsr:instance"
+    KEY_PAIR_XPATH = "C,/nsr:key-pair"
 
     def __init__(self, dts, log, loop, nsm):
         self._dts = dts
@@ -3011,6 +3092,7 @@ class NsrDtsHandler(object):
 
         self._nsr_regh = None
         self._scale_regh = None
+        self._key_pair_regh = None
 
     @property
     def nsm(self):
@@ -3144,12 +3226,20 @@ class NsrDtsHandler(object):
 
             return added_cfgs, deleted_cfgs, updated_cfgs
 
+        def get_nsr_key_pairs(dts_member_reg, xact):
+            key_pairs = {}
+            for instance_cfg, keyspec in dts_member_reg.get_xact_elements(xact, include_keyspec=True):
+                self._log.debug("Key pair received is {} KS: {}".format(instance_cfg, keyspec))
+                xpath = keyspec.to_xpath(RwNsrYang.get_schema())
+                key_pairs[instance_cfg.name] = instance_cfg
+            return key_pairs
+
         def on_apply(dts, acg, xact, action, scratch):
             """Apply the  configuration"""
             self._log.debug("Got nsr apply (xact: %s) (action: %s)(scr: %s)",
                             xact, action, scratch)
 
-            def handle_create_nsr(msg, restart_mode=False):
+            def handle_create_nsr(msg, key_pairs=None, restart_mode=False):
                 # Handle create nsr requests """
                 # Do some validations
                 if not msg.has_field("nsd"):
@@ -3159,7 +3249,7 @@ class NsrDtsHandler(object):
 
                 self._log.debug("Creating NetworkServiceRecord %s  from nsr config  %s",
                                msg.id, msg.as_dict())
-                nsr = self.nsm.create_nsr(msg, restart_mode=restart_mode)
+                nsr = self.nsm.create_nsr(msg, key_pairs=key_pairs, restart_mode=restart_mode)
                 return nsr
 
             def handle_delete_nsr(msg):
@@ -3190,8 +3280,11 @@ class NsrDtsHandler(object):
                             xact, action, scratch)
 
             if action == rwdts.AppconfAction.INSTALL and xact.id is None:
+                key_pairs = []
+                for element in self._key_pair_regh.elements:
+                    key_pairs.append(element)
                 for element in self._nsr_regh.elements:
-                    nsr = handle_create_nsr(element, restart_mode=True)
+                    nsr = handle_create_nsr(element, key_pairs, restart_mode=True)
                     self._loop.create_task(begin_instantiation(nsr))
 
 
@@ -3205,7 +3298,8 @@ class NsrDtsHandler(object):
             for msg in added_msgs:
                 if msg.id not in self._nsm.nsrs:
                     self._log.info("Create NSR received in on_apply to instantiate NS:%s", msg.id)
-                    nsr = handle_create_nsr(msg)
+                    key_pairs = get_nsr_key_pairs(self._key_pair_regh, xact)
+                    nsr = handle_create_nsr(msg,key_pairs)
                     self._loop.create_task(begin_instantiation(nsr))
 
             for msg in deleted_msgs:
@@ -3322,6 +3416,11 @@ class NsrDtsHandler(object):
                                       flags=rwdts.Flag.SUBSCRIBER | rwdts.Flag.DELTA_READY| rwdts.Flag.CACHE,
                                       )
 
+            self._key_pair_regh = acg.register(
+                                      xpath=NsrDtsHandler.KEY_PAIR_XPATH,
+                                      flags=rwdts.Flag.SUBSCRIBER | rwdts.Flag.DELTA_READY | rwdts.Flag.CACHE,
+                                       )
+
 
 class NsrOpDataDtsHandler(object):
     """ The network service op data DTS handler """
@@ -3718,7 +3817,7 @@ class NsManager(object):
         #           msg.nsr_id_ref,
         #           msg.scaling_group_name_ref,
         #           msg.instance_id)
-    
+
     def nsr_update_cfg(self, nsr_id, msg):
         nsr = self._nsrs[nsr_id]
         nsr.nsr_cfg_msg= msg
@@ -3741,16 +3840,17 @@ class NsManager(object):
         # Not calling in a separate task as this is called from a separate task
         yield from nsr.delete_vl_instance(vld)
 
-    def create_nsr(self, nsr_msg, restart_mode=False):
+    def create_nsr(self, nsr_msg, key_pairs=None,restart_mode=False):
         """ Create an NSR instance """
         if nsr_msg.id in self._nsrs:
             msg = "NSR id %s already exists" % nsr_msg.id
             self._log.error(msg)
             raise NetworkServiceRecordError(msg)
 
-        self._log.info("Create NetworkServiceRecord nsr id %s from nsd_id %s",
+        self._log.info("Create NetworkServiceRecord nsr id %s from nsd_id %s, restart mode %s",
                        nsr_msg.id,
-                       nsr_msg.nsd.id)
+                       nsr_msg.nsd.id,
+                       restart_mode)
 
         nsm_plugin = self._ro_plugin_selector.ro_plugin
         sdn_account_name = self._cloud_account_handler.get_cloud_account_sdn_name(nsr_msg.cloud_account)
@@ -3762,10 +3862,12 @@ class NsManager(object):
                                    nsm_plugin,
                                    nsr_msg,
                                    sdn_account_name,
-                                   restart_mode=restart_mode
+                                   key_pairs,
+                                   restart_mode=restart_mode,
+                                   vlr_handler=self._ro_plugin_selector._records_publisher._vlr_pub_hdlr
                                    )
         self._nsrs[nsr_msg.id] = nsr
-        nsm_plugin.create_nsr(nsr_msg, nsr_msg.nsd)
+        nsm_plugin.create_nsr(nsr_msg, nsr_msg.nsd, key_pairs)
 
         return nsr
 
@@ -3893,7 +3995,7 @@ class NsManager(object):
             self.create_nsd(nsd)
         else:
             self._log.debug("Updating NSD id = %s, nsd = %s", nsd.id, nsd)
-            self._nsds[nsd.id].update(nsd)  
+            self._nsds[nsd.id].update(nsd)
 
     def delete_nsd(self, nsd_id):
         """ Delete the Network service descriptor with the passed id """
@@ -3945,9 +4047,6 @@ class NsManager(object):
         """ Update the virtual network function descriptor """
         self._log.debug("Update virtual network function descriptor- %s", vnfd)
 
-        # Hack to remove duplicates from leaf-lists - to be fixed by RIFT-6511
-        for ivld in vnfd.internal_vld:
-            ivld.internal_connection_point_ref = list(set(ivld.internal_connection_point_ref))
 
         if vnfd.id not in self._vnfds:
             self._log.debug("No VNFD found - creating VNFD id = %s", vnfd.id)
@@ -4027,7 +4126,10 @@ class NsManager(object):
 
         # Terminate the instances/networks assocaited with this nw service
         self._log.debug("Terminating the network service %s", nsr_id)
-        yield from self._nsrs[nsr_id].terminate()
+        try :
+            yield from self._nsrs[nsr_id].terminate()
+        except Exception as e:
+            self.log.exception("Failed to terminate NSR[id=%s]", nsr_id)
 
         # Unref the NSD
         yield from self.nsd_unref_by_nsr_id(nsr_id)