VOM: fixes for stats
[vpp.git] / extras / vom / vom / gbp_bridge_domain.cpp
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 #include "vom/gbp_bridge_domain.hpp"
17 #include "vom/gbp_bridge_domain_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
20 #include "vom/singular_db_funcs.hpp"
21
22 namespace VOM {
23
24 /**
25  * A DB of al the interfaces, key on the name
26  */
27 singular_db<uint32_t, gbp_bridge_domain> gbp_bridge_domain::m_db;
28
29 gbp_bridge_domain::event_handler gbp_bridge_domain::m_evh;
30
31 /**
32  * Construct a new object matching the desried state
33  */
34 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
35                                      const interface& bvi)
36   : m_id(bd.id())
37   , m_bd(bd.singular())
38   , m_bvi(bvi.singular())
39 {
40 }
41
42 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
43                                      const interface& bvi,
44                                      const interface& uu_fwd)
45   : m_id(bd.id())
46   , m_bd(bd.singular())
47   , m_bvi(bvi.singular())
48   , m_uu_fwd(uu_fwd.singular())
49 {
50 }
51
52 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
53                                      const std::shared_ptr<interface> bvi,
54                                      const std::shared_ptr<interface> uu_fwd)
55   : m_id(bd.id())
56   , m_bd(bd.singular())
57   , m_bvi(bvi->singular())
58   , m_uu_fwd(uu_fwd->singular())
59 {
60 }
61
62 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
63                                      const interface& bvi,
64                                      const std::shared_ptr<interface> uu_fwd)
65   : m_id(bd.id())
66   , m_bd(bd.singular())
67   , m_bvi(bvi.singular())
68   , m_uu_fwd(uu_fwd->singular())
69 {
70 }
71
72 gbp_bridge_domain::gbp_bridge_domain(const gbp_bridge_domain& bd)
73   : m_id(bd.id())
74   , m_bd(bd.m_bd)
75   , m_bvi(bd.m_bvi)
76   , m_uu_fwd(bd.m_uu_fwd)
77 {
78 }
79
80 const gbp_bridge_domain::key_t
81 gbp_bridge_domain::key() const
82 {
83   return (m_bd->key());
84 }
85
86 uint32_t
87 gbp_bridge_domain::id() const
88 {
89   return (m_bd->id());
90 }
91
92 const std::shared_ptr<bridge_domain>
93 gbp_bridge_domain::get_bridge_domain()
94 {
95   return m_bd;
96 }
97
98 const std::shared_ptr<interface>
99 gbp_bridge_domain::get_bvi()
100 {
101   return m_bvi;
102 }
103
104 bool
105 gbp_bridge_domain::operator==(const gbp_bridge_domain& b) const
106 {
107   bool equal = true;
108
109   if (m_bvi && b.m_bvi)
110     equal &= (m_bvi->key() == b.m_bvi->key());
111   else if (!m_bvi && !b.m_bvi)
112     ;
113   else
114     equal = false;
115
116   if (m_uu_fwd && b.m_uu_fwd)
117     equal &= (m_uu_fwd->key() == b.m_uu_fwd->key());
118   else if (!m_uu_fwd && !b.m_uu_fwd)
119     ;
120   else
121     equal = false;
122
123   return ((m_bd->key() == b.m_bd->key()) && equal);
124 }
125
126 void
127 gbp_bridge_domain::sweep()
128 {
129   if (rc_t::OK == m_id.rc()) {
130     HW::enqueue(new gbp_bridge_domain_cmds::delete_cmd(m_id));
131   }
132   HW::write();
133 }
134
135 void
136 gbp_bridge_domain::replay()
137 {
138   if (rc_t::OK == m_id.rc()) {
139     if (m_bvi && m_uu_fwd)
140       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
141                                                          m_uu_fwd->handle()));
142     else
143       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
144         m_id, handle_t::INVALID, handle_t::INVALID));
145   }
146 }
147
148 gbp_bridge_domain::~gbp_bridge_domain()
149 {
150   sweep();
151
152   // not in the DB anymore.
153   m_db.release(m_id.data(), this);
154 }
155
156 std::string
157 gbp_bridge_domain::to_string() const
158 {
159   std::ostringstream s;
160   s << "gbp-bridge-domain:[" << m_bd->to_string();
161
162   if (m_bvi)
163     s << " bvi:" << m_bvi->to_string();
164   if (m_uu_fwd)
165     s << " uu-fwd:" << m_uu_fwd->to_string();
166
167   s << "]";
168
169   return (s.str());
170 }
171
172 std::shared_ptr<gbp_bridge_domain>
173 gbp_bridge_domain::find(const key_t& key)
174 {
175   return (m_db.find(key));
176 }
177
178 void
179 gbp_bridge_domain::update(const gbp_bridge_domain& desired)
180 {
181   /*
182    * the desired state is always that the interface should be created
183    */
184   if (rc_t::OK != m_id.rc()) {
185     if (m_bvi && m_uu_fwd)
186       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
187                                                          m_uu_fwd->handle()));
188     else
189       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
190         m_id, handle_t::INVALID, handle_t::INVALID));
191   }
192 }
193
194 std::shared_ptr<gbp_bridge_domain>
195 gbp_bridge_domain::find_or_add(const gbp_bridge_domain& temp)
196 {
197   return (m_db.find_or_add(temp.m_id.data(), temp));
198 }
199
200 std::shared_ptr<gbp_bridge_domain>
201 gbp_bridge_domain::singular() const
202 {
203   return find_or_add(*this);
204 }
205
206 void
207 gbp_bridge_domain::dump(std::ostream& os)
208 {
209   db_dump(m_db, os);
210 }
211
212 void
213 gbp_bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
214 {
215   /*
216    * dump VPP Bridge domains
217    */
218   std::shared_ptr<gbp_bridge_domain_cmds::dump_cmd> cmd =
219     std::make_shared<gbp_bridge_domain_cmds::dump_cmd>();
220
221   HW::enqueue(cmd);
222   HW::write();
223
224   for (auto& record : *cmd) {
225     auto& payload = record.get_payload();
226
227     std::shared_ptr<interface> uu_fwd =
228       interface::find(payload.bd.uu_fwd_sw_if_index);
229     std::shared_ptr<interface> bvi =
230       interface::find(payload.bd.bvi_sw_if_index);
231
232     if (uu_fwd && bvi) {
233       gbp_bridge_domain bd(payload.bd.bd_id, bvi, uu_fwd);
234       OM::commit(key, bd);
235       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
236     } else if (bvi) {
237       gbp_bridge_domain bd(payload.bd.bd_id, *bvi);
238       OM::commit(key, bd);
239       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
240     }
241   }
242 }
243
244 gbp_bridge_domain::event_handler::event_handler()
245 {
246   OM::register_listener(this);
247   inspect::register_handler({ "gbd", "gbridge" }, "GBP Bridge Domains", this);
248 }
249
250 void
251 gbp_bridge_domain::event_handler::handle_replay()
252 {
253   m_db.replay();
254 }
255
256 dependency_t
257 gbp_bridge_domain::event_handler::order() const
258 {
259   return (dependency_t::TABLE);
260 }
261
262 void
263 gbp_bridge_domain::event_handler::show(std::ostream& os)
264 {
265   db_dump(m_db, os);
266 }
267 }
268
269 /*
270  * fd.io coding-style-patch-verification: ON
271  *
272  * Local Variables:
273  * eval: (c-set-style "mozilla")
274  * End:
275  */