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