0d316476a8f9f2a526a187892e063b4ea508452e
[vpp.git] / src / vpp-api / vom / arp_proxy_binding.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_ARP_PROXY_BINDING_H__
17 #define __VOM_ARP_PROXY_BINDING_H__
18
19 #include "vom/arp_proxy_config.hpp"
20 #include "vom/dump_cmd.hpp"
21 #include "vom/hw.hpp"
22 #include "vom/inspect.hpp"
23 #include "vom/interface.hpp"
24 #include "vom/object_base.hpp"
25 #include "vom/om.hpp"
26 #include "vom/rpc_cmd.hpp"
27 #include "vom/singular_db.hpp"
28
29 #include <vapi/vpe.api.vapi.hpp>
30
31 namespace VOM {
32 /**
33  * A representation of LLDP client configuration on an interface
34  */
35 class arp_proxy_binding : public object_base
36 {
37 public:
38   /**
39    * Construct a new object matching the desried state
40    */
41   arp_proxy_binding(const interface& itf, const arp_proxy_config& proxy_cfg);
42
43   /**
44    * Copy Constructor
45    */
46   arp_proxy_binding(const arp_proxy_binding& o);
47
48   /**
49    * Destructor
50    */
51   ~arp_proxy_binding();
52
53   /**
54    * Return the 'singular' of the LLDP binding that matches this object
55    */
56   std::shared_ptr<arp_proxy_binding> singular() const;
57
58   /**
59    * convert to string format for debug purposes
60    */
61   std::string to_string() const;
62
63   /**
64    * Dump all LLDP bindings into the stream provided
65    */
66   static void dump(std::ostream& os);
67
68   /**
69    * A command class that binds the LLDP config to the interface
70    */
71   class bind_cmd
72     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Proxy_arp_intfc_enable_disable>
73   {
74   public:
75     /**
76      * Constructor
77      */
78     bind_cmd(HW::item<bool>& item, const handle_t& itf);
79
80     /**
81      * Issue the command to VPP/HW
82      */
83     rc_t issue(connection& con);
84     /**
85      * convert to string format for debug purposes
86      */
87     std::string to_string() const;
88
89     /**
90      * Comparison operator - only used for UT
91      */
92     bool operator==(const bind_cmd& i) const;
93
94   private:
95     /**
96      * Reference to the HW::item of the interface to bind
97      */
98     const handle_t& m_itf;
99   };
100
101   /**
102    * A cmd class that Unbinds ArpProxy Config from an interface
103    */
104   class unbind_cmd
105     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Proxy_arp_intfc_enable_disable>
106   {
107   public:
108     /**
109      * Constructor
110      */
111     unbind_cmd(HW::item<bool>& item, const handle_t& itf);
112
113     /**
114      * Issue the command to VPP/HW
115      */
116     rc_t issue(connection& con);
117     /**
118      * convert to string format for debug purposes
119      */
120     std::string to_string() const;
121
122     /**
123      * Comparison operator - only used for UT
124      */
125     bool operator==(const unbind_cmd& i) const;
126
127   private:
128     /**
129      * Reference to the HW::item of the interface to unbind
130      */
131     const handle_t& m_itf;
132   };
133
134 private:
135   /**
136    * Class definition for listeners to OM events
137    */
138   class event_handler : public OM::listener, public inspect::command_handler
139   {
140   public:
141     event_handler();
142     virtual ~event_handler() = default;
143
144     /**
145      * Handle a populate event
146      */
147     void handle_populate(const client_db::key_t& key);
148
149     /**
150      * Handle a replay event
151      */
152     void handle_replay();
153
154     /**
155      * Show the object in the Singular DB
156      */
157     void show(std::ostream& os);
158
159     /**
160      * Get the sortable Id of the listener
161      */
162     dependency_t order() const;
163   };
164
165   /**
166    * event_handler to register with OM
167    */
168   static event_handler m_evh;
169
170   /**
171    * Enquue commonds to the VPP command Q for the update
172    */
173   void update(const arp_proxy_binding& obj);
174
175   /**
176    * Find or add LLDP binding to the OM
177    */
178   static std::shared_ptr<arp_proxy_binding> find_or_add(
179     const arp_proxy_binding& temp);
180
181   /*
182    * It's the OM class that calls singular()
183    */
184   friend class OM;
185
186   /**
187    * It's the singular_db class that calls replay()
188    */
189   friend class singular_db<interface::key_type, arp_proxy_binding>;
190
191   /**
192    * Sweep/reap the object if still stale
193    */
194   void sweep(void);
195
196   /**
197    * replay the object to create it in hardware
198    */
199   void replay(void);
200
201   /**
202    * A reference counting pointer to the interface on which LLDP config
203    * resides. By holding the reference here, we can guarantee that
204    * this object will outlive the interface
205    */
206   const std::shared_ptr<interface> m_itf;
207
208   /**
209    * A reference counting pointer to the prxy config.
210    */
211   const std::shared_ptr<arp_proxy_config> m_arp_proxy_cfg;
212
213   /**
214    * HW configuration for the binding. The bool representing the
215    * do/don't bind.
216    */
217   HW::item<bool> m_binding;
218
219   /**
220    * A map of all ArpProxy bindings keyed against the interface.
221    */
222   static singular_db<interface::key_type, arp_proxy_binding> m_db;
223 };
224 };
225
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "mozilla")
231  * End:
232  */
233
234 #endif