def _delete_residue_staged_delete(self, th, key, dep): #super small optimization if dep.nfvo_behaviors.staged_delete: kp = '/nfvo-rel2:nfvo/nfvo-rel2-esc-sd:staged-delete-esc/' staged_delete_node = maagic.get_node(th, kp + "delete") staged_deleted_node = maagic.get_node(th, kp + "deleted") del staged_delete_node[key] del staged_deleted_node[key]
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): result = '' self.log.info('VXLAN Fabric Verify for {}'.format(kp)) allOk = True with maapi.single_read_trans(uinfo.username, 'vxlan-verify') as cfg: svc = maagic.get_node(cfg, kp) root = ncs.maagic.get_root(cfg) devs = root.devices.device numleafs = len(svc.leaves) numspines = len(svc.spines) self.log.info('Fabric: {}, spines: {}, leafs: {}'.format( svc.name, numspines, numleafs)) # Check all spines for s in svc.spines: result += checkdev(devs, s.device, 'Spine', numleafs) for l in svc.leaves: result += checkdev(devs, l.device, 'Leaf', numspines) if allOk: result += 'SUMMMARY: BGP AND OSPF OK\n' output.ready = True else: result += 'SUMMARY: NOT READY\n' output.ready = False output.result = result
def is_ha_slave(): with maapi.single_read_trans("", "system", db=ncs.OPERATIONAL) as trans: if trans.exists("/tfnm:ncs-state/tfnm:ha"): mode = str( maagic.get_node(trans, '/tfnm:ncs-state/tfnm:ha/tfnm:mode')) return (mode == 'slave' or mode == 'relay-slave') return False
def is_ha_master_or_no_ha(): with maapi.single_read_trans("", "system", db=ncs.OPERATIONAL) as trans: if trans.exists("/tfnm:ncs-state/tfnm:ha"): mode = str( maagic.get_node(trans, '/tfnm:ncs-state/tfnm:ha/tfnm:mode')) return (mode == 'master') else: return True
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 cb_action(self, uinfo, _name, kp, input, output): # actions have access to the CDB. with maapi.single_read_trans(uinfo.username, 'pathtrace') as cfg: svc = maagic.get_node(cfg, kp) root = ncs.maagic.get_root(cfg) devs = root.devices.device msg = "running pathtrace from {} to {}" self.log.info(msg.format(svc.device, svc.destination)) p = ping(devs, svc.device, svc.destination) t = traceroute(devs, svc.device, svc.destination) output.result = p + t output.ready = True
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_post_modification(self, tctx, op, kp, root, proplist): self.log.info('Service postmod(service=', kp, ')') self.log.info('Service postmod(op=', str(op), ')') """ OP = 1: 'CREATED', 2: 'DELETED', 3: 'MODIFIED', 4: 'VALUE_SET', 5: 'MOVED_AFTER', 6: 'ATTR_SET' """ if op != 2: service = maagic.get_node(maagic.get_trans(root), kp) restore_point = root.top_l3vpn__l3vpn_restore_points.create( service.name + '-' + datetime.now().strftime("%Y-%m-%d_%H:%M:%S")) t = maagic.get_trans(root) t.copy_tree(service._path, restore_point._path)
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 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)
def _redeploy_services(self, th): for svc in self._vnf_info_svcs_to_redeploy: self.log.info("Redeploying '%s'" % (svc)) redep = maagic.get_node( th, "%s/nfvo-rel2-esc:reactive-re-deploy" % (svc)) redep()