def remove_pnfdev(self, pnfdev): """Remove a PNFDev from the Tenant. Args: addr, a PNFDev object Returns: None Raises: KeyError, if the pnfdev is not available """ pnfdevs = getattr(self, pnfdev.ALIAS) if pnfdev.addr not in pnfdevs: return del pnfdevs[pnfdev.addr] belongs = Session().query(TblBelongs) \ .filter(TblBelongs.tenant_id == self.tenant_id, TblBelongs.addr == pnfdev.addr) \ .first() session = Session() session.delete(belongs) session.commit()
def add_pnfdev(self, pnfdev): """Add a new PNF Dev to the Tenant. Args: pnfdev, a PNFDev object Returns: None Raises: KeyError, if the pnfdev is not available """ pnfdevs = getattr(self, pnfdev.ALIAS) if pnfdev.addr in pnfdevs: return pnfdevs[pnfdev.addr] = pnfdev belongs = TblBelongs(tenant_id=self.tenant_id, addr=pnfdev.addr) session = Session() session.add(belongs) session.commit()
def request_tenant(self, owner, desc, tenant_name, bssid_type, tenant_id=None): """Request new Tenant.""" if tenant_id in self.tenants: raise ValueError("Tenant %s exists", tenant_id) if self.load_pending_tenant(tenant_id): raise ValueError("Tenant %s exists", tenant_id) try: session = Session() if tenant_id: request = TblPendingTenant(tenant_id=tenant_id, owner=owner, tenant_name=tenant_name, desc=desc, bssid_type=bssid_type) else: request = TblPendingTenant(owner=owner, tenant_name=tenant_name, desc=desc, bssid_type=bssid_type) session.add(request) session.commit() except IntegrityError: session.rollback() raise ValueError("Tenant name %s exists", tenant_name) return request.tenant_id
def add_tenant(self, owner, desc, tenant_name, tenant_id=None): """Create new Tenant.""" if tenant_id in self.tenants: raise ValueError("Tenant %s exists", tenant_id) try: session = Session() if tenant_id: request = TblTenant(tenant_id=tenant_id, tenant_name=tenant_name, owner=owner, desc=desc) else: request = TblTenant(owner=owner, tenant_name=tenant_name, desc=desc) session.add(request) session.commit() except IntegrityError: session.rollback() raise ValueError("Tenant name %s exists", tenant_name) self.tenants[request.tenant_id] = \ Tenant(request.tenant_id, request.tenant_name, self.accounts[owner].username, desc) return request.tenant_id
def generate_default_accounts(): """Generate default accounts. Three default accounts (one root account and two user accounts are created the first time the controller is started. """ if not Session().query(TblAccount).all(): LOG.info("Generating default accounts") session = Session() session.add(TblAccount(username="******", password="******", role="admin", name="Administrator", surname="", email="*****@*****.**")) session.add(TblAccount(username="******", password="******", role="user", name="Foo", surname="", email="*****@*****.**")) session.add(TblAccount(username="******", password="******", role="user", name="Bar", surname="", email="*****@*****.**")) session.commit()
def name(self, name): """Set name.""" session = Session() account = session.query(TblAccount) \ .filter(TblAccount.username == self.username) \ .first() account.name = name session.commit() self._name = name
def surname(self, surname): """Set surname.""" session = Session() account = Session().query(TblAccount) \ .filter(TblAccount.username == self.username) \ .first() account.surname = surname session.commit() self._surname = surname
def email(self, email): """Set email.""" session = Session() account = Session().query(TblAccount) \ .filter(TblAccount.username == self.username) \ .first() account.email = email session.commit() self._email = email
def add_feed(self): """Create new Feed.""" feed_id = self.feed_id RUNTIME.feeds[feed_id] = Feed(feed_id) session = Session() session.add(TblFeed(feed_id=feed_id, created=RUNTIME.feeds[feed_id].created, updated=RUNTIME.feeds[feed_id].updated)) session.commit() return RUNTIME.feeds[feed_id]
def add_pnfdev(self, addr, label): """Add PNFDev.""" if addr in self.pnfdevs: raise KeyError(addr) self.pnfdevs[addr] = self.PNFDEV(addr, label) session = Session() session.add(self.TBL_PNFDEV(addr=addr, label=label)) session.commit() return self.pnfdevs[addr]
def reject_tenant(cls, tenant_id): """Reject previously requested Tenant.""" pending = Session().query(TblPendingTenant) \ .filter(TblPendingTenant.tenant_id == tenant_id) \ .first() if not pending: raise KeyError(tenant_id) session = Session() session.delete(pending) session.commit()
def remove_denied(self, sta_addr): """ Remove entry from ACL. """ deny = Session().query(TblDeny) \ .filter(TblDeny.addr == sta_addr) \ .first() if not deny: raise KeyError(sta_addr) session = Session() session.delete(deny) session.commit() del self.denied[sta_addr]
def remove_allowed(self, sta_addr): """ Remove entry from ACL. """ allow = Session().query(TblAllow) \ .filter(TblAllow.addr == sta_addr) \ .first() if not allow: raise KeyError(sta_addr) session = Session() session.delete(allow) session.commit() del self.allowed[sta_addr]
def remove_imsi2mac(self, imsi): """Remove IMSI to MAC mapped value from table.""" imsi2mac = Session().query(TblIMSI2MAC) \ .filter(TblIMSI2MAC.imsi == imsi) \ .first() if not imsi2mac: raise KeyError(imsi) session = Session() session.delete(imsi2mac) session.commit() del self.imsi2mac[imsi]
def pnfdev(self, pnfdev): """Set the PNFDev and update database.""" self.__pnfdev = pnfdev session = Session() feed = Session().query(TblFeed) \ .filter(TblFeed.feed_id == self.feed_id) \ .first() if self.pnfdev: feed.pnfdev_addr = self.pnfdev.addr else: feed.pnfdev_addr = None session.commit()
def remove_feed(self, feed_id): """Remove Feed.""" if feed_id not in RUNTIME.feeds: raise KeyError(feed_id) self.bind_feed(feed_id) del RUNTIME.feeds[feed_id] feed = Session().query(TblFeed) \ .filter(TblFeed.feed_id == feed_id) \ .first() session = Session() session.delete(feed) session.commit()
def add_allowed(self, sta_addr, label): """ Add entry to ACL. """ allow = Session().query(TblAllow) \ .filter(TblAllow.addr == sta_addr) \ .first() if allow: raise ValueError(sta_addr) session = Session() session.add(TblAllow(addr=sta_addr, label=label)) session.commit() acl = ACL(sta_addr, label) self.allowed[sta_addr] = acl return acl
def add_denied(self, sta_addr, label): """ Add entry to ACL. """ deny = Session().query(TblDeny) \ .filter(TblDeny.addr == sta_addr) \ .first() if deny: raise ValueError(sta_addr) session = Session() session.add(TblDeny(addr=sta_addr, label=label)) session.commit() acl = ACL(sta_addr, label) self.denied[sta_addr] = acl return acl
def remove_tenant(self, tenant_id): """Delete existing Tenant.""" if tenant_id not in self.tenants: raise KeyError(tenant_id) tenant = self.tenants[tenant_id] # remove pnfdev in this tenant devs = Session().query(TblBelongs) \ .filter(TblBelongs.tenant_id == tenant_id) for dev in devs: session = Session() session.delete(dev) session.commit() # remove tenant del self.tenants[tenant_id] tenant = Session().query(TblTenant) \ .filter(TblTenant.tenant_id == tenant_id) \ .first() session = Session() session.delete(tenant) session.commit() # remove running modules for component in self.components.values(): if not hasattr(component, 'modules'): continue to_be_removed = [] for module in component.modules.values(): if module.tenant_id == tenant_id: to_be_removed.append(module.module_id) for module_id in to_be_removed: component.remove_module(module_id)
def add_imsi2mac(self, imsi, addr): """Add IMSI to MAC mapped value to table.""" imsi2mac = Session().query(TblIMSI2MAC) \ .filter(TblIMSI2MAC.imsi == imsi) \ .first() if imsi2mac: raise ValueError(imsi) try: session = Session() session.add(TblIMSI2MAC(imsi=imsi, addr=addr)) session.commit() except IntegrityError: session.rollback() raise ValueError("MAC address must be unique %s", addr) self.imsi2mac[imsi] = addr
def remove_account(self, username): """Remove an account.""" if username == 'root': raise ValueError("Cannot removed root account") account = Session().query(TblAccount) \ .filter(TblAccount.username == str(username)) \ .first() if not account: raise KeyError(username) session = Session() session.delete(account) session.commit() del self.accounts[username] to_be_deleted = [x.tenant_id for x in self.tenants.values() if x.owner == username] for tenant_id in to_be_deleted: self.remove_tenant(tenant_id)
def __load_feeds(self): """Load Feeds.""" for feed in Session().query(TblFeed).all(): RUNTIME.feeds[feed.feed_id] = Feed(feed.feed_id) RUNTIME.feeds[feed.feed_id].created = feed.created RUNTIME.feeds[feed.feed_id].updated = feed.updated if feed.pnfdev_addr: pnfdev = Session().query(TblPNFDev) \ .filter(TblPNFDev.addr == EtherAddress(feed.pnfdev_addr)) \ .first() if pnfdev: pnfdevs = getattr(RUNTIME, pnfdev.tbl_type) RUNTIME.feeds[feed.feed_id].pnfdev = \ pnfdevs[feed.pnfdev_addr] pnfdev = pnfdevs[feed.pnfdev_addr] pnfdev.feed = RUNTIME.feeds[feed.feed_id] else: session = Session() delete = Session().query(TblFeed) \ .filter(TblFeed.feed_id == feed.feed_id) \ .first() delete.pnfdev = None session.commit() query = Session().query( TblFeed, func.max(TblFeed.feed_id).label("max_id")) if query.one().max_id: self.feed_id = int(query.one().max_id)
def remove_account(self, username): """Remove an account.""" if username == 'root': raise ValueError("Cannot removed root account") account = Session().query(TblAccount) \ .filter(TblAccount.username == str(username)) \ .first() if not account: raise KeyError(username) session = Session() session.delete(account) session.commit() del self.accounts[username] to_be_deleted = [ x.tenant_id for x in self.tenants.values() if x.owner == username ] for tenant_id in to_be_deleted: self.remove_tenant(tenant_id)
def create_account(self, username, password, role, name, surname, email): """Create a new account.""" if username in self.accounts: LOG.error("'%s' already registered", username) raise ValueError("%s already registered" % username) session = Session() account = TblAccount(username=username, password=password, role=role, name=name, surname=surname, email=email) session.add(account) session.commit() self.accounts[account.username] = Account(account.username, account.password, account.name, account.surname, account.email, account.role)
def remove_tenant(self, tenant_id): """Delete existing Tenant.""" if tenant_id not in self.tenants: raise KeyError(tenant_id) tenant = self.tenants[tenant_id] # remove slices in this tenant for dscp in list(tenant.slices): tenant.del_slice(dscp) # remove tenant del self.tenants[tenant_id] tenant = Session().query(TblTenant) \ .filter(TblTenant.tenant_id == tenant_id) \ .first() session = Session() session.delete(tenant) session.commit() # remove running modules for component in self.components.values(): if not hasattr(component, 'modules'): continue to_be_removed = [] for module in component.modules.values(): if module.tenant_id == tenant_id: to_be_removed.append(module.module_id) for module_id in to_be_removed: component.remove_module(module_id)
def request_tenant(self, owner, desc, tenant_name, bssid_type, tenant_id=None, plmn_id=None): """Request new Tenant.""" if tenant_id in self.tenants: raise ValueError("Tenant %s exists", tenant_id) if self.load_pending_tenant(tenant_id): raise ValueError("Tenant %s exists", tenant_id) try: session = Session() if tenant_id: request = TblPendingTenant(tenant_id=tenant_id, owner=owner, tenant_name=tenant_name, desc=desc, bssid_type=bssid_type, plmn_id=plmn_id) else: request = TblPendingTenant(owner=owner, tenant_name=tenant_name, desc=desc, bssid_type=bssid_type, plmn_id=plmn_id) session.add(request) session.commit() except IntegrityError: session.rollback() raise ValueError("Tenant name %s exists", tenant_name) return request.tenant_id
def remove_pnfdev(self, addr): """Remove PNFDev.""" if addr not in self.pnfdevs: raise KeyError(addr) pnfdev = self.pnfdevs[addr] for tenant in RUNTIME.tenants.values(): tenant_pnfdevs = getattr(tenant, self.PNFDEV.ALIAS) if addr in tenant_pnfdevs: tenant.remove_pnfdev(pnfdev) del self.pnfdevs[addr] pnfdev = Session().query(self.TBL_PNFDEV) \ .filter(self.TBL_PNFDEV.addr == addr) \ .first() session = Session() session.delete(pnfdev) session.commit()
def remove_pnfdev(self, addr): """Remove PNFDev.""" if addr not in self.pnfdevs: raise KeyError(addr) pnfdev = self.pnfdevs[addr] for tenant in RUNTIME.tenants.values(): tenant_pnfdevs = getattr(tenant, self.PNFDEV.ALIAS) if addr in tenant_pnfdevs: tenant.remove_pnfdev(pnfdev) del self.pnfdevs[addr] pnfdev = Session().query(self.TBL_PNFDEV) \ .filter(self.TBL_PNFDEV.addr == addr) \ .first() session = Session() session.delete(pnfdev) session.commit()
def add_traffic_rule(self, match, dscp, label, priority=0): """Add a new traffic rule to the Tenant. Args: match, a Match object dscp, a slice DSCP code label, a humand readable description of the rule Returns: None Raises: Nones """ trule = TrafficRule(tenant=self, match=match, dscp=dscp, priority=priority, label=label) # Send command to IBN from empower.ibnp.ibnpserver import IBNPServer ibnp_server = get_module(IBNPServer.__module__) if ibnp_server: ibnp_server.add_traffic_rule(trule) rule = TblTrafficRule(tenant_id=self.tenant_id, match=match, dscp=dscp, priority=priority, label=label) try: session = Session() session.add(rule) session.commit() except IntegrityError: session.rollback() raise ValueError("Duplicate (%s, %s)" % (self.tenant_id, match))
def create_account(self, username, password, role, name, surname, email): """Create a new account.""" if username in self.accounts: LOG.error("'%s' already registered", username) raise ValueError("%s already registered" % username) session = Session() account = TblAccount(username=username, password=password, role=role, name=name, surname=surname, email=email) session.add(account) session.commit() self.accounts[account.username] = Account(account.username, account.password, account.name, account.surname, account.email, account.role)
def del_slice(self, dscp): """Del slice from. Args: dscp, a DSCP object Returns: None Raises: ValueError, if the dscp is not valid """ # fetch slice slc = self.slices[dscp] tenant_id = self.tenant_id # delete it from the db try: session = Session() rem = Session().query(TblSlice) \ .filter(TblSlice.tenant_id == self.tenant_id) \ .filter(TblSlice.dscp == dscp) \ .first() session.delete(rem) for wtp_addr in slc.wifi['wtps']: rem = \ Session().query(TblSliceBelongs) \ .filter(TblSliceBelongs.tenant_id == tenant_id) \ .filter(TblSliceBelongs.dscp == slc.dscp) \ .filter(TblSliceBelongs.addr == wtp_addr) \ .first() if rem: session.delete(rem) for vbs_addr in slc.lte['vbses']: rem = \ Session().query(TblSliceBelongs) \ .filter(TblSliceBelongs.tenant_id == tenant_id) \ .filter(TblSliceBelongs.dscp == slc.dscp) \ .filter(TblSliceBelongs.addr == vbs_addr) \ .first() if rem: session.delete(rem) session.commit() except IntegrityError: session.rollback() raise ValueError() # delete it from the WTPs for wtp_addr in self.wtps: wtp = self.wtps[wtp_addr] if not wtp.is_online(): continue for block in wtp.supports: wtp.connection.send_del_slice(block, self.tenant_name, dscp) # delete it from the VBSes for vbs_addr in self.vbses: vbs = self.vbses[vbs_addr] if not vbs.is_online(): continue for cell in vbs.cells.values(): vbs.connection.send_del_ran_mac_slice_request(cell, self.plmn_id, dscp) # remove slice del self.slices[dscp]
def add_slice(self, dscp, request): """Add a new slice to the Tenant. Args: dscp, a DSCP object request, the slice descriptor in json format Returns: None Raises: ValueError, if the dscp is not valid """ # create new instance slc = Slice(dscp, self, request) # descriptors has been parsed, now it is safe to write to the db try: session = Session() tbl_slc = TblSlice(tenant_id=self.tenant_id, dscp=slc.dscp, wifi=json.dumps(slc.wifi['static-properties']), lte=json.dumps(slc.lte['static-properties'])) session.add(tbl_slc) for wtp_addr in slc.wifi['wtps']: properties = \ json.dumps(slc.wifi['wtps'][wtp_addr]['static-properties']) belongs = TblSliceBelongs(tenant_id=self.tenant_id, dscp=tbl_slc.dscp, addr=wtp_addr, properties=properties) session.add(belongs) for vbs_addr in slc.lte['vbses']: properties = \ json.dumps(slc.lte['vbses'][vbs_addr]['static-properties']) belongs = TblSliceBelongs(tenant_id=self.tenant_id, dscp=tbl_slc.dscp, addr=vbs_addr, properties=properties) session.add(belongs) session.commit() except IntegrityError: session.rollback() raise ValueError() # store slice self.slices[dscp] = slc # create slice on WTPs for wtp_addr in self.wtps: wtp = self.wtps[wtp_addr] if not wtp.is_online(): continue for block in wtp.supports: wtp.connection.send_set_slice(block, slc) # create slice on VBSes for vbs_addr in self.vbses: vbs = self.vbses[vbs_addr] if not vbs.is_online(): continue for cell in vbs.cells.values(): vbs.connection.\ send_add_set_ran_mac_slice_request(cell, slc, EP_OPERATION_ADD)
def set_slice(self, dscp, request): """Update a slice in the Tenant. Args: dscp, a DSCP object request, the slice descriptor in json format Returns: None Raises: ValueError, if the dscp is not valid """ # create new instance slc = Slice(dscp, self, request) tenant_id = self.tenant_id # update db try: session = Session() tbl_slice = Session().query(TblSlice) \ .filter(TblSlice.tenant_id == tenant_id) \ .filter(TblSlice.dscp == slc.dscp) \ .first() tbl_slice.wifi = json.dumps(slc.wifi['static-properties']) tbl_slice.lte = json.dumps(slc.lte['static-properties']) for wtp_addr in slc.wifi['wtps']: properties = \ json.dumps(slc.wifi['wtps'][wtp_addr]['static-properties']) tbl_belongs = \ Session().query(TblSliceBelongs) \ .filter(TblSliceBelongs.tenant_id == tenant_id) \ .filter(TblSliceBelongs.dscp == slc.dscp) \ .filter(TblSliceBelongs.addr == wtp_addr) \ .first() if not tbl_belongs: belongs = TblSliceBelongs(tenant_id=self.tenant_id, dscp=slc.dscp, addr=wtp_addr, properties=properties) session.add(belongs) else: tbl_belongs.properties = properties for vbs_addr in slc.lte['vbses']: properties = \ json.dumps(slc.lte['vbses'][vbs_addr]['static-properties']) tbl_belongs = \ Session().query(TblSliceBelongs) \ .filter(TblSliceBelongs.tenant_id == tenant_id) \ .filter(TblSliceBelongs.dscp == slc.dscp) \ .filter(TblSliceBelongs.addr == vbs_addr) \ .first() if not tbl_belongs: belongs = TblSliceBelongs(tenant_id=self.tenant_id, dscp=slc.dscp, addr=vbs_addr, properties=properties) session.add(belongs) else: tbl_belongs.properties = properties session.commit() except IntegrityError: session.rollback() raise ValueError() # store slice self.slices[dscp] = slc # create slice on WTPs for wtp_addr in self.wtps: wtp = self.wtps[wtp_addr] if not wtp.is_online(): continue for block in wtp.supports: wtp.connection.send_set_slice(block, slc) # create slice on VBSes for vbs_addr in self.vbses: vbs = self.vbses[vbs_addr] if not vbs.is_online(): continue current_rntis = [] # The UEs in the slice must be confirmed for ue in list(self.ues.values()): if vbs == ue.vbs and dscp == ue.slice: current_rntis.append(ue.rnti) for cell in vbs.cells.values(): vbs.connection.\ send_add_set_ran_mac_slice_request(cell, slc, EP_OPERATION_SET, current_rntis)