VPP Object Model (VOM)
[vpp.git] / src / vpp-api / vom / bridge_domain_arp_entry.hpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __VOM_BRIDGE_DOMAIN_ARP_ENTRY_H__
17 #define __VOM_BRIDGE_DOMAIN_ARP_ENTRY_H__
18
19 #include "vom/bridge_domain.hpp"
20 #include "vom/interface.hpp"
21 #include "vom/singular_db.hpp"
22 #include "vom/types.hpp"
23
24 #include <vapi/l2.api.vapi.hpp>
25
26 namespace VOM {
27 /**
28  * A entry in the ARP termination table of a Bridge Domain
29  */
30 class bridge_domain_arp_entry : public object_base
31 {
32 public:
33   /**
34    * The key for a bridge_domain ARP entry;
35    *  the BD, IP address and MAC address
36    */
37   typedef std::tuple<uint32_t, mac_address_t, boost::asio::ip::address> key_t;
38
39   /**
40    * Construct a bridge_domain in the given bridge domain
41    */
42   bridge_domain_arp_entry(const bridge_domain& bd,
43                           const mac_address_t& mac,
44                           const boost::asio::ip::address& ip_addr);
45
46   /**
47    * Construct a bridge_domain in the default table
48    */
49   bridge_domain_arp_entry(const mac_address_t& mac,
50                           const boost::asio::ip::address& ip_addr);
51
52   /**
53    * Copy Construct
54    */
55   bridge_domain_arp_entry(const bridge_domain_arp_entry& r);
56
57   /**
58    * Destructor
59    */
60   ~bridge_domain_arp_entry();
61
62   /**
63    * Return the matching 'singular instance'
64    */
65   std::shared_ptr<bridge_domain_arp_entry> singular() const;
66
67   /**
68    * Find the instnace of the bridge_domain domain in the OM
69    */
70   static std::shared_ptr<bridge_domain_arp_entry> find(
71     const bridge_domain_arp_entry& temp);
72
73   /**
74    * Dump all bridge_domain-doamin into the stream provided
75    */
76   static void dump(std::ostream& os);
77
78   /**
79    * replay the object to create it in hardware
80    */
81   void replay(void);
82
83   /**
84    * Convert to string for debugging
85    */
86   std::string to_string() const;
87
88   /**
89    * A command class that creates or updates the bridge domain ARP Entry
90    */
91   class create_cmd
92     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Bd_ip_mac_add_del>
93   {
94   public:
95     /**
96      * Constructor
97      */
98     create_cmd(HW::item<bool>& item,
99                uint32_t id,
100                const mac_address_t& mac,
101                const boost::asio::ip::address& ip_addr);
102
103     /**
104      * Issue the command to VPP/HW
105      */
106     rc_t issue(connection& con);
107
108     /**
109      * convert to string format for debug purposes
110      */
111     std::string to_string() const;
112
113     /**
114      * Comparison operator - only used for UT
115      */
116     bool operator==(const create_cmd& i) const;
117
118   private:
119     uint32_t m_bd;
120     mac_address_t m_mac;
121     boost::asio::ip::address m_ip_addr;
122   };
123
124   /**
125    * A cmd class that deletes a bridge domain ARP entry
126    */
127   class delete_cmd
128     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Bd_ip_mac_add_del>
129   {
130   public:
131     /**
132      * Constructor
133      */
134     delete_cmd(HW::item<bool>& item,
135                uint32_t id,
136                const mac_address_t& mac,
137                const boost::asio::ip::address& ip_addr);
138
139     /**
140      * Issue the command to VPP/HW
141      */
142     rc_t issue(connection& con);
143
144     /**
145      * convert to string format for debug purposes
146      */
147     std::string to_string() const;
148
149     /**
150      * Comparison operator - only used for UT
151      */
152     bool operator==(const delete_cmd& i) const;
153
154   private:
155     uint32_t m_bd;
156     mac_address_t m_mac;
157     boost::asio::ip::address m_ip_addr;
158   };
159
160 private:
161   /**
162    * Class definition for listeners to OM events
163    */
164   class event_handler : public OM::listener, public inspect::command_handler
165   {
166   public:
167     event_handler();
168     virtual ~event_handler() = default;
169
170     /**
171      * Handle a populate event
172      */
173     void handle_populate(const client_db::key_t& key);
174
175     /**
176      * Handle a replay event
177      */
178     void handle_replay();
179
180     /**
181      * Show the object in the Singular DB
182      */
183     void show(std::ostream& os);
184
185     /**
186      * Get the sortable Id of the listener
187      */
188     dependency_t order() const;
189   };
190
191   /**
192    * event_handler to register with OM
193    */
194   static event_handler m_evh;
195
196   /**
197    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
198    */
199   void update(const bridge_domain_arp_entry& obj);
200
201   /**
202    * Find or add the instnace of the bridge_domain domain in the OM
203    */
204   static std::shared_ptr<bridge_domain_arp_entry> find_or_add(
205     const bridge_domain_arp_entry& temp);
206
207   /*
208    * It's the VPPHW class that updates the objects in HW
209    */
210   friend class OM;
211
212   /**
213    * It's the singular_db class that calls replay()
214    */
215   friend class singular_db<key_t, bridge_domain_arp_entry>;
216
217   /**
218    * Sweep/reap the object if still stale
219    */
220   void sweep(void);
221
222   /**
223    * HW configuration for the result of creating the bridge_domain
224    */
225   HW::item<bool> m_hw;
226
227   /**
228    * The bridge_domain domain the bridge_domain is in.
229    */
230   std::shared_ptr<bridge_domain> m_bd;
231
232   /**
233    * The mac to match
234    */
235   mac_address_t m_mac;
236
237   /**
238    * The IP address
239    */
240   boost::asio::ip::address m_ip_addr;
241
242   /**
243    * A map of all bridge_domains
244    */
245   static singular_db<key_t, bridge_domain_arp_entry> m_db;
246 };
247
248 std::ostream& operator<<(std::ostream& os,
249                          const bridge_domain_arp_entry::key_t& key);
250 };
251
252 /*
253  * fd.io coding-style-patch-verification: ON
254  *
255  * Local Variables:
256  * eval: (c-set-style "mozilla")
257  * End:
258  */
259
260 #endif