2786297b0c8b575137939541836d5287618aa504
[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 {
38 }
39
40 gbp_route_domain::gbp_route_domain(const route_domain& rd,
41                                    const interface& ip4_uu_fwd,
42                                    const interface& ip6_uu_fwd)
43   : m_id(rd.table_id())
44   , m_rd(rd.singular())
45   , m_ip4_uu_fwd(ip4_uu_fwd.singular())
46   , m_ip6_uu_fwd(ip6_uu_fwd.singular())
47 {
48 }
49
50 gbp_route_domain::gbp_route_domain(const route_domain& rd)
51   : m_id(rd.table_id())
52   , m_rd(rd.singular())
53 {
54 }
55
56 const gbp_route_domain::key_t
57 gbp_route_domain::key() const
58 {
59   return (m_rd->key());
60 }
61
62 uint32_t
63 gbp_route_domain::id() const
64 {
65   return (m_rd->table_id());
66 }
67
68 bool
69 gbp_route_domain::operator==(const gbp_route_domain& b) const
70 {
71   bool equal = true;
72
73   if (m_ip4_uu_fwd && b.m_ip4_uu_fwd)
74     equal &= (m_ip4_uu_fwd->key() == b.m_ip4_uu_fwd->key());
75   else if (!m_ip4_uu_fwd && !b.m_ip4_uu_fwd)
76     ;
77   else
78     equal = false;
79
80   if (m_ip6_uu_fwd && b.m_ip6_uu_fwd)
81     equal &= (m_ip6_uu_fwd->key() == b.m_ip6_uu_fwd->key());
82   else if (!m_ip6_uu_fwd && !b.m_ip6_uu_fwd)
83     ;
84   else
85     equal = false;
86
87   return ((m_rd->key() == b.m_rd->key()) && equal);
88 }
89
90 void
91 gbp_route_domain::sweep()
92 {
93   if (rc_t::OK == m_id.rc()) {
94     HW::enqueue(new gbp_route_domain_cmds::delete_cmd(m_id));
95   }
96   HW::write();
97 }
98
99 void
100 gbp_route_domain::replay()
101 {
102   if (rc_t::OK == m_id.rc()) {
103     if (m_ip4_uu_fwd && m_ip6_uu_fwd)
104       HW::enqueue(new gbp_route_domain_cmds::create_cmd(
105         m_id, m_ip4_uu_fwd->handle(), m_ip6_uu_fwd->handle()));
106     else
107       HW::enqueue(new gbp_route_domain_cmds::create_cmd(m_id, handle_t::INVALID,
108                                                         handle_t::INVALID));
109   }
110 }
111
112 gbp_route_domain::~gbp_route_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_route_domain::to_string() const
122 {
123   std::ostringstream s;
124   s << "gbp-route-domain:[" << m_rd->to_string() << "]";
125
126   return (s.str());
127 }
128
129 std::shared_ptr<gbp_route_domain>
130 gbp_route_domain::find(const key_t& key)
131 {
132   return (m_db.find(key));
133 }
134
135 void
136 gbp_route_domain::update(const gbp_route_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_ip4_uu_fwd && m_ip6_uu_fwd)
143       HW::enqueue(new gbp_route_domain_cmds::create_cmd(
144         m_id, m_ip4_uu_fwd->handle(), m_ip6_uu_fwd->handle()));
145     else
146       HW::enqueue(new gbp_route_domain_cmds::create_cmd(m_id, handle_t::INVALID,
147                                                         handle_t::INVALID));
148   }
149 }
150
151 std::shared_ptr<gbp_route_domain>
152 gbp_route_domain::find_or_add(const gbp_route_domain& temp)
153 {
154   return (m_db.find_or_add(temp.m_id.data(), temp));
155 }
156
157 std::shared_ptr<gbp_route_domain>
158 gbp_route_domain::singular() const
159 {
160   return find_or_add(*this);
161 }
162
163 void
164 gbp_route_domain::dump(std::ostream& os)
165 {
166   db_dump(m_db, os);
167 }
168
169 void
170 gbp_route_domain::event_handler::handle_populate(const client_db::key_t& key)
171 {
172   /*
173    * dump VPP Route domains
174    */
175   std::shared_ptr<gbp_route_domain_cmds::dump_cmd> cmd =
176     std::make_shared<gbp_route_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> ip6_uu_fwd =
185       interface::find(payload.rd.ip6_uu_sw_if_index);
186     std::shared_ptr<interface> ip4_uu_fwd =
187       interface::find(payload.rd.ip4_uu_sw_if_index);
188
189     if (ip6_uu_fwd && ip4_uu_fwd) {
190       gbp_route_domain rd(payload.rd.rd_id, *ip4_uu_fwd, *ip6_uu_fwd);
191       OM::commit(key, rd);
192       VOM_LOG(log_level_t::DEBUG) << "dump: " << rd.to_string();
193     } else {
194       gbp_route_domain rd(payload.rd.rd_id);
195       OM::commit(key, rd);
196       VOM_LOG(log_level_t::DEBUG) << "dump: " << rd.to_string();
197     }
198   }
199 }
200
201 gbp_route_domain::event_handler::event_handler()
202 {
203   OM::register_listener(this);
204   inspect::register_handler({ "grd", "groute" }, "GBP Route Domains", this);
205 }
206
207 void
208 gbp_route_domain::event_handler::handle_replay()
209 {
210   m_db.replay();
211 }
212
213 dependency_t
214 gbp_route_domain::event_handler::order() const
215 {
216   return (dependency_t::TABLE);
217 }
218
219 void
220 gbp_route_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  */