def _seg_id_allocations(self): seg_ids = set() for seg_id_range in self.seg_id_ranges: seg_min, seg_max = seg_id_range seg_ids |= set(moves.range(seg_min, seg_max + 1)) session = db.get_session() with session.begin(subtransactions=True): allocs = session.query(self.model).all() for alloc in allocs: try: seg_ids.remove(alloc.segmentation_id) except KeyError: # it's not allocatable, so check if its allocated if not alloc.allocated: # it's not, so remove it from table LOG.info( _LI("Removing seg_id %s from pool") % alloc.segmentation_id) session.delete(alloc) for seg_id in sorted(seg_ids): alloc = self.model(segmentation_id=seg_id) session.add(alloc)
def update_project_entry(self, pid, dci_id, result): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaTenants).filter_by(id=pid).update({ 'result': result, 'dci_id': dci_id })
def release_segmentation_id(self, seg_id): inside = any(lo <= seg_id <= hi for lo, hi in self.seg_id_ranges) session = db.get_session() with session.begin(subtransactions=True): query = session.query(self.model).filter_by(segmentation_id=seg_id) if inside: del_time = utils.utc_time(time.ctime()) count = query.update({ "allocated": False, "network_id": None, "source": None, "delete_time": del_time }) if count: LOG.info( _LI("Releasing segmentation id %s to pool") % seg_id) else: count = query.delete() if count: LOG.info( _LI("Releasing segmentation_id %s outside pool") % seg_id) if not count: LOG.info(_LI("segmentation_id %s not found") % seg_id)
def release_subnet(self, subnet_address): subnet_addr_int = int(netaddr.IPAddress(subnet_address)) inside = any(lo <= subnet_addr_int <= hi for lo, hi in self.subnet_ranges) session = db.get_session() with session.begin(subtransactions=True): query = session.query( self.model).filter_by(subnet_address=subnet_address) if inside: count = query.update({ "allocated": False, "network_id": None, "subnet_id": None }) if count: LOG.info( _LI("Releasing subnet id %s to pool") % subnet_address) else: count = query.delete() if count: LOG.info( _LI("Releasing subnet %s outside pool") % subnet_address) if not count: LOG.info(_LI("subnet %s not found") % subnet_address)
def _subnet_id_allocations(self): subnet_ids = sorted( set(moves.range(self.subnet_min, self.subnet_max, self.step))) # seg_ids = set() # for subnet_range in self.subnet_ranges: # subnet_min, subnet_max = subnet_range # subnet_ids |= set(moves.xrange(subnet_min, subnet_max, self.step)) session = db.get_session() with session.begin(subtransactions=True): allocs = (session.query(self.model).all()) for alloc in allocs: try: ip = int(netaddr.IPAddress(alloc.subnet_address)) subnet_ids.remove(ip) except KeyError: # it's not allocatable, so check if its allocated if not alloc.allocated: # it's not, so remove it from table LOG.info( _LI("Removing subnet %s from pool") % alloc.subnet_address) session.delete(alloc) for subnet_id in subnet_ids: subnet_add = str(netaddr.IPAddress(subnet_id)) alloc = self.model(subnet_address=subnet_add) session.add(alloc)
def get_seg_netid_src(self, source): session = db.get_session() netid_dict = {} allocs = (session.query(self.model).filter_by(source=source).all()) for alloc in allocs: if alloc.network_id is not None: netid_dict[alloc.network_id] = alloc.segmentation_id return netid_dict
def get_all_seg_netid(self): session = db.get_session() netid_dict = {} allocs = (session.query(self.model).all()) for alloc in allocs: if alloc.network_id is not None: netid_dict[alloc.network_id] = alloc.segmentation_id return netid_dict
def get_network_by_name(self, name): session = db.get_session() try: with session.begin(subtransactions=True): net = session.query(DfaNetwork).filter_by(name=name).all() return net except orm_exc.NoResultFound: LOG.info(_LI('Network %(name)s does not exist'), {'name': name})
def update_subnet(self, subnet, net_id, subnet_id): session = db.get_session() with session.begin(subtransactions=True): session.query(self.model).filter_by(subnet_address=subnet).update({ "network_id": net_id, "subnet_id": subnet_id })
def update_fw_db_result(self, fw_id, fw_data): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaFwInfo).filter_by(fw_id=fw_id).update({ 'openstack_provision_status': fw_data.get('os_status'), 'dcnm_provision_status': fw_data.get('dcnm_status') })
def release_subnet_by_netid(self, netid): session = db.get_session() try: with session.begin(subtransactions=True): session.query(self.model).filter_by(allocated=True, network_id=netid).update( {"allocated": False}) except orm_exc.NoResultFound: LOG.info("Network %(netid)s does not exist" % ({'netid': netid}))
def release_subnet_no_netid(self): net = '' session = db.get_session() try: with session.begin(subtransactions=True): (session.query(self.model).filter_by(allocated=True, network_id=net).update( {"allocated": False})) except orm_exc.NoResultFound: LOG.error(_LE("Query failed in release subnet no netid"))
def get_project_name(self, pid): session = db.get_session() try: with session.begin(subtransactions=True): ent = session.query(DfaTenants).filter_by(id=pid).one() return ent and ent.name except orm_exc.NoResultFound: LOG.info('Project %(id)s does not exist', {'id': pid}) except orm_exc.MultipleResultsFound: LOG.error('More than one enty found for project %(id)s.', {'id': pid})
def del_project_db(self, pid): session = db.get_session() try: with session.begin(subtransactions=True): ent = session.query(DfaTenants).filter_by(id=pid).one() session.delete(ent) except orm_exc.NoResultFound: LOG.info(_LI('Project %(id)s does not exist'), {'id': pid}) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for project %(id)s.'), {'id': pid})
def get_project_id(self, name): session = db.get_session() try: with session.begin(subtransactions=True): ent = session.query(DfaTenants).filter_by(name=name).one() return ent and ent.id except orm_exc.NoResultFound: LOG.info(_LI('Project %(name)s does not exist'), {'name': name}) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for project %(name)s.'), {'name': name})
def get_agent_configurations(self, host): session = db.get_session() with session.begin(subtransactions=True): try: ent = session.query(DfaAgentsDb).filter_by(host=host).one() return ent.configurations except orm_exc.NoResultFound: LOG.info(_LI('Agent %(host)s does not exist.'), {'host': host}) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for agent %(host)s.'), {'host': host})
def add_network_db(self, net_id, net_data, source, result): session = db.get_session() with session.begin(subtransactions=True): net = DfaNetwork(network_id=net_id, name=net_data.get('name'), config_profile=net_data.get('config_profile'), segmentation_id=net_data.get('segmentation_id'), tenant_id=net_data.get('tenant_id'), fwd_mod=net_data.get('fwd_mod'), source=source, result=result) session.add(net)
def get_vm(self, port_id): session = db.get_session() try: with session.begin(subtransactions=True): port = session.query(DfaVmInfo).filter_by( port_id=port_id).one() return port except orm_exc.NoResultFound: LOG.info(_LI('Port %(id)s does not exist'), {'id': port_id}) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for Port %(id)s.'), {'id': port_id})
def get_network(self, net_id): session = db.get_session() try: with session.begin(subtransactions=True): net = session.query(DfaNetwork).filter_by( network_id=net_id).one() return net except orm_exc.NoResultFound: LOG.info('Network %(id)s does not exist', {'id': net_id}) except orm_exc.MultipleResultsFound: LOG.error('More than one enty found for network %(id)s.', {'id': net_id})
def get_network_by_segid(self, segid): session = db.get_session() try: with session.begin(subtransactions=True): net = session.query(DfaNetwork).filter_by( segmentation_id=segid).one() return net except orm_exc.NoResultFound: LOG.info(_LI('Network %(segid)s does not exist'), {'segid': segid}) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for seg-id %(id)s.'), {'id': segid})
def get_fw_by_rtrid(self, rtrid): session = db.get_session() try: with session.begin(subtransactions=True): rtr = session.query(DfaFwInfo).filter_by(router_id=rtrid) except orm_exc.NoResultFound: LOG.info(_LI('rtr %(rtrid)s does not exist') % ({'rtrid': rtrid})) except orm_exc.MultipleResultsFound: LOG.error(_LE("More than one enty found for rtrid-id %(id)s."), ({ 'id': rtrid })) return rtr
def query_topology_db(self, dict_convert=False, **req): """Query an entry to the topology DB. """ session = db.get_session() with session.begin(subtransactions=True): try: # Check if entry exists. topo_disc = session.query(DfaTopologyDb).filter_by(**req).all() except orm_exc.NoResultFound: LOG.info("No Topology results found for %s", req) return None if dict_convert: return self._convert_topo_obj_dict(topo_disc) return topo_disc
def delete_topology_entry(self, **req): """Delete the entries from the topology DB. """ session = db.get_session() with session.begin(subtransactions=True): try: rows = session.query(DfaTopologyDb).filter_by(**req).all() except orm_exc.NoResultFound: LOG.info("No Topology results found for %s", req) return try: for row in rows: session.delete(row) except Exception as exc: LOG.error("Exception raised %s", str(exc))
def get_fw_by_rtr_netid(self, netid): session = db.get_session() try: with session.begin(subtransactions=True): net = session.query(DfaFwInfo).filter_by( router_net_id=netid).one() return net except orm_exc.NoResultFound: LOG.info('Network %(segid)s does not exist' % ({'netid': netid})) except orm_exc.MultipleResultsFound: LOG.error("More than one enty found for netid-id %(id)s.", ({ 'id': netid }))
def get_fw_by_tenant_id(self, tenant_id): session = db.get_session() try: with session.begin(subtransactions=True): fw = session.query(DfaFwInfo).filter( (DfaFwInfo.tenant_id == tenant_id)).one() fw_dict = self.conv_db_dict(fw) return fw_dict except orm_exc.NoResultFound: LOG.info(_LI('FW %s does not exist') % tenant_id) except orm_exc.MultipleResultsFound: LOG.error(_LE("More than one enty found for tenant-id %(id)s."), ({ 'id': tenant_id }))
def get_fw(self, fw_id): session = db.get_session() fw = None try: with session.begin(subtransactions=True): fw = session.query(DfaFwInfo).filter_by(fw_id=fw_id).first() fw_dict = self.conv_db_dict(fw) except orm_exc.NoResultFound: LOG.info(_LI('fw %(fwid)s does not exist') % ({'fw_id': fw_id})) except orm_exc.MultipleResultsFound: LOG.error(_LE("More than one enty found for fwid-id %(id)s."), ({ 'id': fw_id })) return fw, fw_dict
def get_subnet(self, sub): session = db.get_session() try: with session.begin(subtransactions=True): net = session.query(self.model).filter_by(allocated=True, subnet_address=sub).\ one() return net except orm_exc.NoResultFound: LOG.info('subnet %(sub)s does not exist', ({'sub': sub})) except orm_exc.MultipleResultsFound: LOG.error("More than one enty found for sub %(sub)s.", ({ 'sub': sub })) return None
def clear_fw_entry_by_netid(self, net_id): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaFwInfo).filter_by(in_network_id=net_id).update({ 'in_network_id': '', 'in_service_node_ip': '' }) # We don't need to do the below if above succeeds, TODO(padkrish) session.query(DfaFwInfo).filter_by(out_network_id=net_id).update({ 'out_network_id': '', 'out_service_node_ip': '' })
def add_vms_db(self, vm_data, result): session = db.get_session() with session.begin(subtransactions=True): vm = DfaVmInfo(instance_id=vm_data['oui'].get('vm_uuid'), name=vm_data['oui'].get('vm_name'), status=vm_data.get('status'), network_id=vm_data.get('net_uuid'), port_id=vm_data.get('port_uuid'), ip=vm_data['oui'].get('ip_addr'), mac=vm_data.get('vm_mac'), segmentation_id=vm_data.get('segmentation_id'), fwd_mod=vm_data['oui'].get('fwd_mod'), gw_mac=vm_data['oui'].get('gw_mac'), host=vm_data.get('host'), result=result) session.add(vm)
def get_fw_by_netid(self, netid): session = db.get_session() try: with session.begin(subtransactions=True): fw = session.query(DfaFwInfo).filter( (DfaFwInfo.in_network_id == netid) | (DfaFwInfo.out_network_id == netid)).one() return fw except orm_exc.NoResultFound: LOG.info(_LI('FW %(netid)s does not exist'), ({'netid': netid})) except orm_exc.MultipleResultsFound: LOG.error(_LE("More than one enty found for netid-id %(id)s."), ({ 'id': netid })) return None
def update_agent_db(self, agent_info): session = db.get_session() host = agent_info.get('host') with session.begin(subtransactions=True): try: # Check if entry exists. session.query(DfaAgentsDb).filter_by(host=host).one() # Entry exist, only update the heartbeat and configurations. session.query(DfaAgentsDb).filter_by(host=host).update( {'heartbeat': agent_info.get('timestamp')}) except orm_exc.NoResultFound: LOG.info(_LI('Creating new entry for agent on %(host)s.'), {'host': host}) agent = DfaAgentsDb(host=host, created=agent_info.get('timestamp'), heartbeat=agent_info.get('timestamp'), configurations=agent_info.get('config')) session.add(agent) except orm_exc.MultipleResultsFound: LOG.error(_LE('More than one enty found for agent %(host)s.'), {'host': host})
def get_all_projects(self): session = db.get_session() with session.begin(subtransactions=True): projs = session.query(DfaTenants).all() return projs
def update_project_entry(self, pid, dci_id, result): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaTenants).filter_by(id=pid).update( {'result': result, 'dci_id': dci_id})
def get_all_networks(self): session = db.get_session() with session.begin(subtransactions=True): nets = session.query(DfaNetwork).all() return nets
def update_network_db(self, net_id, result): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaNetwork).filter_by( network_id=net_id).update({"result": result})
def update_network(self, net_id, **params): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaNetwork).filter_by( network_id=net_id).update(params.get('columns'))
def add_project_db(self, pid, name, dci_id, result): proj = DfaTenants(id=pid, name=name, dci_id=dci_id, result=result) session = db.get_session() with session.begin(subtransactions=True): session.add(proj)
def update_agent_configurations(self, host, configs): session = db.get_session() with session.begin(subtransactions=True): # Update the configurations. return session.query(DfaAgentsDb).filter_by(host=host).update( {'configurations': configs})
def update_vm_db(self, vm_port_id, **params): session = db.get_session() with session.begin(subtransactions=True): session.query(DfaVmInfo).filter_by( port_id=vm_port_id).update(params.get('columns'))
def get_fialed_projects_entries(self, fail_res): session = db.get_session() with session.begin(subtransactions=True): ent = session.query(DfaTenants).filter_by(result=fail_res).all() return ent
def get_vms_for_this_req(self, **req): session = db.get_session() with session.begin(subtransactions=True): vms = session.query(DfaVmInfo).filter_by(**req).all() return vms
def get_vms(self): session = db.get_session() with session.begin(subtransactions=True): vms = session.query(DfaVmInfo).all() return vms
def delete_network_db(self, net_id): session = db.get_session() with session.begin(subtransactions=True): net = session.query(DfaNetwork).filter_by( network_id=net_id).first() session.delete(net)
def delete_vm_db(self, vm_uuid): session = db.get_session() with session.begin(subtransactions=True): vm = session.query(DfaVmInfo).filter_by( instance_id=vm_uuid).first() session.delete(vm)