2221c616dbb67ced301c243b5e06f8a7ff726e54
[vpp.git] / extras / vom / vom / gbp_subnet.cpp
1 /*
2  * Copyright (c) 2018 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 #include "vom/gbp_subnet.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_subnet_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 gbp_subnet::type_t::type_t(int v, const std::string s)
24   : enum_base<gbp_subnet::type_t>(v, s)
25 {
26 }
27
28 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_INTERNAL(
29   0,
30   "stitched-internal");
31 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_EXTERNAL(
32   1,
33   "stitched-external");
34 const gbp_subnet::type_t gbp_subnet::type_t::TRANSPORT(1, "transport");
35
36 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
37
38 gbp_subnet::event_handler gbp_subnet::m_evh;
39
40 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
41                        const route::prefix_t& prefix,
42                        const type_t& type)
43   : m_hw(false)
44   , m_rd(rd.singular())
45   , m_prefix(prefix)
46   , m_type(type)
47   , m_recirc(nullptr)
48   , m_epg(nullptr)
49 {
50 }
51
52 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
53                        const route::prefix_t& prefix,
54                        const gbp_recirc& recirc,
55                        const gbp_endpoint_group& epg)
56   : m_hw(false)
57   , m_rd(rd.singular())
58   , m_prefix(prefix)
59   , m_type(type_t::STITCHED_EXTERNAL)
60   , m_recirc(recirc.singular())
61   , m_epg(epg.singular())
62 {
63 }
64
65 gbp_subnet::gbp_subnet(const gbp_subnet& o)
66   : m_hw(o.m_hw)
67   , m_rd(o.m_rd)
68   , m_prefix(o.m_prefix)
69   , m_type(o.m_type)
70   , m_recirc(o.m_recirc)
71   , m_epg(o.m_epg)
72 {
73 }
74
75 gbp_subnet::~gbp_subnet()
76 {
77   sweep();
78   m_db.release(key(), this);
79 }
80
81 const gbp_subnet::key_t
82 gbp_subnet::key() const
83 {
84   return (std::make_pair(m_rd->key(), m_prefix));
85 }
86
87 bool
88 gbp_subnet::operator==(const gbp_subnet& gs) const
89 {
90   return ((key() == gs.key()) && (m_type == gs.m_type) &&
91           (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg));
92 }
93
94 void
95 gbp_subnet::sweep()
96 {
97   if (m_hw) {
98     HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
99   }
100   HW::write();
101 }
102
103 void
104 gbp_subnet::replay()
105 {
106   if (m_hw) {
107     HW::enqueue(new gbp_subnet_cmds::create_cmd(
108       m_hw, m_rd->id(), m_prefix, m_type,
109       (m_recirc ? m_recirc->handle() : handle_t::INVALID),
110       (m_epg ? m_epg->id() : ~0)));
111   }
112 }
113
114 std::string
115 gbp_subnet::to_string() const
116 {
117   std::ostringstream s;
118   s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
119     << m_prefix.to_string();
120   if (m_recirc)
121     s << ", " << m_recirc->to_string();
122   if (m_epg)
123     s << ", " << m_epg->to_string();
124
125   s << "]";
126
127   return (s.str());
128 }
129
130 void
131 gbp_subnet::update(const gbp_subnet& r)
132 {
133   if (rc_t::OK != m_hw.rc()) {
134     HW::enqueue(new gbp_subnet_cmds::create_cmd(
135       m_hw, m_rd->id(), m_prefix, m_type,
136       (m_recirc ? m_recirc->handle() : handle_t::INVALID),
137       (m_epg ? m_epg->id() : ~0)));
138   } else {
139     if (m_type != r.m_type) {
140       m_epg = r.m_epg;
141       m_recirc = r.m_recirc;
142       m_type = r.m_type;
143
144       HW::enqueue(new gbp_subnet_cmds::create_cmd(
145         m_hw, m_rd->id(), m_prefix, m_type,
146         (m_recirc ? m_recirc->handle() : handle_t::INVALID),
147         (m_epg ? m_epg->id() : ~0)));
148     }
149   }
150 }
151
152 std::shared_ptr<gbp_subnet>
153 gbp_subnet::find_or_add(const gbp_subnet& temp)
154 {
155   return (m_db.find_or_add(temp.key(), temp));
156 }
157
158 std::shared_ptr<gbp_subnet>
159 gbp_subnet::find(const key_t& k)
160 {
161   return (m_db.find(k));
162 }
163
164 std::shared_ptr<gbp_subnet>
165 gbp_subnet::singular() const
166 {
167   return find_or_add(*this);
168 }
169
170 void
171 gbp_subnet::dump(std::ostream& os)
172 {
173   db_dump(m_db, os);
174 }
175
176 gbp_subnet::event_handler::event_handler()
177 {
178   OM::register_listener(this);
179   inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
180 }
181
182 void
183 gbp_subnet::event_handler::handle_replay()
184 {
185   m_db.replay();
186 }
187
188 void
189 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
190 {
191   std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
192     std::make_shared<gbp_subnet_cmds::dump_cmd>();
193
194   HW::enqueue(cmd);
195   HW::write();
196
197   for (auto& record : *cmd) {
198     auto& payload = record.get_payload();
199
200     route::prefix_t pfx = from_api(payload.subnet.prefix);
201     std::shared_ptr<gbp_route_domain> rd =
202       gbp_route_domain::find(payload.subnet.rd_id);
203
204     if (rd) {
205       switch (payload.subnet.type) {
206         case GBP_API_SUBNET_TRANSPORT: {
207           gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
208           OM::commit(key, gs);
209           VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
210           break;
211         }
212         case GBP_API_SUBNET_STITCHED_INTERNAL: {
213           gbp_subnet gs(*rd, pfx, type_t::STITCHED_INTERNAL);
214           OM::commit(key, gs);
215           VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
216           break;
217         }
218         case GBP_API_SUBNET_STITCHED_EXTERNAL: {
219           std::shared_ptr<interface> itf =
220             interface::find(payload.subnet.sw_if_index);
221           std::shared_ptr<gbp_endpoint_group> epg =
222             gbp_endpoint_group::find(payload.subnet.epg_id);
223
224           if (itf && epg) {
225             std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
226
227             if (recirc) {
228               gbp_subnet gs(*rd, pfx, *recirc, *epg);
229               OM::commit(key, gs);
230               VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
231             }
232           }
233         }
234       }
235     }
236   }
237 }
238
239 dependency_t
240 gbp_subnet::event_handler::order() const
241 {
242   return (dependency_t::ENTRY);
243 }
244
245 void
246 gbp_subnet::event_handler::show(std::ostream& os)
247 {
248   db_dump(m_db, os);
249 }
250
251 std::ostream&
252 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
253 {
254   os << "[" << key.first << ", " << key.second << "]";
255
256   return os;
257 }
258
259 } // namespace VOM
260
261 /*
262  * fd.io coding-style-patch-verification: ON
263  *
264  * Local Variables:
265  * eval: (c-set-style "mozilla")
266  * End:
267  */