tests: refactor vpp_object.py 78/18578/3
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Thu, 28 Mar 2019 16:53:44 +0000 (09:53 -0700)
committerNeale Ranns <nranns@cisco.com>
Fri, 29 Mar 2019 16:02:27 +0000 (16:02 +0000)
Move __str__ to super for all subclasses of VppObject
Implement __repr__ in VppObject
Implement __hash__ and __eq__

Change-Id: Ibd4ea37b84b17f499ab86630fb5b9ed9c8b4b1c2
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
18 files changed:
test/bfd.py
test/test_abf.py
test/test_gbp.py
test/test_pipe.py
test/vpp_bier.py
test/vpp_gre_interface.py
test/vpp_igmp.py
test/vpp_ip_route.py
test/vpp_ipsec.py
test/vpp_ipsec_tun_interface.py
test/vpp_l2.py
test/vpp_memif.py
test/vpp_mpls_tunnel_interface.py
test/vpp_neighbor.py
test/vpp_object.py
test/vpp_srv6.py
test/vpp_udp_encap.py
test/vpp_vxlan_gbp_tunnel.py

index d99bbf6..84ef7b0 100644 (file)
@@ -222,9 +222,6 @@ class VppBFDAuthKey(VppObject):
     def object_id(self):
         return "bfd-auth-key-%s" % self._conf_key_id
 
-    def __str__(self):
-        return self.object_id()
-
 
 class VppBFDUDPSession(VppObject):
     """ Represents BFD UDP session in VPP """
@@ -430,9 +427,6 @@ class VppBFDUDPSession(VppObject):
                                         self.peer_addr,
                                         self.af)
 
-    def __str__(self):
-        return self.object_id()
-
     def admin_up(self):
         """ set bfd session admin-up """
         is_ipv6 = 1 if AF_INET6 == self._af else 0
index ecd8c53..e2f9745 100644 (file)
@@ -88,9 +88,6 @@ class VppAbfPolicy(VppObject):
     def query_vpp_config(self):
         return find_abf_policy(self._test, self.policy_id)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("abf-policy-%d" % self.policy_id)
 
@@ -131,9 +128,6 @@ class VppAbfAttach(VppObject):
                                    self.policy_id,
                                    self.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("abf-attach-%d-%d" % (self.policy_id, self.sw_if_index))
 
index 74eb424..c5f5192 100644 (file)
@@ -131,9 +131,6 @@ class VppGbpEndpoint(VppObject):
     def remove_vpp_config(self):
         self._test.vapi.gbp_endpoint_del(self.handle)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-endpoint:[%d==%d:%s:%d]" % (self.handle,
                                                 self.itf.sw_if_index,
@@ -172,9 +169,6 @@ class VppGbpRecirc(VppObject):
             self.epg.sclass,
             self.is_ext)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-recirc:[%d]" % (self.recirc.sw_if_index)
 
@@ -212,9 +206,6 @@ class VppGbpExtItf(VppObject):
             self.bd.bd_id,
             self.rd.rd_id)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-ext-itf:[%d]" % (self.itf.sw_if_index)
 
@@ -256,9 +247,6 @@ class VppGbpSubnet(VppObject):
             self.prefix.encode(),
             self.type)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-subnet:[%d-%s]" % (self.rd_id, self.prefix)
 
@@ -314,9 +302,6 @@ class VppGbpEndpointGroup(VppObject):
     def remove_vpp_config(self):
         self._test.vapi.gbp_endpoint_group_del(self.sclass)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-endpoint-group:[%d]" % (self.vnid)
 
@@ -359,9 +344,6 @@ class VppGbpBridgeDomain(VppObject):
     def remove_vpp_config(self):
         self._test.vapi.gbp_bridge_domain_del(self.bd.bd_id)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-bridge-domain:[%d]" % (self.bd.bd_id)
 
@@ -398,9 +380,6 @@ class VppGbpRouteDomain(VppObject):
     def remove_vpp_config(self):
         self._test.vapi.gbp_route_domain_del(self.rd_id)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-route-domain:[%d]" % (self.rd_id)
 
@@ -483,9 +462,6 @@ class VppGbpContract(VppObject):
             [],
             self.allowed_ethertypes)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-contract:[%d:%s:%d]" % (self.sclass,
                                             self.dclass,
@@ -525,9 +501,6 @@ class VppGbpVxlanTunnel(VppInterface):
     def remove_vpp_config(self):
         self._test.vapi.gbp_vxlan_tunnel_del(self.vni)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-vxlan:%d" % (self.sw_if_index)
 
@@ -576,9 +549,6 @@ class VppGbpAcl(VppObject):
     def remove_vpp_config(self):
         self._test.vapi.acl_del(self.acl_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gbp-acl:[%d]" % (self.acl_index)
 
index d2f21ef..2c23bd0 100644 (file)
@@ -38,9 +38,6 @@ class VppPipe(VppInterface):
         self._test.vapi.pipe_delete(
             self.result.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "pipe-%d" % (self._sw_if_index)
 
index f389ca1..8e27f25 100644 (file)
@@ -97,9 +97,6 @@ class VppBierTable(VppObject):
             self.mpls_label,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bier-table;[%d:%d:%d]" % (self.id.set_id,
                                           self.id.sub_domain_id,
@@ -198,9 +195,6 @@ class VppBierRoute(VppObject):
             is_replace=1)
         self.paths = []
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bier-route;[%d:%d:%d:%d]" % (self.tbl_id.set_id,
                                              self.tbl_id.sub_domain_id,
@@ -234,9 +228,6 @@ class VppBierImp(VppObject):
         self._test.vapi.bier_imp_del(
             self.bi_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bier-imp;[%d:%d:%d:%d]" % (self.tbl_id.set_id,
                                            self.tbl_id.sub_domain_id,
@@ -267,9 +258,6 @@ class VppBierDispTable(VppObject):
             self.id,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bier-disp-table;[%d]" % (self.id)
 
@@ -316,9 +304,6 @@ class VppBierDispEntry(VppObject):
             self.rpf_id,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bier-disp-entry;[%d:%d]" % (self.tbl_id,
                                             self.bp)
index d6f4202..46dce36 100644 (file)
@@ -39,9 +39,6 @@ class VppGreInterface(VppInterface):
                                           session_id=self.t_session,
                                           is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gre-%d" % self.sw_if_index
 
@@ -84,8 +81,5 @@ class VppGre6Interface(VppInterface):
                                           is_add=0,
                                           is_ip6=1)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gre-%d" % self._sw_if_index
index 2403fbf..8f78a9b 100644 (file)
@@ -68,9 +68,6 @@ class VppHostState(VppObject):
             [],
             self.sg.gaddr_p)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "%s:%d" % (self.sg, self.sw_if_index)
 
index b7ceb34..0bd6dd3 100644 (file)
@@ -143,9 +143,6 @@ class VppIpTable(VppObject):
                           self.table_id,
                           inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("table-%s-%d" %
                 ("v6" if self.is_ip6 == 1 else "v4",
@@ -178,9 +175,6 @@ class VppIpInterfaceAddress(VppObject):
                                        self.prefix.length,
                                        self.intf.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "interface-ip-%s-%s" % (self.intf, self.prefix)
 
@@ -214,9 +208,6 @@ class VppIpInterfaceBind(VppObject):
             self.intf.sw_if_index,
             self.table.is_ip6).vrf_id == self.table.table_id
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "interface-bind-%s-%s" % (self.intf, self.table)
 
@@ -453,9 +444,6 @@ class VppIpRoute(VppObject):
                           self.table_id,
                           inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d:%s/%d"
                 % (self.table_id,
@@ -579,9 +567,6 @@ class VppIpMRoute(VppObject):
                            self.table_id,
                            inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         if self.is_ip6:
             return ("%d:(%s,%s/%d)"
@@ -667,9 +652,6 @@ class VppMplsIpBind(VppObject):
                 return True
         return False
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d:%s binds %d:%s/%d"
                 % (self.table_id,
@@ -705,9 +687,6 @@ class VppMplsTable(VppObject):
             return True
         return False
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("table-mpls-%d" % (self.table_id))
 
@@ -766,9 +745,6 @@ class VppMplsRoute(VppObject):
         return find_mpls_route(self._test, self.table_id,
                                self.local_label, self.eos_bit)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d:%s/%d"
                 % (self.table_id,
index 5866a0b..278ff36 100644 (file)
@@ -24,9 +24,6 @@ class VppIpsecSpd(VppObject):
     def remove_vpp_config(self):
         self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "ipsec-spd-%d" % self.id
 
@@ -59,9 +56,6 @@ class VppIpsecSpdItfBinding(VppObject):
                                                    self.itf.sw_if_index,
                                                    is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bind-%s-to-%s" % (self.spd.id, self.itf)
 
@@ -149,9 +143,6 @@ class VppIpsecSpdEntry(VppObject):
             remote_port_stop=self.remote_port_stop,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
                                                 self.priority,
@@ -241,9 +232,6 @@ class VppIpsecSA(VppObject):
             flags=self.flags,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "ipsec-sa-%d" % self.id
 
index 5c014ea..c427936 100644 (file)
@@ -43,8 +43,5 @@ class VppIpsecTunInterface(VppTunnelInterface):
             self.integ_alg, self.local_integ_key, self.remote_integ_key,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "ipsec-tun-if-%d" % self._sw_if_index
index dc73e42..b606ff0 100644 (file)
@@ -93,9 +93,6 @@ class VppBridgeDomain(VppObject):
     def query_vpp_config(self):
         return find_bridge_domain(self._test, self.bd_id)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "bridge-domain-%d" % (self.bd_id)
 
@@ -125,9 +122,6 @@ class VppBridgeDomainPort(VppObject):
                                        self.bd.bd_id,
                                        self.itf.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "BD-Port-%s-%s" % (self.bd, self.itf)
 
@@ -157,9 +151,6 @@ class VppBridgeDomainArpEntry(VppObject):
                                             self.mac.packed,
                                             self.ip.address)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
 
@@ -203,9 +194,6 @@ class VppL2FibEntry(VppObject):
                                  self.mac.packed,
                                  self.itf.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
 
@@ -232,8 +220,5 @@ class VppL2Vtr(VppObject):
             return (d.vtr_op == self.op)
         return False
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "L2-vtr-%s-%d" % (str(self.itf), self.op)
index 4ad9ffd..7836dc1 100644 (file)
@@ -64,9 +64,6 @@ class VppSocketFilename(VppObject):
     def query_vpp_config(self):
         return self._test.vapi.memif_socket_filename_dump()
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "%d" % (self.socket_id)
 
@@ -135,9 +132,6 @@ class VppMemif(VppObject):
         dump = self._test.vapi.memif_dump()
         return get_if_dump(dump, self.sw_if_index)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         if self.sw_if_index:
             return "%d:%d:%d" % (self.role, self.if_id, self.sw_if_index)
index a2ab1bf..12f62fb 100644 (file)
@@ -60,9 +60,6 @@ class VppMPLSTunnelInterface(VppInterface):
                 return True
         return False
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("mpls-tunnel%d-%d" % (self.tunnel_index,
                                       self.sw_if_index))
index 95bcc0d..eb9635f 100644 (file)
@@ -77,9 +77,6 @@ class VppNeighbor(VppObject):
                         self.nbr_addr,
                         self.is_static())
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d:%s" % (self.sw_if_index, self.nbr_addr))
 
index a0b9fe3..2c07d47 100644 (file)
@@ -27,10 +27,29 @@ class VppObject(object):
         """ Remove the configuration for this object from vpp. """
         pass
 
-    @abc.abstractmethod
     def object_id(self):
         """ Return a unique string representing this object. """
-        pass
+        return "Undefined. for <%s %s>" % (self.__class__.__name__, id(self))
+
+    def __str__(self):
+        return self.object_id()
+
+    def __repr__(self):
+        return '<%s>' % self.object_id()
+
+    def __hash__(self):
+        return hash(self.object_id())
+
+    def __eq__(self, other):
+        if not isinstance(other, self.__class__):
+            return NotImplemented
+        if other.object_id() == self.object_id():
+            return True
+        return False
+
+    # This can be removed when python2 support is dropped.
+    def __ne__(self, other):
+        return not self.__eq__(other)
 
 
 class VppObjectRegistry(object):
@@ -84,6 +103,6 @@ class VppObjectRegistry(object):
         if failed:
             logger.error("REG: Couldn't remove configuration for object(s):")
             for obj in failed:
-                logger.error(moves.reprlib.repr(obj))
+                logger.error(repr(obj))
             raise Exception("Couldn't remove configuration for object(s): %s" %
                             (", ".join(str(x) for x in failed)))
index b84eea3..b6dbc01 100644 (file)
@@ -86,9 +86,6 @@ class VppSRv6LocalSID(VppObject):
         # use _configured flag for now
         return self._configured
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d;%s,%d"
                 % (self.fib_table,
@@ -144,9 +141,6 @@ class VppSRv6Policy(VppObject):
         # use _configured flag for now
         return self._configured
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d;%s-><%s>;%d"
                 % (self.sr_type,
@@ -219,9 +213,6 @@ class VppSRv6Steering(VppObject):
         # use _configured flag for now
         return self._configured
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("%d;%d;%s/%d->%s"
                 % (self.table_id,
index 7534912..bdca9a8 100644 (file)
@@ -54,9 +54,6 @@ class VppUdpEncap(VppObject):
     def query_vpp_config(self):
         return find_udp_encap(self._test, self)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return ("udp-encap-%d" % self.id)
 
index efeb4f9..47817ed 100644 (file)
@@ -68,9 +68,6 @@ class VppVxlanGbpTunnel(VppInterface):
                                                        self.dst,
                                                        self.vni))
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "vxlan-gbp-%d-%d-%s-%s" % (self.sw_if_index, self.vni,
                                           self.src, self.dst)