6cad1954e5d605055aa5c7fe9b6eb77f7eaa917a
[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   : m_id(bd.id())
36   , m_bd(bd.singular())
37 {
38 }
39
40 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
41                                      const interface& bvi,
42                                      const interface& uu_fwd)
43   : m_id(bd.id())
44   , m_bd(bd.singular())
45   , m_bvi(bvi.singular())
46   , m_uu_fwd(uu_fwd.singular())
47 {
48 }
49
50 gbp_bridge_domain::gbp_bridge_domain(const gbp_bridge_domain& bd)
51   : m_id(bd.id())
52   , m_bd(bd.m_bd)
53 {
54 }
55
56 const gbp_bridge_domain::key_t
57 gbp_bridge_domain::key() const
58 {
59   return (m_bd->key());
60 }
61
62 uint32_t
63 gbp_bridge_domain::id() const
64 {
65   return (m_bd->id());
66 }
67
68 bool
69 gbp_bridge_domain::operator==(const gbp_bridge_domain& b) const
70 {
71   bool equal = true;
72
73   if (m_bvi && b.m_bvi)
74     equal &= (m_bvi->key() == b.m_bvi->key());
75   else if (!m_bvi && !b.m_bvi)
76     ;
77   else
78     equal = false;
79
80   if (m_uu_fwd && b.m_uu_fwd)
81     equal &= (m_uu_fwd->key() == b.m_uu_fwd->key());
82   else if (!m_uu_fwd && !b.m_uu_fwd)
83     ;
84   else
85     equal = false;
86
87   return ((m_bd->key() == b.m_bd->key()) && equal);
88 }
89
90 void
91 gbp_bridge_domain::sweep()
92 {
93   if (rc_t::OK == m_id.rc()) {
94     HW::enqueue(new gbp_bridge_domain_cmds::delete_cmd(m_id));
95   }
96   HW::write();
97 }
98
99 void
100 gbp_bridge_domain::replay()
101 {
102   if (rc_t::OK == m_id.rc()) {
103     if (m_bvi && m_uu_fwd)
104       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
105                                                          m_uu_fwd->handle()));
106     else
107       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
108         m_id, handle_t::INVALID, handle_t::INVALID));
109   }
110 }
111
112 gbp_bridge_domain::~gbp_bridge_domain()
113 {
114   sweep();
115
116   // not in the DB anymore.
117   m_db.release(m_id.data(), this);
118 }
119
120 std::string
121 gbp_bridge_domain::to_string() const
122 {
123   std::ostringstream s;
124   s << "gbp-bridge-domain:[" << m_bd->to_string() << "]";
125
126   return (s.str());
127 }
128
129 std::shared_ptr<gbp_bridge_domain>
130 gbp_bridge_domain::find(const key_t& key)
131 {
132   return (m_db.find(key));
133 }
134
135 void
136 gbp_bridge_domain::update(const gbp_bridge_domain& desired)
137 {
138   /*
139    * the desired state is always that the interface should be created
140    */
141   if (rc_t::OK != m_id.rc()) {
142     if (m_bvi && m_uu_fwd)
143       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
144                                                          m_uu_fwd->handle()));
145     else
146       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
147         m_id, handle_t::INVALID, handle_t::INVALID));
148   }
149 }
150
151 std::shared_ptr<gbp_bridge_domain>
152 gbp_bridge_domain::find_or_add(const gbp_bridge_domain& temp)
153 {
154   return (m_db.find_or_add(temp.m_id.data(), temp));
155 }
156
157 std::shared_ptr<gbp_bridge_domain>
158 gbp_bridge_domain::singular() const
159 {
160   return find_or_add(*this);
161 }
162
163 void
164 gbp_bridge_domain::dump(std::ostream& os)
165 {
166   db_dump(m_db, os);
167 }
168
169 void
170 gbp_bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
171 {
172   /*
173    * dump VPP Bridge domains
174    */
175   std::shared_ptr<gbp_bridge_domain_cmds::dump_cmd> cmd =
176     std::make_shared<gbp_bridge_domain_cmds::dump_cmd>();
177
178   HW::enqueue(cmd);
179   HW::write();
180
181   for (auto& record : *cmd) {
182     auto& payload = record.get_payload();
183
184     std::shared_ptr<interface> uu_fwd =
185       interface::find(payload.bd.uu_fwd_sw_if_index);
186     std::shared_ptr<interface> bvi =
187       interface::find(payload.bd.bvi_sw_if_index);
188
189     if (uu_fwd && bvi) {
190       gbp_bridge_domain bd(payload.bd.bd_id, *bvi, *uu_fwd);
191       OM::commit(key, bd);
192       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
193     } else {
194       gbp_bridge_domain bd(payload.bd.bd_id);
195       OM::commit(key, bd);
196       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
197     }
198   }
199 }
200
201 gbp_bridge_domain::event_handler::event_handler()
202 {
203   OM::register_listener(this);
204   inspect::register_handler({ "gbd", "gbridge" }, "GBP Bridge Domains", this);
205 }
206
207 void
208 gbp_bridge_domain::event_handler::handle_replay()
209 {
210   m_db.replay();
211 }
212
213 dependency_t
214 gbp_bridge_domain::event_handler::order() const
215 {
216   return (dependency_t::TABLE);
217 }
218
219 void
220 gbp_bridge_domain::event_handler::show(std::ostream& os)
221 {
222   db_dump(m_db, os);
223 }
224 }
225
226 /*
227  * fd.io coding-style-patch-verification: ON
228  *
229  * Local Variables:
230  * eval: (c-set-style "mozilla")
231  * End:
232  */