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