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 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 load_pending_tenants(cls, username=None): """Fetch pending tenants requests.""" if username: return Session().query(TblPendingTenant) \ .filter(TblPendingTenant.owner == username) \ .all() else: return Session().query(TblPendingTenant).all()
def __load_slices(self): """Load Slices.""" slices = Session().query(TblSlice).all() for slc in slices: tenant = RUNTIME.tenants[slc.tenant_id] desc = { 'dscp': slc.dscp, 'wtps': {}, 'vbses': {}, 'wifi': json.loads(slc.wifi), 'lte': json.loads(slc.lte) } if slc.dscp not in tenant.slices: tenant.slices[slc.dscp] = Slice(slc.dscp, tenant, desc) t_slice = tenant.slices[slc.dscp] belongs = \ Session().query(TblSliceBelongs) \ .filter(TblSliceBelongs.dscp == slc.dscp) \ .filter(TblSliceBelongs.tenant_id == slc.tenant_id) \ .all() for belong in belongs: if belong.addr not in self.pnfdevs: continue pnfdev = self.pnfdevs[belong.addr] pnfdevs = None if pnfdev.ALIAS == "vbses": pnfdevs = t_slice.lte[pnfdev.ALIAS] if pnfdev.addr not in pnfdevs: pnfdevs[belong.addr] = { 'static-properties': {}, 'cells': {} } else: pnfdevs = t_slice.wifi[pnfdev.ALIAS] if pnfdev.addr not in pnfdevs: pnfdevs[belong.addr] = { 'static-properties': {}, 'blocks': {} } pnfdevs[belong.addr]['static-properties'] = \ json.loads(belong.properties)
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 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 password(self, password): """Set name.""" session = Session() account = session.query(TblAccount) \ .filter(TblAccount.username == self.username) \ .first() account.password = password session.commit() self._password = password
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 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_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 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 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_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 __load_acl(self): """ Load ACL list. """ for allow in Session().query(TblAllow).all(): if allow.addr in self.allowed: raise ValueError(allow.addr_str) acl = ACL(allow.addr, allow.label) self.allowed[allow.addr] = acl for deny in Session().query(TblDeny).all(): if deny.addr in self.denied: raise ValueError(deny.addr_str) self.denied[deny.addr] = acl
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 __load_accounts(self): """Load accounts table.""" for account in Session().query(TblAccount).all(): self.accounts[account.username] = Account( account.username, account.password, account.name, account.surname, account.email, account.role)
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 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 del_traffic_rule(self, match): """Delete a traffic rule from this tenant. Args: match, a Match object Returns: None Raises: None """ rule = Session().query(TblTrafficRule) \ .filter(TblTrafficRule.tenant_id == self.tenant_id, TblTrafficRule.match == match) \ .first() if not rule: raise KeyError(rule) # Send command to IBN from empower.ibnp.ibnpserver import IBNPServer ibnp_server = get_module(IBNPServer.__module__) if ibnp_server: ibnp_server.del_traffic_rule(self.tenant_id, match) session = Session() session.delete(rule) session.commit()
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 __load_pnfdevs(self): """Load PNFDevs.""" pnfdevs = Session().query(self.TBL_PNFDEV).all() for pnfdev in pnfdevs: if pnfdev.addr in self.pnfdevs: raise KeyError(pnfdev.addr) self.pnfdevs[pnfdev.addr] = \ self.PNFDEV(pnfdev.addr, pnfdev.label)
def create_account(self, username, password, role, name, surname, email): """Create a new account.""" if username in self.accounts: raise ValueError("%s already registered" % username) if role not in [ROLE_ADMIN, ROLE_USER]: raise ValueError("Invalid role %s" % role) 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_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 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 bind_feed(self, feed_id, pnfdev_addr=None): """Bind Feed to PNFDev.""" if feed_id not in RUNTIME.feeds: raise KeyError(feed_id) feed = RUNTIME.feeds[feed_id] # if the feed is pointing to a dev, then reset the feed attribute of # that dev to None if feed.pnfdev: pnfdev = Session().query(TblPNFDev) \ .filter(TblPNFDev.addr == EtherAddress(feed.pnfdev.addr)) \ .first() if pnfdev: pnfdevs = getattr(RUNTIME, pnfdev.tbl_type) pnfdevs[feed.pnfdev.addr].feed = None # reset fedd pnfdev attribute to none feed.pnfdev = None # set the new pnfdev if pnfdev_addr: pnfdev = Session().query(TblPNFDev) \ .filter(TblPNFDev.addr == EtherAddress(pnfdev_addr)) \ .first() if not pnfdev: raise KeyError(pnfdev_addr) pnfdevs = getattr(RUNTIME, pnfdev.tbl_type) pnfdev = pnfdevs[pnfdev_addr] feed.pnfdev = pnfdev pnfdev.feed = feed
def bind_feed(self, feed_id, addr=None): """Bind Feed to PNFDev.""" if feed_id not in RUNTIME.feeds: raise KeyError(feed_id) feed = RUNTIME.feeds[feed_id] # if the feed is pointing to a dev, then reset the feed attribute of # that dev to None if feed.pnfdev: pnfdev = Session().query(TblPNFDev) \ .filter(TblPNFDev.addr == EtherAddress(feed.pnfdev.addr)) \ .first() if pnfdev: pnfdevs = getattr(RUNTIME, pnfdev.tbl_type) pnfdevs[feed.pnfdev.addr].feed = None # reset fedd pnfdev attribute to none feed.pnfdev = None # set the new pnfdev if addr: pnfdev = Session().query(TblPNFDev) \ .filter(TblPNFDev.addr == EtherAddress(addr)) \ .first() if not pnfdev: raise KeyError(addr) pnfdevs = getattr(RUNTIME, pnfdev.tbl_type) pnfdev = pnfdevs[addr] feed.pnfdev = pnfdev pnfdev.feed = feed
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 __load_tenants(self): """Load Tenants.""" for tenant in Session().query(TblTenant).all(): if tenant.tenant_id in self.tenants: raise KeyError(tenant.tenant_id) self.tenants[tenant.tenant_id] = \ Tenant(tenant.tenant_id, tenant.tenant_name, tenant.owner, tenant.desc, tenant.bssid_type)
def __load_traffic_rules(self): rules = Session().query(TblTrafficRule).all() for rule in rules: tenant = RUNTIME.tenants[rule.tenant_id] traffic_rule = TrafficRule(tenant=tenant, match=rule.match, priority=rule.priority, label=rule.label, dscp=rule.dscp) self.add_traffic_rule(traffic_rule)
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 add_tenant(self, owner, desc, tenant_name, bssid_type, tenant_id=None, plmn_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, bssid_type=bssid_type, plmn_id=plmn_id) else: request = TblTenant(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) self.tenants[request.tenant_id] = \ Tenant(request.tenant_id, request.tenant_name, self.accounts[owner].username, desc, request.bssid_type, request.plmn_id) 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 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 __load_belongs(self): """Load Tenant/PNFDevs relationship.""" for belongs in Session().query(TblBelongs).all(): if belongs.addr not in self.pnfdevs: continue if belongs.tenant_id not in RUNTIME.tenants: raise KeyError("Tenant not found %s", belongs.tenant_id) pnfdev = self.pnfdevs[belongs.addr] tenant = RUNTIME.tenants[belongs.tenant_id] tenant_pnfdevs = getattr(tenant, self.PNFDEV.ALIAS) tenant_pnfdevs[pnfdev.addr] = pnfdev
def add_tenant(self, owner, desc, tenant_name, bssid_type, tenant_id=None, plmn_id=None): """Create new Tenant.""" if tenant_id in self.tenants: raise ValueError("Tenant %s exists" % tenant_id) plmn_ids = [tenant.plmn_id for tenant in self.tenants.values()] if plmn_id and plmn_id in plmn_ids: raise ValueError("PLMN ID %s exists" % plmn_id) if bssid_type not in T_TYPES: raise ValueError("Invalid bssid_type %s" % bssid_type) session = Session() if tenant_id: request = TblTenant(tenant_id=tenant_id, tenant_name=tenant_name, owner=owner, desc=desc, bssid_type=bssid_type, plmn_id=plmn_id) else: request = TblTenant(owner=owner, tenant_name=tenant_name, desc=desc, bssid_type=bssid_type, plmn_id=plmn_id) session.add(request) session.commit() self.tenants[request.tenant_id] = \ Tenant(request.tenant_id, request.tenant_name, self.accounts[owner].username, desc, request.bssid_type, request.plmn_id) # create default queue dscp = DSCP() descriptor = {} self.tenants[request.tenant_id].add_slice(dscp, descriptor) return request.tenant_id
def traffic_rules(self): """Fetch traffic rule queues in this tenant.""" trs = \ Session().query(TblTrafficRule) \ .filter(TblTrafficRule.tenant_id == self.tenant_id) \ .all() results = {} for rule in trs: results[rule.match] = {'match': rule.match, 'label': rule.label, 'priority': rule.priority, 'dscp': rule.dscp} return results
def add_pnfdev(self, addr, label): """Add PNFDev.""" if addr in self.pnfdevs: raise ValueError("Device address %s already present" % 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]