vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / extras / vom / vom / gbp_endpoint.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_endpoint.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_endpoint_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 singular_db<gbp_endpoint::key_t, gbp_endpoint> gbp_endpoint::m_db;
24
25 gbp_endpoint::event_handler gbp_endpoint::m_evh;
26
27 const gbp_endpoint::flags_t gbp_endpoint::flags_t::NONE(0, "none");
28 const gbp_endpoint::flags_t gbp_endpoint::flags_t::BOUNCE(1, "bounce");
29 const gbp_endpoint::flags_t gbp_endpoint::flags_t::LEARNT(2, "learnt");
30 const gbp_endpoint::flags_t gbp_endpoint::flags_t::REMOTE(4, "remote");
31 const gbp_endpoint::flags_t gbp_endpoint::flags_t::EXTERNAL(8, "external");
32
33 gbp_endpoint::flags_t::flags_t(int v, const std::string& s)
34   : enum_base<gbp_endpoint::flags_t>(v, s)
35 {
36 }
37
38 gbp_endpoint::gbp_endpoint(
39   const interface& itf,
40   const std::vector<boost::asio::ip::address>& ip_addrs,
41   const mac_address_t& mac,
42   const gbp_endpoint_group& epg,
43   const flags_t& flags)
44   : m_hdl(handle_t::INVALID)
45   , m_itf(itf.singular())
46   , m_ips(ip_addrs)
47   , m_mac(mac)
48   , m_epg(epg.singular())
49   , m_flags(flags)
50 {
51 }
52
53 gbp_endpoint::gbp_endpoint(const gbp_endpoint& gbpe)
54   : m_hdl(gbpe.m_hdl)
55   , m_itf(gbpe.m_itf)
56   , m_ips(gbpe.m_ips)
57   , m_mac(gbpe.m_mac)
58   , m_epg(gbpe.m_epg)
59   , m_flags(gbpe.m_flags)
60 {
61 }
62
63 gbp_endpoint::~gbp_endpoint()
64 {
65   sweep();
66   m_db.release(key(), this);
67 }
68
69 const gbp_endpoint::key_t
70 gbp_endpoint::key() const
71 {
72   return (std::make_pair(m_itf->key(), m_mac));
73 }
74
75 bool
76 gbp_endpoint::operator==(const gbp_endpoint& gbpe) const
77 {
78   return ((key() == gbpe.key()) && (m_epg == gbpe.m_epg) &&
79           (m_flags == gbpe.m_flags));
80 }
81
82 void
83 gbp_endpoint::sweep()
84 {
85   if (m_hdl) {
86     HW::enqueue(new gbp_endpoint_cmds::delete_cmd(m_hdl));
87   }
88   HW::write();
89 }
90
91 void
92 gbp_endpoint::replay()
93 {
94   if (m_hdl) {
95     HW::enqueue(new gbp_endpoint_cmds::create_cmd(
96       m_hdl, m_itf->handle(), m_ips, m_mac, m_epg->sclass(), m_flags));
97   }
98 }
99
100 std::string
101 gbp_endpoint::to_string() const
102 {
103   std::ostringstream s;
104   s << "gbp-endpoint:[" << m_itf->to_string() << ", ips:[";
105
106   for (auto ip : m_ips)
107     s << ip.to_string();
108
109   s << "], " << m_mac.to_string() << ", epg:" << m_epg->to_string() << "]";
110
111   return (s.str());
112 }
113
114 void
115 gbp_endpoint::update(const gbp_endpoint& r)
116 {
117   if (rc_t::OK != m_hdl.rc()) {
118     HW::enqueue(new gbp_endpoint_cmds::create_cmd(
119       m_hdl, m_itf->handle(), m_ips, m_mac, m_epg->sclass(), m_flags));
120   }
121 }
122
123 std::shared_ptr<gbp_endpoint>
124 gbp_endpoint::find_or_add(const gbp_endpoint& temp)
125 {
126   return (m_db.find_or_add(temp.key(), temp));
127 }
128
129 std::shared_ptr<gbp_endpoint>
130 gbp_endpoint::find(const key_t& k)
131 {
132   return (m_db.find(k));
133 }
134
135 std::shared_ptr<gbp_endpoint>
136 gbp_endpoint::singular() const
137 {
138   return find_or_add(*this);
139 }
140
141 void
142 gbp_endpoint::dump(std::ostream& os)
143 {
144   db_dump(m_db, os);
145 }
146
147 gbp_endpoint::event_handler::event_handler()
148 {
149   OM::register_listener(this);
150   inspect::register_handler({ "gbp-endpoint" }, "GBP Endpoints", this);
151 }
152
153 void
154 gbp_endpoint::event_handler::handle_replay()
155 {
156   m_db.replay();
157 }
158
159 void
160 gbp_endpoint::event_handler::handle_populate(const client_db::key_t& key)
161 {
162   std::shared_ptr<gbp_endpoint_cmds::dump_cmd> cmd =
163     std::make_shared<gbp_endpoint_cmds::dump_cmd>();
164
165   HW::enqueue(cmd);
166   HW::write();
167
168   for (auto& record : *cmd) {
169     auto& payload = record.get_payload();
170
171     std::vector<boost::asio::ip::address> addresses;
172
173     for (uint8_t n = 0; n < payload.endpoint.n_ips; n++)
174       addresses.push_back(from_api(payload.endpoint.ips[n]));
175     std::shared_ptr<interface> itf =
176       interface::find(payload.endpoint.sw_if_index);
177     std::shared_ptr<gbp_endpoint_group> epg =
178       gbp_endpoint_group::find(payload.endpoint.sclass);
179     mac_address_t mac = from_api(payload.endpoint.mac);
180
181     VOM_LOG(log_level_t::DEBUG) << "data: " << payload.endpoint.sw_if_index;
182
183     if (itf && epg) {
184       gbp_endpoint gbpe(*itf, addresses, mac, *epg);
185       OM::commit(key, gbpe);
186
187       VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
188     } else {
189       VOM_LOG(log_level_t::ERROR)
190         << "no interface:" << payload.endpoint.sw_if_index
191         << "or sclass:" << payload.endpoint.sclass;
192     }
193   }
194 }
195
196 dependency_t
197 gbp_endpoint::event_handler::order() const
198 {
199   return (dependency_t::ENTRY);
200 }
201
202 void
203 gbp_endpoint::event_handler::show(std::ostream& os)
204 {
205   db_dump(m_db, os);
206 }
207
208 std::ostream&
209 operator<<(std::ostream& os, const gbp_endpoint::key_t& key)
210 {
211   os << key.first << "," << key.second;
212
213   return os;
214 }
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  */