RIFT-14721
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / rwnsmtasklet.py
index bb21767..9877ae9 100755 (executable)
@@ -25,6 +25,8 @@ import tempfile
 import time
 import uuid
 import yaml
+import requests
+import json
 
 
 from collections import deque
@@ -1229,7 +1231,7 @@ 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):
         self._dts = dts
         self._log = log
         self._loop = loop
@@ -1241,6 +1243,7 @@ class NetworkServiceRecord(object):
         self._nsd = None
         self._nsr_msg = None
         self._nsr_regh = None
+        self._key_pairs = key_pairs
         self._vlrs = []
         self._vnfrs = {}
         self._vnfds = {}
@@ -2900,8 +2903,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
@@ -2913,6 +2918,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())
@@ -2958,6 +2964,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 """
@@ -2970,7 +2982,7 @@ class NsrRpcDtsHandler(object):
                     "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))
                 
 
@@ -2979,10 +2991,11 @@ class NsrRpcDtsHandler(object):
             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)
@@ -2996,17 +3009,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,
@@ -3032,6 +3054,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
@@ -3041,6 +3064,7 @@ class NsrDtsHandler(object):
 
         self._nsr_regh = None
         self._scale_regh = None
+        self._key_pair_regh = None
 
     @property
     def nsm(self):
@@ -3174,12 +3198,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"):
@@ -3189,7 +3221,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):
@@ -3220,8 +3252,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))
 
 
@@ -3235,7 +3270,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:
@@ -3352,6 +3388,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 """
@@ -3771,7 +3812,7 @@ 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
@@ -3792,10 +3833,11 @@ class NsManager(object):
                                    nsm_plugin,
                                    nsr_msg,
                                    sdn_account_name,
+                                   key_pairs,
                                    restart_mode=restart_mode
                                    )
         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