VOM reshuffle
[vpp.git] / src / vpp-api / vom / l3_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_L3_BINDING_H__
17 #define __VOM_L3_BINDING_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/singular_db.hpp"
25
26 namespace VOM {
27 /**
28  * A representation of L3 configuration on an interface
29  */
30 class l3_binding : public object_base
31 {
32 public:
33   /**
34    * Construct a new object matching the desried state
35    */
36   l3_binding(const interface& itf, const route::prefix_t& pfx);
37
38   /**
39    * Copy Constructor
40    */
41   l3_binding(const l3_binding& o);
42
43   /**
44    * Destructor
45    */
46   ~l3_binding();
47
48   /**
49    * Return the 'singular instance' of the L3-Config that matches this
50    * object
51    */
52   std::shared_ptr<l3_binding> singular() const;
53
54   /**
55    * convert to string format for debug purposes
56    */
57   std::string to_string() const;
58
59   /**
60    * Return the prefix associated with this L3config
61    */
62   const route::prefix_t& prefix() const;
63
64   /**
65    * Dump all l3_bindings into the stream provided
66    */
67   static void dump(std::ostream& os);
68
69   /**
70    * The key type for l3_bindings
71    */
72   typedef std::pair<interface::key_type, route::prefix_t> key_type_t;
73
74   /**
75    * Find an singular instance in the DB for the interface passed
76    */
77   static std::deque<std::shared_ptr<l3_binding>> find(const interface& i);
78
79 private:
80   /**
81    * Class definition for listeners to OM events
82    */
83   class event_handler : public OM::listener, public inspect::command_handler
84   {
85   public:
86     event_handler();
87     virtual ~event_handler() = default;
88
89     /**
90      * Handle a populate event
91      */
92     void handle_populate(const client_db::key_t& key);
93
94     /**
95      * Handle a replay event
96      */
97     void handle_replay();
98
99     /**
100      * Show the object in the Singular DB
101      */
102     void show(std::ostream& os);
103
104     /**
105      * Get the sortable Id of the listener
106      */
107     dependency_t order() const;
108   };
109
110   /**
111    * event_handler to register with OM
112    */
113   static event_handler m_evh;
114
115   /**
116    * Enquue commonds to the VPP command Q for the update
117    */
118   void update(const l3_binding& obj);
119
120   /**
121    * Find or add the singular instance in the DB
122    */
123   static std::shared_ptr<l3_binding> find_or_add(const l3_binding& temp);
124
125   /*
126    * It's the VPPHW class that updates the objects in HW
127    */
128   friend class OM;
129
130   /**
131      e* It's the singular_db class that calls replay()
132   */
133   friend class singular_db<key_type_t, l3_binding>;
134
135   /**
136    * Sweep/reap the object if still stale
137    */
138   void sweep(void);
139
140   /**
141    * replay the object to create it in hardware
142    */
143   void replay(void);
144
145   /**
146    * A reference counting pointer the interface that this L3 layer
147    * represents. By holding the reference here, we can guarantee that
148    * this object will outlive the interface
149    */
150   const std::shared_ptr<interface> m_itf;
151
152   /**
153    * The prefix for this L3 configuration
154    */
155   const route::prefix_t& m_pfx;
156
157   /**
158    * HW configuration for the binding. The bool representing the
159    * do/don't bind.
160    */
161   HW::item<bool> m_binding;
162
163   /**
164    * A map of all L3 configs keyed against a combination of the interface
165    * and subnet's keys.
166    */
167   static singular_db<key_type_t, l3_binding> m_db;
168 };
169
170 /**
171  * Ostream output for the key
172  */
173 std::ostream& operator<<(std::ostream& os, const l3_binding::key_type_t& key);
174 };
175
176 /*
177  * fd.io coding-style-patch-verification: ON
178  *
179  * Local Variables:
180  * eval: (c-set-style "mozilla")
181  * End:
182  */
183
184 #endif