Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / bridge_domain_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_ENTRY_H__
17 #define __VOM_BRIDGE_DOMAIN_ENTRY_H__
18
19 #include "vom/bridge_domain.hpp"
20 #include "vom/interface.hpp"
21 #include "vom/singular_db.hpp"
22
23 #include <vapi/l2.api.vapi.hpp>
24
25 namespace VOM {
26 /**
27  * A MAC forwarding entry in the bridge-domain/L2-FIB
28  */
29 class bridge_domain_entry : public object_base
30 {
31 public:
32   /**
33    * The key for a bridge_domain
34    */
35   typedef std::pair<uint32_t, mac_address_t> key_t;
36
37   /**
38    * Construct a bridge_domain in the given bridge domain
39    */
40   bridge_domain_entry(const bridge_domain& bd,
41                       const mac_address_t& mac,
42                       const interface& tx_itf);
43
44   /**
45    * Construct a bridge_domain in the default table
46    */
47   bridge_domain_entry(const mac_address_t& mac, const interface& tx_itf);
48
49   /**
50    * Copy Construct
51    */
52   bridge_domain_entry(const bridge_domain_entry& r);
53
54   /**
55    * Destructor
56    */
57   ~bridge_domain_entry();
58
59   /**
60    * Return the matching 'singular instance'
61    */
62   std::shared_ptr<bridge_domain_entry> singular() const;
63
64   /**
65    * Find the instnace of the bridge_domain domain in the OM
66    */
67   static std::shared_ptr<bridge_domain_entry> find(
68     const bridge_domain_entry& temp);
69
70   /**
71    * Dump all bridge_domain-doamin into the stream provided
72    */
73   static void dump(std::ostream& os);
74
75   /**
76    * replay the object to create it in hardware
77    */
78   void replay(void);
79
80   /**
81    * Convert to string for debugging
82    */
83   std::string to_string() const;
84
85   /**
86    * A command class that creates or updates the bridge_domain
87    */
88   class create_cmd : public rpc_cmd<HW::item<bool>, rc_t, vapi::L2fib_add_del>
89   {
90   public:
91     /**
92      * Constructor
93      */
94     create_cmd(HW::item<bool>& item,
95                const mac_address_t& mac,
96                uint32_t id,
97                handle_t tx_intf);
98
99     /**
100      * Issue the command to VPP/HW
101      */
102     rc_t issue(connection& con);
103
104     /**
105      * convert to string format for debug purposes
106      */
107     std::string to_string() const;
108
109     /**
110      * Comparison operator - only used for UT
111      */
112     bool operator==(const create_cmd& i) const;
113
114   private:
115     mac_address_t m_mac;
116     uint32_t m_bd;
117     handle_t m_tx_itf;
118   };
119
120   /**
121    * A cmd class that deletes a bridge_domain
122    */
123   class delete_cmd : public rpc_cmd<HW::item<bool>, rc_t, vapi::L2fib_add_del>
124   {
125   public:
126     /**
127      * Constructor
128      */
129     delete_cmd(HW::item<bool>& item, const mac_address_t& mac, uint32_t id);
130
131     /**
132      * Issue the command to VPP/HW
133      */
134     rc_t issue(connection& con);
135
136     /**
137      * convert to string format for debug purposes
138      */
139     std::string to_string() const;
140
141     /**
142      * Comparison operator - only used for UT
143      */
144     bool operator==(const delete_cmd& i) const;
145
146   private:
147     mac_address_t m_mac;
148     uint32_t m_bd;
149   };
150
151   /**
152    * A cmd class that Dumps all the interface spans
153    */
154   class dump_cmd : public VOM::dump_cmd<vapi::L2_fib_table_dump>
155   {
156   public:
157     /**
158      * Constructor
159      */
160     dump_cmd();
161     dump_cmd(const dump_cmd& d);
162
163     /**
164      * Issue the command to VPP/HW
165      */
166     rc_t issue(connection& con);
167     /**
168      * convert to string format for debug purposes
169      */
170     std::string to_string() const;
171
172     /**
173      * Comparison operator - only used for UT
174      */
175     bool operator==(const dump_cmd& i) const;
176
177   private:
178     /**
179      * HW reutrn code
180      */
181     HW::item<bool> item;
182   };
183
184 private:
185   /**
186    * Class definition for listeners to OM events
187    */
188   class event_handler : public OM::listener, public inspect::command_handler
189   {
190   public:
191     event_handler();
192     virtual ~event_handler() = default;
193
194     /**
195      * Handle a populate event
196      */
197     void handle_populate(const client_db::key_t& key);
198
199     /**
200      * Handle a replay event
201      */
202     void handle_replay();
203
204     /**
205      * Show the object in the Singular DB
206      */
207     void show(std::ostream& os);
208
209     /**
210      * Get the sortable Id of the listener
211      */
212     dependency_t order() const;
213   };
214
215   /**
216    * event_handler to register with OM
217    */
218   static event_handler m_evh;
219
220   /**
221    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
222    */
223   void update(const bridge_domain_entry& obj);
224
225   /**
226    * Find or add the instnace of the bridge_domain domain in the OM
227    */
228   static std::shared_ptr<bridge_domain_entry> find_or_add(
229     const bridge_domain_entry& temp);
230
231   /*
232    * It's the VPPHW class that updates the objects in HW
233    */
234   friend class OM;
235
236   /**
237    * It's the singular_db class that calls replay()
238    */
239   friend class singular_db<key_t, bridge_domain_entry>;
240
241   /**
242    * Sweep/reap the object if still stale
243    */
244   void sweep(void);
245
246   /**
247    * HW configuration for the result of creating the bridge_domain
248    */
249   HW::item<bool> m_hw;
250
251   /**
252    * The mac to match
253    */
254   mac_address_t m_mac;
255
256   /**
257    * The bridge_domain domain the bridge_domain is in.
258    */
259   std::shared_ptr<bridge_domain> m_bd;
260
261   /**
262    * The set of paths
263    */
264   std::shared_ptr<interface> m_tx_itf;
265
266   /**
267    * A map of all bridge_domains
268    */
269   static singular_db<key_t, bridge_domain_entry> m_db;
270 };
271
272 std::ostream& operator<<(std::ostream& os,
273                          const bridge_domain_entry::key_t& key);
274 };
275
276 /*
277  * fd.io coding-style-patch-verification: ON
278  *
279  * Local Variables:
280  * eval: (c-set-style "mozilla")
281  * End:
282  */
283
284 #endif