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