tests: refactor vpp_object.py

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>
diff --git a/test/bfd.py b/test/bfd.py
index d99bbf6..84ef7b0 100644
--- a/test/bfd.py
+++ b/test/bfd.py
@@ -222,9 +222,6 @@
     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 @@
                                         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
diff --git a/test/test_abf.py b/test/test_abf.py
index ecd8c53..e2f9745 100644
--- a/test/test_abf.py
+++ b/test/test_abf.py
@@ -88,9 +88,6 @@
     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 @@
                                    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))
 
diff --git a/test/test_gbp.py b/test/test_gbp.py
index 74eb424..c5f5192 100644
--- a/test/test_gbp.py
+++ b/test/test_gbp.py
@@ -131,9 +131,6 @@
     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 @@
             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 @@
             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 @@
             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 @@
     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 @@
     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 @@
     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 @@
             [],
             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 @@
     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 @@
     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)
 
diff --git a/test/test_pipe.py b/test/test_pipe.py
index d2f21ef..2c23bd0 100644
--- a/test/test_pipe.py
+++ b/test/test_pipe.py
@@ -38,9 +38,6 @@
         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)
 
diff --git a/test/vpp_bier.py b/test/vpp_bier.py
index f389ca1..8e27f25 100644
--- a/test/vpp_bier.py
+++ b/test/vpp_bier.py
@@ -97,9 +97,6 @@
             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 @@
             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 @@
         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 @@
             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 @@
             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)
diff --git a/test/vpp_gre_interface.py b/test/vpp_gre_interface.py
index d6f4202..46dce36 100644
--- a/test/vpp_gre_interface.py
+++ b/test/vpp_gre_interface.py
@@ -39,9 +39,6 @@
                                           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 @@
                                           is_add=0,
                                           is_ip6=1)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "gre-%d" % self._sw_if_index
diff --git a/test/vpp_igmp.py b/test/vpp_igmp.py
index 2403fbf..8f78a9b 100644
--- a/test/vpp_igmp.py
+++ b/test/vpp_igmp.py
@@ -68,9 +68,6 @@
             [],
             self.sg.gaddr_p)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "%s:%d" % (self.sg, self.sw_if_index)
 
diff --git a/test/vpp_ip_route.py b/test/vpp_ip_route.py
index b7ceb34..0bd6dd3 100644
--- a/test/vpp_ip_route.py
+++ b/test/vpp_ip_route.py
@@ -143,9 +143,6 @@
                           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 @@
                                        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 @@
             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 @@
                           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 @@
                            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 @@
                 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 @@
             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 @@
         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,
diff --git a/test/vpp_ipsec.py b/test/vpp_ipsec.py
index 5866a0b..278ff36 100644
--- a/test/vpp_ipsec.py
+++ b/test/vpp_ipsec.py
@@ -24,9 +24,6 @@
     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 @@
                                                    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 @@
             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 @@
             flags=self.flags,
             is_add=0)
 
-    def __str__(self):
-        return self.object_id()
-
     def object_id(self):
         return "ipsec-sa-%d" % self.id
 
diff --git a/test/vpp_ipsec_tun_interface.py b/test/vpp_ipsec_tun_interface.py
index 5c014ea..c427936 100644
--- a/test/vpp_ipsec_tun_interface.py
+++ b/test/vpp_ipsec_tun_interface.py
@@ -43,8 +43,5 @@
             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
diff --git a/test/vpp_l2.py b/test/vpp_l2.py
index dc73e42..b606ff0 100644
--- a/test/vpp_l2.py
+++ b/test/vpp_l2.py
@@ -93,9 +93,6 @@
     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 @@
                                        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 @@
                                             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 @@
                                  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 @@
             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)
diff --git a/test/vpp_memif.py b/test/vpp_memif.py
index 4ad9ffd..7836dc1 100644
--- a/test/vpp_memif.py
+++ b/test/vpp_memif.py
@@ -64,9 +64,6 @@
     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 @@
         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)
diff --git a/test/vpp_mpls_tunnel_interface.py b/test/vpp_mpls_tunnel_interface.py
index a2ab1bf..12f62fb 100644
--- a/test/vpp_mpls_tunnel_interface.py
+++ b/test/vpp_mpls_tunnel_interface.py
@@ -60,9 +60,6 @@
                 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))
diff --git a/test/vpp_neighbor.py b/test/vpp_neighbor.py
index 95bcc0d..eb9635f 100644
--- a/test/vpp_neighbor.py
+++ b/test/vpp_neighbor.py
@@ -77,9 +77,6 @@
                         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))
 
diff --git a/test/vpp_object.py b/test/vpp_object.py
index a0b9fe3..2c07d47 100644
--- a/test/vpp_object.py
+++ b/test/vpp_object.py
@@ -27,10 +27,29 @@
         """ 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 @@
         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)))
diff --git a/test/vpp_srv6.py b/test/vpp_srv6.py
index b84eea3..b6dbc01 100644
--- a/test/vpp_srv6.py
+++ b/test/vpp_srv6.py
@@ -86,9 +86,6 @@
         # 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 @@
         # 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 @@
         # 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,
diff --git a/test/vpp_udp_encap.py b/test/vpp_udp_encap.py
index 7534912..bdca9a8 100644
--- a/test/vpp_udp_encap.py
+++ b/test/vpp_udp_encap.py
@@ -54,9 +54,6 @@
     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)
 
diff --git a/test/vpp_vxlan_gbp_tunnel.py b/test/vpp_vxlan_gbp_tunnel.py
index efeb4f9..47817ed 100644
--- a/test/vpp_vxlan_gbp_tunnel.py
+++ b/test/vpp_vxlan_gbp_tunnel.py
@@ -68,9 +68,6 @@
                                                        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)