def post_iterate(self, state): context = "system" added = state[0] removed = state[1] with maapi.single_read_trans("", context) as outer: for key in added: kp = '/nfvo-rel2:nfvo/nfvo-rel2:vnf-info/nfvo-rel2-esc:esc/nfvo-rel2-esc:vnf-deployment{"%s" "%s" "%s"}' % key self.log.info("VNFSubscriber, stored kp: %s" % (kp)) dep = maagic.get_node(outer, kp) username = dep.username # Switch usercontext to the stored username with maapi.single_write_trans(username, context) as th: dep = maagic.get_node(th, kp) template = ncs.template.Template(dep) # Write to /nfvo/tailf-esc-internal template.apply('tailf-etsi-rel2-nfvo-esc-vnf-info-copy') # Delete any possible residue in staged delete for # this vnf self._delete_residue_staged_delete(th, key, dep) try: th.apply() except Exception as e: self.log.error("Received %s when commiting %s" % (e, key)) self._handle_commit_error(username, context, key, e.message) for key in removed: svc_kp = '/nfvo-rel2:nfvo/nfvo-rel2-esc:tailf-esc-internal/nfvo-rel2-esc:vnf-deployment{"%s" "%s" "%s"}'\ % key if outer.exists(svc_kp): svc = maagic.get_node(outer, svc_kp) username = str(svc.username) staged_delete = svc.nfvo_behaviors.staged_delete else: # If not service is not found, we may still need to clean up the oper data username = "******" staged_delete = False self.log.info("Delete %s" % (svc_kp)) with maapi.single_write_trans(username, context) as th: if staged_delete: self.log.info("Using staged delete for %s/%s/%s" % key) delete_kp = "/nfvo-rel2:nfvo/nfvo-rel2-esc-sd:staged-delete-esc/nfvo-rel2-esc-sd:delete" else: self.log.info("Skipping staged delete for %s/%s/%s" % key) delete_kp = "/nfvo-rel2:nfvo/nfvo-rel2-esc-sd:staged-delete-esc/nfvo-rel2-esc-sd:deleted" delete_list = maagic.get_node(th, delete_kp) delete = delete_list.create(key) delete.username = username th.apply()
def cb_action(self, uinfo, name, kp, input, output): log.info('action input: ', input.uri) if input.raw: tosca = input.raw else: tosca = self._load_uri(input.uri) log.debug("TOSCA = {}".format(tosca)) tosca_vnf = yaml.load(tosca) vnfd = sp.tosca2cvnfd(tosca_vnf) log.debug("VNFD = {}".format(vnfd)) # Wrap vnfd in /nfvo/vnfd vnfd = {"tailf-etsi-rel2-nfvo:nfvo": {"vnfd": vnfd}} with maapi.single_write_trans(uinfo.username, "tailf-etsi-rel2-nfvo-tosca") as th: # vnfds = maagic.get_root(th).nfvo.vnfd log.info("JSON: {}".format(json.dumps(vnfd))) th.load_config_cmds(maapi.CONFIG_JSON | maapi.CONFIG_MERGE, json.dumps(vnfd), "/nfvo/vnfd") th.apply()
def _handle_commit_error(self, username, context, key, error_msg): with maapi.single_write_trans(username, context, db=ncs.OPERATIONAL) as th: results = maagic.get_root( th).nfvo_rel2__nfvo.vnf_info.esc.vnf_deployment_result res = results.create(key) res.status.error = error_msg th.apply()
def cb_action(self, uinfo, name, kp, input, output, trans): device_serial=kp[0][0].as_pyval() with maapi.single_write_trans(uinfo.username, "system", db=ncs.RUNNING) as th: self.log.info("Onboarding ztp device: %s" % (kp)) root = maagic.get_root(th) device=root.ztp[device_serial] ztp_template_vars = ncs.template.Variables() ztp_template_template = ncs.template.Template(device) ztp_template_vars.add('DEVICE_NAME', device_serial) ztp_template_vars.add('ADDRESS', device.ip) ztp_template_template.apply('ztp-template', ztp_template_vars) th.apply() with maapi.single_read_trans(uinfo.username, "system", db=ncs.OPERATIONAL) as th: root = maagic.get_root(th) inputs = root.devices.device[device_serial].ssh.fetch_host_keys.get_input() self.log.info(root.devices.device[device_serial].ssh.fetch_host_keys(inputs)) self.log.info(root.devices.device[device_serial].sync_from()) #send notif csocket = socket.socket() try: ctx = dp.init_daemon('send-notif') # making a control socket dp.connect(dx=ctx, sock=csocket, type=dp.CONTROL_SOCKET, ip='127.0.0.1', port=_ncs.PORT) # getting all the required hashes ns_hash = _ncs.str2hash("http://example.com/ztp") # notif_name_hash = _ncs.str2hash('service-name') notif_state_hash = _ncs.str2hash('state') notif_device_hash = _ncs.str2hash('device') notif_hash = _ncs.str2hash('ztp-status') # making the notification message = [] message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, notif_hash), _ncs.Value((notif_hash, ns_hash), _ncs.C_XMLBEGIN))] message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_state_hash), _ncs.Value("ready"))] message += [_ncs.TagValue(ncs.XmlTag(ns_hash, notif_device_hash), _ncs.Value(device_serial))] message += [_ncs.TagValue(_ncs.XmlTag(ns_hash, notif_hash), _ncs.Value((notif_hash, ns_hash), _ncs.C_XMLEND))] # registering the stream livectx = dp.register_notification_stream(ctx, None, csocket, "ztp") # time now = datetime.datetime.now() time = _ncs.DateTime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, now.timetz().hour, now.timetz().minute) # sending the notification dp.notification_send(livectx, time, message) self.log.info(message) self.log.info("ZTP: Onboarding notification has been generated.")
def run_with_trans(self, callback: Callable[[Transaction], T], write: bool = False, db: int = _ncs.RUNNING) -> T: if write: # we do not want to write to the user's transaction with maapi.single_write_trans(self.uinfo.username, self.uinfo.context, db=db) as trans: return callback(trans) elif self.uinfo.actx_thandle == -1: with maapi.single_read_trans(self.uinfo.username, self.uinfo.context, db=db) as trans: return callback(trans) else: mp = maapi.Maapi() return callback(mp.attach(self.uinfo.actx_thandle))
def post_iterate(self, state): self.log.info('Staged Delete: RUNNING , state=' + str(state)) context = "system" with maapi.single_write_trans("", context) as th: for k in state: deleted = maagic.get_node( th, '/nfvo-rel2:nfvo/nfvo-rel2-esc-sd:staged-delete-esc/nfvo-rel2-esc-sd:deleted' ) with maapi.single_write_trans(deleted[k].username, context) as uth: vnf_svcs = maagic.get_node( uth, '/nfvo-rel2:nfvo/nfvo-rel2-esc:tailf-esc-internal' '/nfvo-rel2-esc:vnf-deployment') # Delete the VNF Service del vnf_svcs[k] # Find all devices dep_res = maagic.get_node( uth, '/nfvo-rel2:nfvo/nfvo-rel2:vnf-info/nfvo-rel2-esc:esc/' 'nfvo-rel2-esc:vnf-deployment-result') if k in dep_res: for vdu in dep_res[k].vdu: for vmd in vdu.vm_device: device_name = vmd.device_name self.log.info('device_name: %s' % (device_name)) devices = maagic.get_node( uth, "/ncs:devices/ncs:device") del devices[device_name] # delete the deployment result del dep_res[k] uth.apply() del deleted[k] th.apply()
def run_with_trans(self, callback, write=False): if write: # we do not want to write to the user's transaction with maapi.single_write_trans(self.uinfo.username, self.uinfo.context) as trans: return callback(trans) elif self.uinfo.actx_thandle == -1: with maapi.single_read_trans(self.uinfo.username, self.uinfo.context) as trans: return callback(trans) else: mp = maapi.Maapi() return callback(mp.attach(self.uinfo.actx_thandle))
def cb_action(self, uinfo, name, kp, input, output): global alive if not alive: raise Exception("I'm dead!") now = str(datetime.datetime.now()) # Make sure we can open a write trans and write something to CDB with maapi.single_write_trans(uinfo.username, "alive") as th: k8s = maagic.get_node(th, "/lr-test:k8s") k8s.last_live_check = now th.apply() output.result = now
def cb_action(self, uinfo, name, kp, input, output): with maapi.single_write_trans(uinfo.username, uinfo.context, db=ncs.OPERATIONAL) as th: root = ncs.maagic.get_root(th) deployments = root.nfvo_rel2__nfvo.vnf_info.esc.vnf_deployment_result self.log.info( "There are {} existing deployments to migrate".format( len(deployments))) for dep in deployments: self._handle_dep_res(root, th) th.apply() output.result = "Migration Complete!"
def post_iterate(self, state): context = 'system' with maapi.single_read_trans('', context) as ro_th: ro_root = maagic.get_root(ro_th) for kp in state: notif = maagic.get_node(ro_th, kp) if not notif.data.escEvent: continue event = notif.data.escEvent username = _get_username(ro_root, event) vnfm_name = ncs.maagic.cd(notif, '../../../ncs:name') if (username and notif.subscription == f'cisco-etsi-nfvo-{vnfm_name}'): # Switch user context with maapi.single_write_trans(username, context) as th: self._handle_notif(notif, th) th.apply()
def cb_action(self, uinfo, name, kp, input, output, trans): device_serial = kp[0][0].as_pyval() with maapi.single_write_trans(uinfo.username, "system", db=ncs.RUNNING) as th: root = maagic.get_root(th) ztp = root.ztp[device_serial] filename = ztp.day0 ip = ztp.ip hostname = ztp.hostname f = open("/opt/ncs-run/packages/ztp/day0_templates/{}".format(filename), "r") day0 = f.read() f.close() # replacing values in the template day0 = day0.replace("${IP}", ip) day0 = day0.replace("${HOSTNAME}", hostname) day0 = day0.replace("${SERIAL}", device_serial) output.message = day0
def post_iterate(self, state): context = "system" with maapi.single_read_trans("", context) as ro_th: ro_root = maagic.get_root(ro_th) for notifInfo in state: notif = maagic.get_node(ro_th, notifInfo.kp) if not notif.data.escEvent: continue event = notif.data.escEvent username = self._get_username(ro_root, notifInfo.esc, event) if username and notif.subscription == "tailf_nfvo": # Switch user context with maapi.single_write_trans(username, context) as th: self._vnf_info_svcs_to_redeploy = set() self._handle_notif(notifInfo.esc, notif, th) th.apply() if self._vnf_info_svcs_to_redeploy: with maapi.single_read_trans(username, context) as th: self._redeploy_services(th)