import gi
gi.require_version('RwYang', '1.0')
-gi.require_version('RwNsdYang', '1.0')
+gi.require_version('NsdYang', '1.0')
gi.require_version('RwDts', '1.0')
gi.require_version('RwNsmYang', '1.0')
gi.require_version('RwNsrYang', '1.0')
VnfrYang,
RwVnfrYang,
RwNsmYang,
- RwsdnYang,
+ RwsdnalYang,
RwDts as rwdts,
RwTypes,
ProtobufC,
vnfr = yield from self._nsr.fetch_vnfr(nsr_vnfr.xpath)
self._log.debug("Received VNFR is %s", vnfr)
- sff = RwsdnYang.VNFFGSff()
+ sff = RwsdnalYang.VNFFGSff()
sff_list[nsr_vnfr.vnfd.id] = sff
sff.name = nsr_vnfr.name
sff.function_type = nsr_vnfr.vnfd.service_function_chain
(member_vnfd.member_vnf_index_ref == const_vnfd.member_vnf_index):
group_info = self.resolve_placement_group_cloud_construct(group)
if group_info is None:
- self._log.error("Could not resolve cloud-construct for placement group: %s", group.name)
+ self._log.info("Could not resolve cloud-construct for placement group: %s", group.name)
### raise PlacementGroupError("Could not resolve cloud-construct for placement group: {}".format(group.name))
else:
self._log.info("Successfully resolved cloud construct for placement group: %s for VNF: %s (Member Index: %s)",
# Delete an NSD record
self._log.debug("Deleting NSD with id %s", msg.id)
if self._nsm.nsd_in_use(msg.id):
- self._log.debug("Cannot delete NSD in use - %s", msg.id)
- err = "Cannot delete an NSD in use - %s" % msg.id
- raise NetworkServiceDescriptorRefCountExists(err)
+ errmsg = "Cannot delete an NSD in use - %s" % msg.id
+ self._log.error(errmsg)
+ xpath = ks_path.to_xpath(NsdYang.get_schema())
+ xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
+ xpath,
+ errmsg)
+ xact_info.respond_xpath(rwdts.XactRspCode.NACK)
+ return
yield from delete_nsd_libs(msg.id)
self._nsm.delete_nsd(msg.id)
"nsr_id":str(uuid.uuid4())
})
- 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))
-
+ if not ('name' in rpc_ip and 'nsd_ref' in rpc_ip and
+ ('cloud_account' in rpc_ip or 'om_datacenter' in rpc_ip)):
+ errmsg = (
+ "Mandatory parameters name or nsd_ref or cloud account not found in start-network-service {}".
+ format(rpc_ip))
+ self._log.error(errmsg)
+ xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
+ NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH,
+ errmsg)
+ xact_info.respond_xpath(rwdts.XactRspCode.NACK,
+ NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH)
+ return
self._log.debug("start-network-service RPC input: {}".format(rpc_ip))
nsd_copy = self.nsm.get_nsd(rpc_ip.nsd_ref)
- #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)
ns_instance_config.nsd.from_dict(nsd_copy.msg.as_dict())
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,
NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH,
rpc_op)
except Exception as e:
- self._log.error("Exception processing the "
- "start-network-service: {}".format(e))
- self._log.exception(e)
+ errmsg = ("Exception processing the "
+ "start-network-service: {}".format(e))
+ self._log.exception(errmsg)
+ xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
+ NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH,
+ errmsg)
xact_info.respond_xpath(rwdts.XactRspCode.NACK,
NsrRpcDtsHandler.EXEC_NSR_CONF_O_XPATH)
-
hdl_ns = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_ns_config_prepare,)
with self._dts.group_create() as group:
fref = ProtobufC.FieldReference.alloc()
fref.goto_whole_message(msg.to_pbcm())
+ def send_err_msg(err_msg):
+ self._log.error(errmsg)
+ xact_info.send_error_xpath(RwTypes.RwStatus.FAILURE,
+ xpath,
+ errmsg)
+ xact_info.respond_xpath(rwdts.XactRspCode.NACK)
+
+
if action in [rwdts.QueryAction.CREATE, rwdts.QueryAction.UPDATE, rwdts.QueryAction.DELETE]:
# if this is an NSR create
if action != rwdts.QueryAction.DELETE and msg.id not in self._nsm.nsrs:
# Ensure the Cloud account/datacenter has been specified
if not msg.has_field("cloud_account") and not msg.has_field("om_datacenter"):
- raise NsrInstantiationFailed("Cloud account or datacenter not specified in NSR")
+ errmsg = ("Cloud account or datacenter not specified in NS {}".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
# Check if nsd is specified
if not msg.has_field("nsd"):
- raise NsrInstantiationFailed("NSD not specified in NSR")
+ errmsg = ("NSD not specified in NS {}".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
else:
nsr = self._nsm.nsrs[msg.id]
if msg.has_field("nsd"):
if nsr.state != NetworkServiceRecordState.RUNNING:
- raise NsrVlUpdateError("Unable to update VL when NSR not in running state")
+ errmsg = ("Unable to update VL when NS {} not in running state".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
+
if 'vld' not in msg.nsd or len(msg.nsd.vld) == 0:
- raise NsrVlUpdateError("NS config NSD should have atleast 1 VLD defined")
+ errmsg = ("NS config {} NSD should have atleast 1 VLD".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
if msg.has_field("scaling_group"):
if nsr.state != NetworkServiceRecordState.RUNNING:
- raise ScalingOperationError("Unable to perform scaling action when NS is not in running state")
+ errmsg = ("Unable to perform scaling action when NS {} not in running state".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
if len(msg.scaling_group) > 1:
- raise ScalingOperationError("Only a single scaling group can be configured at a time")
+ errmsg = ("Only a single scaling group can be configured at a time for NS {}".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
for group_msg in msg.scaling_group:
num_new_group_instances = len(group_msg.instance)
if num_new_group_instances > 1:
- raise ScalingOperationError("Only a single scaling instance can be modified at a time")
+ errmsg = ("Only a single scaling instance can be modified at a time for NS {}".
+ format(msg.name))
+ send_err_msg(errmsg)
+ return
elif num_new_group_instances == 1:
scale_group = nsr.scaling_groups[group_msg.scaling_group_name_ref]
if action in [rwdts.QueryAction.CREATE, rwdts.QueryAction.UPDATE]:
if len(scale_group.instances) == scale_group.max_instance_count:
- raise ScalingOperationError("Max instances for %s reached" % scale_group)
+ errmsg = (" Max instances for {} reached for NS {}".
+ format(str(scale_group), msg.name))
+ send_err_msg(errmsg)
+ return
acg.handle.prepare_complete_ok(xact_info.handle)
self._cloud_account_handler = cloud_account_handler
self._ro_plugin_selector = ro_plugin_selector
- self._ncclient = rift.mano.ncclient.NcClient(
- host="127.0.0.1",
- port=2022,
- username="admin",
- password="admin",
- loop=self._loop)
+ # Intialize the set of variables for implementing Scaling RPC using REST.
+ self._headers = {"content-type":"application/json", "accept":"application/json"}
+ #This will break when we have rbac in the rift code and admin user password is changed or admin it self is removed.
+ self._user = 'admin'
+ self._password = 'admin'
+ self._ip = 'localhost'
+ self._rport = 8008
+ self._conf_url = "https://{ip}:{port}/api/config". \
+ format(ip=self._ip,
+ port=self._rport)
+
self._nsrs = {}
self._nsds = {}
self._vnfds = {}
VnfrDtsHandler(dts, log, loop, self),
NsdRefCountDtsHandler(dts, log, loop, self),
NsrDtsHandler(dts, log, loop, self),
+ ScalingRpcHandler(log, dts, loop, self.scale_rpc_callback),
NsrRpcDtsHandler(dts,log,loop,self),
self._vnfd_dts_handler,
self.cfgmgr_obj,
self._loop.create_task(nsr.delete_scale_group_instance(scale_group_name, instance_id))
+ def scale_rpc_callback(self, xact, msg, action):
+ """Callback handler for RPC calls
+ Args:
+ xact : Transaction Handler
+ msg : RPC input
+ action : Scaling Action
+ """
+ def get_scaling_group_information():
+ scaling_group_url = "{url}/ns-instance-config/nsr/{nsr_id}".format(url=self._conf_url, nsr_id=msg.nsr_id_ref)
+ output = requests.get(scaling_group_url, headers=self._headers, auth=(self._user, self._password), verify=False)
+ if output.text == None or len(output.text) == 0:
+ self.log.error("nsr id %s information not present", self._nsr_id)
+ return None
+ scaling_group_info = json.loads(output.text)
+ return scaling_group_info
+
+ def config_scaling_group_information(scaling_group_info):
+ data_str = json.dumps(scaling_group_info)
+ self.log.debug("scaling group Info %s", data_str)
+
+ scale_out_url = "{url}/ns-instance-config/nsr/{nsr_id}".format(url=self._conf_url, nsr_id=msg.nsr_id_ref)
+ response = requests.put(scale_out_url, data=data_str, verify=False, auth=(self._user, self._password), headers=self._headers)
+ response.raise_for_status()
+
+ def scale_out():
+ scaling_group_info = get_scaling_group_information()
+ if scaling_group_info is None:
+ return
+
+ scaling_group_present = False
+ if "scaling-group" in scaling_group_info["nsr:nsr"]:
+ scaling_group_array = scaling_group_info["nsr:nsr"]["scaling-group"]
+ for scaling_group in scaling_group_array:
+ if scaling_group["scaling-group-name-ref"] == msg.scaling_group_name_ref:
+ scaling_group_present = True
+ if 'instance' not in scaling_group:
+ scaling_group['instance'] = []
+ for instance in scaling_group['instance']:
+ if instance["id"] == int(msg.instance_id):
+ self.log.error("scaling group with instance id %s exists for scale out", msg.instance_id)
+ return
+ scaling_group["instance"].append({"id": int(msg.instance_id)})
+
+ if not scaling_group_present:
+ scaling_group_info["nsr:nsr"]["scaling-group"] = [{"scaling-group-name-ref": msg.scaling_group_name_ref, "instance": [{"id": msg.instance_id}]}]
+
+ config_scaling_group_information(scaling_group_info)
+ return
+
+ def scale_in():
+ scaling_group_info = get_scaling_group_information()
+ if scaling_group_info is None:
+ return
+
+ scaling_group_array = scaling_group_info["nsr:nsr"]["scaling-group"]
+ scaling_group_present = False
+ instance_id_present = False
+ for scaling_group in scaling_group_array:
+ if scaling_group["scaling-group-name-ref"] == msg.scaling_group_name_ref:
+ scaling_group_present = True
+ if 'instance' in scaling_group:
+ instance_array = scaling_group["instance"];
+ for index in range(len(instance_array)):
+ if instance_array[index]["id"] == int(msg.instance_id):
+ instance_array.pop(index)
+ instance_id_present = True
+ break
+
+ if not scaling_group_present:
+ self.log.error("Scaling group %s doesnot exists for scale in", msg.scaling_group_name_ref)
+ return
+
+ if not instance_id_present:
+ self.log.error("Instance id %s doesnot exists for scale in", msg.instance_id)
+ return
+
+ config_scaling_group_information(scaling_group_info)
+ return
+
+ if action == ScalingRpcHandler.ACTION.SCALE_OUT:
+ self._loop.run_in_executor(None, scale_out)
+ else:
+ self._loop.run_in_executor(None, scale_in)
+
def nsr_update_cfg(self, nsr_id, msg):
nsr = self._nsrs[nsr_id]
nsr.nsr_cfg_msg= msg
path = VirtualLinkRecord.vlr_xpath(vlr)
return (yield from self._vlr_pub_hdlr.delete(xact, path))
+
+class ScalingRpcHandler(mano_dts.DtsHandler):
+ """ The Network service Monitor DTS handler """
+ SCALE_IN_INPUT_XPATH = "I,/nsr:exec-scale-in"
+ SCALE_IN_OUTPUT_XPATH = "O,/nsr:exec-scale-in"
+
+ SCALE_OUT_INPUT_XPATH = "I,/nsr:exec-scale-out"
+ SCALE_OUT_OUTPUT_XPATH = "O,/nsr:exec-scale-out"
+
+ ACTION = Enum('ACTION', 'SCALE_IN SCALE_OUT')
+
+ def __init__(self, log, dts, loop, callback=None):
+ super().__init__(log, dts, loop)
+ self.callback = callback
+ self.last_instance_id = defaultdict(int)
+
+ @asyncio.coroutine
+ def register(self):
+
+ @asyncio.coroutine
+ def on_scale_in_prepare(xact_info, action, ks_path, msg):
+ assert action == rwdts.QueryAction.RPC
+
+ try:
+ rpc_op = NsrYang.YangOutput_Nsr_ExecScaleIn.from_dict({
+ "instance_id": msg.instance_id})
+
+ xact_info.respond_xpath(
+ rwdts.XactRspCode.ACK,
+ self.__class__.SCALE_IN_OUTPUT_XPATH,
+ rpc_op)
+
+ if self.callback:
+ self.callback(xact_info.xact, msg, self.ACTION.SCALE_IN)
+ except Exception as e:
+ self.log.exception(e)
+ xact_info.respond_xpath(
+ rwdts.XactRspCode.NACK,
+ self.__class__.SCALE_IN_OUTPUT_XPATH)
+
+ @asyncio.coroutine
+ def on_scale_out_prepare(xact_info, action, ks_path, msg):
+ assert action == rwdts.QueryAction.RPC
+
+ try:
+ scaling_group = msg.scaling_group_name_ref
+ if not msg.instance_id:
+ last_instance_id = self.last_instance_id[scale_group]
+ msg.instance_id = last_instance_id + 1
+ self.last_instance_id[scale_group] += 1
+
+ rpc_op = NsrYang.YangOutput_Nsr_ExecScaleOut.from_dict({
+ "instance_id": msg.instance_id})
+
+ xact_info.respond_xpath(
+ rwdts.XactRspCode.ACK,
+ self.__class__.SCALE_OUT_OUTPUT_XPATH,
+ rpc_op)
+
+ if self.callback:
+ self.callback(xact_info.xact, msg, self.ACTION.SCALE_OUT)
+ except Exception as e:
+ self.log.exception(e)
+ xact_info.respond_xpath(
+ rwdts.XactRspCode.NACK,
+ self.__class__.SCALE_OUT_OUTPUT_XPATH)
+
+ scale_in_hdl = rift.tasklets.DTS.RegistrationHandler(
+ on_prepare=on_scale_in_prepare)
+ scale_out_hdl = rift.tasklets.DTS.RegistrationHandler(
+ on_prepare=on_scale_out_prepare)
+
+ with self.dts.group_create() as group:
+ group.register(
+ xpath=self.__class__.SCALE_IN_INPUT_XPATH,
+ handler=scale_in_hdl,
+ flags=rwdts.Flag.PUBLISHER)
+ group.register(
+ xpath=self.__class__.SCALE_OUT_INPUT_XPATH,
+ handler=scale_out_hdl,
+ flags=rwdts.Flag.PUBLISHER)
+
+
class NsmTasklet(rift.tasklets.Tasklet):
"""
The network service manager tasklet