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