def set_ended(self, session_id): ''' Set the ended status for the session identified with session_id ''' session = get_session() with session.begin(): session.query(SessionModel).filter_by(id=session_id).update({"ended":datetime.datetime.now()}, synchronize_session = False)
def getFlowruleByInternalID(self, internal_id=None): try: session = get_session() return session.query(FlowRuleModel).filter_by(internal_id=internal_id).one() except: return None return None
def dbStoreGraphSessionFromNffgObject(self, session_id, user_id, nffg): session = get_session() with session.begin(): graphsession_ref = GraphSessionModel(session_id=session_id, user_id=user_id, graph_id=nffg.id, started_at = datetime.datetime.now(), graph_name=nffg.name, last_update = datetime.datetime.now(), status='inizialization', description=nffg.description) session.add(graphsession_ref)
def getFlowruleByID(self, flow_rule_id=None): try: session = get_session() return session.query(FlowRuleModel).filter_by(id=flow_rule_id).one() except: return None return None
def getMatchByFlowruleID(self, flowrule_id): try: session = get_session() return session.query(MatchModel).filter_by(flow_rule_id=flowrule_id).one() except: return None return None
def getEndpointsBySessionID(self, session_id): session = get_session() try: ep = session.query(EndpointModel).filter_by(session_id = session_id).all() return ep except: return None
def externalFlowruleExists(self, switch_id, internal_id): session = get_session() try: session.query(FlowRuleModel).filter_by(internal_id=internal_id).filter_by(switch_id=switch_id).filter_by(type='external').one() return True except: return False
def getActiveUserGraphSession(self, user_id, graph_id, error_aware=True): session = get_session() if error_aware: session_ref = session.query(GraphSessionModel).filter_by(user_id = user_id).filter_by(graph_id = graph_id).filter_by(ended = None).filter_by(error = None).first() else: session_ref = session.query(GraphSessionModel).filter_by(user_id = user_id).filter_by(graph_id = graph_id).filter_by(ended = None).order_by(desc(GraphSessionModel.started_at)).first() return session_ref
def getEndpointByGraphID(self, graph_endpoint_id, session_id): session = get_session() try: ep = session.query(EndpointModel).filter_by(session_id = session_id).filter_by(graph_endpoint_id = graph_endpoint_id).one() return ep except: return None
def getTenantName(self, tenant_id): session = get_session() try: return session.query(TenantModel).filter_by(id = tenant_id).one().name except Exception as ex: logging.error(ex) raise TenantNotFound("Tenant not found: "+str(tenant_id))
def checkToken(self, token): session = get_session() try: session.query(UserModel).filter_by(token = token).one() return True except Exception: return False
def getUserByID(self, user_id): session = get_session() try: return session.query(UserModel).filter_by(id = user_id).one() except Exception as ex: logging.error(ex) raise UserNotFound("User not found: "+str(user_id)+" (id)")
def getUserByToken(self, token): session = get_session() try: return session.query(UserModel).filter_by(token = token).one() except Exception as ex: logging.error(ex) raise UserNotFound("Token not found: "+str(token))
def getUser(self, username): session = get_session() try: return session.query(UserModel).filter_by(name = username).one() except Exception as ex: logging.error(ex) raise UserNotFound("User not found: "+str(username)) from None
def dbStoreAction(self, action, flow_rule_db_id, action_db_id=None, output_to_port=None, output_type=None): session = get_session() if action_db_id is None: action_db_id = session.query(func.max(ActionModel.id).label("max_id")).one().max_id if action_db_id is None: action_db_id = 0 else: action_db_id = int(action_db_id) + 1 if output_to_port is None: output_to_port=action.output with session.begin(): action_ref = ActionModel(id=action_db_id, flow_rule_id=flow_rule_db_id, output_type=output_type, output_to_port=output_to_port, output_to_controller=action.controller, _drop=action.drop, set_vlan_id=action.set_vlan_id, set_vlan_priority=action.set_vlan_priority, push_vlan=action.push_vlan, pop_vlan=action.pop_vlan, set_ethernet_src_address=action.set_ethernet_src_address, set_ethernet_dst_address=action.set_ethernet_dst_address, set_ip_src_address=action.set_ip_src_address, set_ip_dst_address=action.set_ip_dst_address, set_ip_tos=action.set_ip_tos, set_l4_src_port=action.set_l4_src_port, set_l4_dst_port=action.set_l4_dst_port, output_to_queue=action.output_to_queue) session.add(action_ref) return action_ref
def getEndpointResourcesByEndpointID(self, endpoint_id): session = get_session() try: eprs = session.query(EndpointResourceModel).filter_by(endpoint_id = endpoint_id).all() return eprs except: return None
def set_error(self, session_id): ''' Set the error status for the active session associated to the user id passed ''' session = get_session() with session.begin(): logging.debug("Put session for session "+str(session_id)+" in error") session.query(SessionModel).filter_by(id=session_id).filter_by(ended = None).filter_by(error = None).update({"error":datetime.datetime.now()}, synchronize_session = False)
def isDirectEndpoint(self, port_in, switch_id): session = get_session() if port_in is None or switch_id is None: return False query_ref = session.query(VlanModel.id).filter_by(vlan_in=None).filter_by(switch_id=switch_id).filter_by(port_in=port_in).all() if len(query_ref)>0: return True return False
def deleteFlowruleByID(self, flow_rule_id): # delete from tables: FlowRuleModel, MatchModel, ActionModel, VlanModel, EndpointResourceModel. session = get_session() with session.begin(): session.query(FlowRuleModel).filter_by(id=flow_rule_id).delete() session.query(MatchModel).filter_by(flow_rule_id=flow_rule_id).delete() session.query(ActionModel).filter_by(flow_rule_id=flow_rule_id).delete() session.query(VlanModel).filter_by(flow_rule_id=flow_rule_id).delete() session.query(EndpointResourceModel).filter_by(resource_id=flow_rule_id).filter_by(resource_type='flow-rule').delete()
def get_active_user_session(self, user_id): ''' returns if exists an active session of the user ''' session = get_session() session_ref = session.query(SessionModel).filter_by(user_id = user_id).filter_by(ended = None).filter_by(error = None).first() if session_ref is None: raise sessionNotFound("Session Not Found") return session_ref
def get_active_user_session_by_nf_fg_id(self, service_graph_id, error_aware=True): session = get_session() if error_aware: session_ref = session.query(SessionModel).filter_by(service_graph_id = service_graph_id).filter_by(ended = None).filter_by(error = None).first() else: session_ref = session.query(SessionModel).filter_by(service_graph_id = service_graph_id).filter_by(ended = None).order_by(desc(SessionModel.started_at)).first() if session_ref is None: raise sessionNotFound("Session Not Found, for servce graph id: "+str(service_graph_id)) return session_ref
def getEndpointVlanInIDs(self, port_in, switch_id): session = get_session() qref = session.query(FlowRuleModel,MatchModel).\ filter(FlowRuleModel.id == MatchModel.flow_rule_id).\ filter(FlowRuleModel.switch_id == switch_id).\ filter(MatchModel.port_in == port_in).\ all() if len(qref)>0: return qref return None
def cleanAll(self): session = get_session() session.query(ActionModel).delete() session.query(EndpointModel).delete() session.query(EndpointResourceModel).delete() session.query(FlowRuleModel).delete() session.query(GraphSessionModel).delete() session.query(MatchModel).delete() session.query(PortModel).delete() session.query(VlanModel).delete() session.query(GraphSessionModel).delete()
def ingressVlanIsBusy(self, vlan_in, port_in, switch_id, query_ref=None): session = get_session() if vlan_in is None or port_in is None or switch_id is None: return False new_query_ref = session.query(VlanModel).filter_by(vlan_in=vlan_in).filter_by(switch_id=switch_id).filter_by(port_in=port_in).all() if len(new_query_ref)>0: if query_ref is not None: query_ref.clear() query_ref.extend(new_query_ref) return True return False
def inizializeSession(self, session_id, user_id, service_graph_id, service_graph_name): ''' inizialize the session in db ''' session = get_session() with session.begin(): session_ref = SessionModel(id=session_id, user_id = user_id, service_graph_id = service_graph_id, started_at = datetime.datetime.now(), service_graph_name=service_graph_name, last_update = datetime.datetime.now(), status='inizialization') session.add(session_ref) pass
def addVlanTracking(self, flow_rule_id, switch_id, vlan_in, port_in, vlan_out, port_out): session = get_session() max_id = session.query(func.max(VlanModel.id).label("max_id")).one().max_id if max_id is None: max_id = 0 else: max_id = int(max_id)+1 with session.begin(): vlan_ref = VlanModel(id=max_id, flow_rule_id=flow_rule_id, switch_id=switch_id, vlan_in=vlan_in, port_in=port_in, vlan_out=vlan_out, port_out=port_out) session.add(vlan_ref)
def dbStoreEndpoint(self, session_id, endpoint_id, graph_endpoint_id, name, _type): session = get_session() if endpoint_id is None: endpoint_id = session.query(func.max(EndpointModel.id).label("max_id")).one().max_id if endpoint_id is None: endpoint_id = 0 else: endpoint_id=int(endpoint_id)+1 with session.begin(): endpoint_ref = EndpointModel(id=endpoint_id, graph_endpoint_id=graph_endpoint_id, session_id=session_id, name=name, type=_type) session.add(endpoint_ref) return endpoint_id
def dbStoreFlowrule(self, session_id, flow_rule, flow_rule_db_id, switch_id): session = get_session() if flow_rule_db_id is None: flow_rule_db_id = session.query(func.max(FlowRuleModel.id).label("max_id")).one().max_id if flow_rule_db_id is None: flow_rule_db_id = 0 else: flow_rule_db_id=int(flow_rule_db_id)+1 with session.begin(): flow_rule_ref = FlowRuleModel(id=flow_rule_db_id, internal_id=flow_rule.internal_id, graph_flow_rule_id=flow_rule.id, session_id=session_id, switch_id=switch_id, priority=flow_rule.priority, status=None, description=flow_rule.description, creation_date=datetime.datetime.now(), last_update=datetime.datetime.now(), type=flow_rule.type) session.add(flow_rule_ref) return flow_rule_db_id
def getFlowruleProgressionPercentage(self,session_id,nffg_id): session = get_session() percentage = 0 flowrules = session.query(FlowRuleModel).filter_by(session_id=session_id).all() count_flowrules = len(flowrules) if count_flowrules<=0: return 0 for fr in flowrules: internal_flowrules = session.query(FlowRuleModel).filter_by(session_id=session_id).filter_by(graph_flow_rule_id=fr.graph_flow_rule_id).filter_by(type='external').all() if len(internal_flowrules)>0: percentage = percentage+1 return ( percentage / count_flowrules * 100 )
def getNewUnivocalSessionID(self): ''' Compute a new session id 32 byte long. Check if it is already exists: if yes, repeat the computation. ''' session = get_session() rows = session.query(GraphSessionModel.session_id).all() while True: session_id = uuid.uuid4().hex found = False for row in rows: if(row.session_id == session_id): found = True break if found==False: return session_id