Move VOM to extras/vom
[vpp.git] / extras / vom / vom / gbp_subnet.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_GBP_SUBNET_H__
17 #define __VOM_GBP_SUBNET_H__
18
19 #include "vom/gbp_endpoint_group.hpp"
20 #include "vom/gbp_recirc.hpp"
21 #include "vom/route.hpp"
22 #include "vom/singular_db.hpp"
23
24 namespace VOM {
25 /**
26  * A GBP Enpoint (i.e. a VM)
27  */
28 class gbp_subnet : public object_base
29 {
30 public:
31   /**
32    * The key for a GBP subnet; table and prefix
33    */
34   typedef std::pair<route_domain::key_t, route::prefix_t> key_t;
35
36   /**
37     * Construct an internal GBP subnet
38     */
39   gbp_subnet(const route_domain& rd, const route::prefix_t& prefix);
40
41   /**
42    * Construct an external GBP subnet
43    */
44   gbp_subnet(const route_domain& rd,
45              const route::prefix_t& prefix,
46              const gbp_recirc& recirc,
47              const gbp_endpoint_group& epg);
48
49   /**
50    * Copy Construct
51    */
52   gbp_subnet(const gbp_subnet& r);
53
54   /**
55    * Destructor
56    */
57   ~gbp_subnet();
58
59   /**
60    * Return the object's key
61    */
62   const key_t key() const;
63
64   /**
65    * comparison operator
66    */
67   bool operator==(const gbp_subnet& bdae) const;
68
69   /**
70    * Return the matching 'singular instance'
71    */
72   std::shared_ptr<gbp_subnet> singular() const;
73
74   /**
75    * Find the instnace of the bridge_domain domain in the OM
76    */
77   static std::shared_ptr<gbp_subnet> find(const key_t& k);
78
79   /**
80    * Dump all bridge_domain-doamin into the stream provided
81    */
82   static void dump(std::ostream& os);
83
84   /**
85    * replay the object to create it in hardware
86    */
87   void replay(void);
88
89   /**
90    * Convert to string for debugging
91    */
92   std::string to_string() const;
93
94 private:
95   struct type_t : public enum_base<type_t>
96   {
97     /**
98      * Internal subnet is reachable through the source EPG's
99      * uplink interface.
100      */
101     const static type_t INTERNAL;
102
103     /**
104      * External subnet requires NAT translation before egress.
105      */
106     const static type_t EXTERNAL;
107
108   private:
109     type_t(int v, const std::string s);
110   };
111
112   /**
113    * Class definition for listeners to OM events
114    */
115   class event_handler : public OM::listener, public inspect::command_handler
116   {
117   public:
118     event_handler();
119     virtual ~event_handler() = default;
120
121     /**
122      * Handle a populate event
123      */
124     void handle_populate(const client_db::key_t& key);
125
126     /**
127      * Handle a replay event
128      */
129     void handle_replay();
130
131     /**
132      * Show the object in the Singular DB
133      */
134     void show(std::ostream& os);
135
136     /**
137      * Get the sortable Id of the listener
138      */
139     dependency_t order() const;
140   };
141
142   /**
143    * event_handler to register with OM
144    */
145   static event_handler m_evh;
146
147   /**
148    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
149    */
150   void update(const gbp_subnet& obj);
151
152   /**
153    * Find or add the instnace of the bridge_domain domain in the OM
154    */
155   static std::shared_ptr<gbp_subnet> find_or_add(const gbp_subnet& temp);
156
157   /*
158    * It's the VPPHW class that updates the objects in HW
159    */
160   friend class OM;
161
162   /**
163    * It's the singular_db class that calls replay()
164    */
165   friend class singular_db<key_t, gbp_subnet>;
166
167   /**
168    * Sweep/reap the object if still stale
169    */
170   void sweep(void);
171
172   /**
173    * HW configuration for the result of creating the subnet
174    */
175   HW::item<bool> m_hw;
176
177   /**
178    * the route domain the prefix is in
179    */
180   const std::shared_ptr<route_domain> m_rd;
181
182   /**
183    * prefix to match
184    */
185   const route::prefix_t m_prefix;
186
187   /*
188    * Subnet type
189    */
190   type_t m_type;
191
192   /**
193    * The interface the prefix is reachable through
194    */
195   std::shared_ptr<gbp_recirc> m_recirc;
196
197   /**
198    * The EPG the subnet is in
199    */
200   std::shared_ptr<gbp_endpoint_group> m_epg;
201
202   /**
203    * A map of all bridge_domains
204    */
205   static singular_db<key_t, gbp_subnet> m_db;
206 };
207
208 }; // namespace
209
210 /*
211  * fd.io coding-style-patch-verification: ON
212  *
213  * Local Variables:
214  * eval: (c-set-style "mozilla")
215  * End:
216  */
217
218 #endif