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