def test_policy_functin_values_delitem(self, value): pf = PolicyFunction(value) values = [x.strip() for x in value.split(',')] for i in range(len(values)): pv = PolicyValue.fromString(i) del pf[pv] assert pf[pv] == PolicyValue.fromString(values[i])
def test_policy_functin_values_getitem(self, value): pf = PolicyFunction(value) if value == PolicyFunction.PASS_EVERYTHING: rnd = random.randint(0, 150) assert pf[PolicyValue(rnd)].value == rnd return values = [x.strip() for x in value.split(',')] for i in range(len(values)): pv = PolicyValue.fromString(i) assert pf[pv] == PolicyValue.fromString(values[i])
def __evaluate_pkt(self, row_value: str) -> str: """__evaluate_pkt. Evaluate a single pkt row of the DF, it will return the compressed string corresponding to the row, A{ID} for an advertisement or W{ID} for a withdraw, the id corresponds to the route in the route_to_id DataFrame :param row_value: row of the dataframe that contains a packet that needs to be analyzed :type row_value: str :rtype: str """ # Get the packet and the route transmitted packet = Packet.fromString(row_value) route = Route.fromString(packet.content) tmp_pv = PolicyValue(0) route.policy_value = tmp_pv # If the route is not in the DataFrane of routes add it if hash(route) not in self.routes.index: self.routes.loc[hash(route)] = self.__get_route_data(route) # Check the packet type and return the corresponding compressed version if packet.packet_type == Packet.UPDATE: return "A" + str( self.routes.loc[hash(route)][NodeAnalyzer.ROUTES_COLUMNS[1]]) if packet.packet_type == Packet.WITHDRAW: return "W" + str( self.routes.loc[hash(route)][NodeAnalyzer.ROUTES_COLUMNS[1]]) print("Something very bad happened") sys.exit(2)
def test_policyvalue_setter(self, addr, path, nh, policy_value): ipaddr = ipaddress.ip_network(addr) pl = PolicyValue(policy_value) route = Route(ipaddr, path, nh) assert route.policy_value.value == 0 route.policy_value = pl assert route.policy_value.value == pl.value assert id(route.policy_value) == id(pl)
def test_policy_value_deepcopy(self, value): pl = PolicyValue(value) memo={} test = copy.deepcopy(pl, memo) test.value = value + 100 assert not id(pl) == id(test) assert id(pl.value) != id(test.value) test = copy.deepcopy(pl, memo) test.value = value + 100 assert not id(pl) == id(test) assert id(pl.value) != id(test.value)
def fromString(cls, string: str): # pylint: disable=invalid-name """fromString. Method to get a route object from the string representation :param string: String representation of the route :type string: str """ res = ast.literal_eval(string) return cls(ipaddress.ip_network(res["addr"]), res["path"], res["nh"], policy_value=PolicyValue.fromString(res["policy_value"]))
def __statate_swap_variation(self, rx_value: str, new_state: set) -> set: pkt = Packet.fromString(rx_value) route = Route.fromString(pkt.content) route.policy_value = PolicyValue(0) self.experiment_actual_state = new_state.copy() new_state = self.actual_state.copy() for row_value, row_nh in zip( self.routes[NodeAnalyzer.ROUTES_COLUMNS[1]], self.routes[NodeAnalyzer.ROUTES_COLUMNS[3]]): if row_nh == route.nh: if row_value in new_state: new_state.remove(row_value) new_state.add( self.routes.loc[hash(route)][NodeAnalyzer.ROUTES_COLUMNS[1]]) return new_state
def __statate_incremental_variation(self, rx_value: str, new_state: set) -> set: pkt = Packet.fromString(rx_value) route = Route.fromString(pkt.content) route.policy_value = PolicyValue(0) if pkt.packet_type == Packet.UPDATE: self.experiment_actual_state = new_state.copy() new_state = self.actual_state.copy() new_state.add( self.routes.loc[hash(route)][NodeAnalyzer.ROUTES_COLUMNS[1]]) elif pkt.packet_type == Packet.WITHDRAW: self.experiment_actual_state = new_state.copy() new_state = self.actual_state.copy() new_state.remove( self.routes.loc[hash(route)][NodeAnalyzer.ROUTES_COLUMNS[1]]) return new_state
def __message_table(self, table: Digraph) -> Digraph: """__message_table. Generates the message table :param table: table object where to define the nodes :returns: table graphviz object modified """ res = r'{{Messages Table}|{id|addr|nh|path|policy_value}' for _id, addr, next_hop, path, policy_value in \ zip(self.node.routes[NodeAnalyzer.ROUTES_COLUMNS[1]], self.node.routes[NodeAnalyzer.ROUTES_COLUMNS[2]], self.node.routes[NodeAnalyzer.ROUTES_COLUMNS[3]], self.node.routes[NodeAnalyzer.ROUTES_COLUMNS[4]], self.node.routes[NodeAnalyzer.ROUTES_COLUMNS[5]],): network = ipaddress.ip_network(addr) path = ast.literal_eval(path) policy_value = PolicyValue(int(policy_value)) route = Route(network, path, next_hop, policy_value=policy_value) res += Plotter.__route_to_table_content(_id, route) res += '}' table.node('route_table', res) return table
def __init__(self, addr, path, nh, mine=False, policy_value=PolicyValue(0)): # pylint: disable=too-many-arguments """__init__. :param addr: addr of the route :param path: Path to reach the destination :param nh: Nh for the address :param policy_value: Policy value to associate with the route """ self._addr = addr # Check that the path is an instance of a list if not isinstance(path, list): raise TypeError(path) self._path = path.copy() self._nh = nh # Check that the policy value is an object of class PolicyValue if not isinstance(policy_value, PolicyValue): raise TypeError(policy_value) self._mine = mine self._policy_value = policy_value # RFD parameters self._figure_of_merit = 0 self._last_time_updated = None self._usable = True self._reachable = True self._reusable_event = None self._flaps = 0 self._first_suppressed_time = None self._t_hold_event = None
def test_policy_value_copy(self, value): pl = PolicyValue(value) test = copy.copy(pl) assert not id(pl) == id(test) assert id(pl.value) == id(test.value)
def test_policy_value_str(self, value, str_expected): pl = PolicyValue(value) assert str(pl) == str_expected
def test_policy_value_init(self, value): pl = PolicyValue(value) assert id(pl) is not None
def test_route_init(self, addr, path, nh, policy_value): ipaddr = ipaddress.ip_network(addr) pl = PolicyValue(policy_value) route = Route(ipaddr, path, nh, policy_value=pl) assert route.addr is ipaddr
def test_mine(self, addr, path, nh, policy_value, mine): ipaddr = ipaddress.ip_network(addr) pv = PolicyValue(policy_value) route = Route(ipaddr, path, nh, policy_value=pv, mine=mine) assert route.mine == mine
def test_policy_value_init_valueerror(self, value): with pytest.raises(ValueError): pl = PolicyValue(value)
def test_policyvalue(self, addr, path, nh, policy_value): ipaddr = ipaddress.ip_network(addr) pl = PolicyValue(policy_value) route = Route(ipaddr, path, nh, policy_value=pl) assert route.policy_value.value == pl.value
def get_fsm_graphviz(self, dot: Digraph) -> Digraph: # pylint: disable=too-many-locals """get_fsm_graphviz. :param dot: dot object of graphviz used to create the graph :returns: the dot object modified """ # Insert all states like nodes for state_hash, state_str in zip( self.node.states.index.tolist(), self.node.states[NodeAnalyzer.STATES_COLUMNS[1]]): state = ast.literal_eval( state_str) if state_str != "set()" else set() if len(state) == 0: dot.node(str(state_hash), label="{}") else: # Find the best known route and put it in bold in the graph best_id = state.pop() res = "<{" + str(best_id) best_route_row = self.node.routes[self.node.routes.value == best_id] network = ipaddress.ip_network(best_route_row.addr.values[0]) path = ast.literal_eval(best_route_row.path.values[0]) policy_value = PolicyValue( int(best_route_row.policy_value.values[0])) best_route = Route(network, path, best_route_row.nh.values[0], policy_value=policy_value) while len(state) > 0: new_elem = state.pop() new_route_row = self.node.routes[self.node.routes.value == new_elem] network = ipaddress.ip_network( new_route_row.addr.values[0]) path = ast.literal_eval(new_route_row.path.values[0]) policy_value = PolicyValue( int(new_route_row.policy_value.values[0])) new_route = Route(network, path, new_route_row.nh.values[0], policy_value=policy_value) if new_route < best_route: best_id = new_elem best_route = new_route res += ", " + str(new_elem) res += "}>" regex = "\\b" + str(best_id) + "\\b" res = re.sub(regex, "<B>" + str(best_id) + "</B>", res) dot.node(str(state_hash), label=res) # Insert every transition like edge for input_state, output_state, cause, response in \ zip(self.node.transitions[NodeAnalyzer.TRANSITIONS_COLUMNS[1]], self.node.transitions[NodeAnalyzer.TRANSITIONS_COLUMNS[2]], self.node.transitions[NodeAnalyzer.TRANSITIONS_COLUMNS[3]], self.node.transitions[NodeAnalyzer.TRANSITIONS_COLUMNS[4]],): # If the output of the transition is empty (No messages sent) # use an empty string to represent it trans_output = "" if response is not None: trans_output = response # Insert the edge dot.edge(str(input_state), str(output_state), label=" {}:{} ".format(cause, trans_output)) return dot
def test_policy_value_fromStr(self, value): pl = PolicyValue.fromString(value) assert id(pl) is not None assert isinstance(pl, PolicyValue) assert pl.value == int(value) if value != "inf" else math.inf
def test_policy_value_value_getset(self, value): pl = PolicyValue(value) assert id(pl) is not None assert pl.value == value pl.value = value + 10 assert pl.value == value + 10
def test_policy_value_getset_valueerror(self, value): with pytest.raises(ValueError): pl = PolicyValue(0) pl.value = value
def test_policy_value_ne(self, value, test): pl = PolicyValue(test) test_pl = PolicyValue(value) assert pl != test_pl
def test_policy_value_gt(self, value, test): pl = PolicyValue(test) test_pl = PolicyValue(value) assert pl > test_pl
def test_policy_value_lt(self, value, test): pl = PolicyValue(value) test_pl = PolicyValue(test) assert pl < test_pl