GBP: l3-out subnets
[vpp.git] / extras / vom / vom / gbp_ext_itf.cpp
1 /*
2  * Copyright (c) 2018 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_ext_itf.hpp"
17 #include "vom/gbp_ext_itf_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21
22 singular_db<gbp_ext_itf::key_t, gbp_ext_itf> gbp_ext_itf::m_db;
23
24 gbp_ext_itf::event_handler gbp_ext_itf::m_evh;
25
26 gbp_ext_itf::gbp_ext_itf(const interface& itf,
27                          const gbp_bridge_domain& gbd,
28                          const gbp_route_domain& grd)
29   : m_hw(false)
30   , m_itf(itf.singular())
31   , m_bd(gbd.singular())
32   , m_rd(grd.singular())
33 {
34 }
35
36 gbp_ext_itf::gbp_ext_itf(const gbp_ext_itf& gbpe)
37   : m_hw(gbpe.m_hw)
38   , m_itf(gbpe.m_itf)
39   , m_bd(gbpe.m_bd)
40   , m_rd(gbpe.m_rd)
41 {
42 }
43
44 gbp_ext_itf::~gbp_ext_itf()
45 {
46   sweep();
47   m_db.release(key(), this);
48 }
49
50 const gbp_ext_itf::key_t
51 gbp_ext_itf::key() const
52 {
53   return (m_itf->key());
54 }
55
56 const handle_t&
57 gbp_ext_itf::handle() const
58 {
59   return m_itf->handle();
60 }
61
62 bool
63 gbp_ext_itf::operator==(const gbp_ext_itf& gei) const
64 {
65   return ((key() == gei.key()) && (m_itf == gei.m_itf) && (m_rd == gei.m_rd) &&
66           (m_bd == gei.m_bd));
67 }
68
69 void
70 gbp_ext_itf::sweep()
71 {
72   if (m_hw) {
73     HW::enqueue(new gbp_ext_itf_cmds::delete_cmd(m_hw, m_itf->handle()));
74   }
75   HW::write();
76 }
77
78 void
79 gbp_ext_itf::replay()
80 {
81   if (m_hw) {
82     HW::enqueue(new gbp_ext_itf_cmds::create_cmd(m_hw, m_itf->handle(),
83                                                  m_bd->id(), m_rd->id()));
84   }
85 }
86
87 std::string
88 gbp_ext_itf::to_string() const
89 {
90   std::ostringstream s;
91   s << "gbp-ext_itf:[" << m_itf->to_string() << ", " << m_bd->to_string()
92     << ", " << m_rd->to_string() << "]";
93
94   return (s.str());
95 }
96
97 void
98 gbp_ext_itf::update(const gbp_ext_itf& r)
99 {
100   if (rc_t::OK != m_hw.rc()) {
101     HW::enqueue(new gbp_ext_itf_cmds::create_cmd(m_hw, m_itf->handle(),
102                                                  m_bd->id(), m_rd->id()));
103   }
104 }
105
106 std::shared_ptr<gbp_ext_itf>
107 gbp_ext_itf::find_or_add(const gbp_ext_itf& temp)
108 {
109   return (m_db.find_or_add(temp.key(), temp));
110 }
111
112 std::shared_ptr<gbp_ext_itf>
113 gbp_ext_itf::find(const key_t& k)
114 {
115   return (m_db.find(k));
116 }
117
118 std::shared_ptr<gbp_ext_itf>
119 gbp_ext_itf::singular() const
120 {
121   return find_or_add(*this);
122 }
123
124 void
125 gbp_ext_itf::dump(std::ostream& os)
126 {
127   db_dump(m_db, os);
128 }
129
130 gbp_ext_itf::event_handler::event_handler()
131 {
132   OM::register_listener(this);
133   inspect::register_handler({ "gbp-ext-itf" }, "GBP External-Itfs", this);
134 }
135
136 void
137 gbp_ext_itf::event_handler::handle_replay()
138 {
139   m_db.replay();
140 }
141
142 void
143 gbp_ext_itf::event_handler::handle_populate(const client_db::key_t& key)
144 {
145   std::shared_ptr<gbp_ext_itf_cmds::dump_cmd> cmd =
146     std::make_shared<gbp_ext_itf_cmds::dump_cmd>();
147
148   HW::enqueue(cmd);
149   HW::write();
150
151   for (auto& record : *cmd) {
152     auto& payload = record.get_payload();
153
154     std::shared_ptr<interface> itf =
155       interface::find(payload.ext_itf.sw_if_index);
156     std::shared_ptr<gbp_bridge_domain> gbd =
157       gbp_bridge_domain::find(payload.ext_itf.bd_id);
158     std::shared_ptr<gbp_route_domain> grd =
159       gbp_route_domain::find(payload.ext_itf.rd_id);
160
161     VOM_LOG(log_level_t::DEBUG) << "data: [" << payload.ext_itf.sw_if_index
162                                 << ", " << payload.ext_itf.bd_id << ", "
163                                 << payload.ext_itf.rd_id << "]";
164
165     if (itf && gbd && grd) {
166       gbp_ext_itf ext_itf(*itf, *gbd, *grd);
167       OM::commit(key, ext_itf);
168
169       VOM_LOG(log_level_t::DEBUG) << "read: " << ext_itf.to_string();
170     }
171   }
172 }
173
174 dependency_t
175 gbp_ext_itf::event_handler::order() const
176 {
177   return (dependency_t::BINDING);
178 }
179
180 void
181 gbp_ext_itf::event_handler::show(std::ostream& os)
182 {
183   db_dump(m_db, os);
184 }
185 } // namespace VOM
186
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "mozilla")
192  * End:
193  */