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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
23 singular_db<gbp_endpoint::key_t, gbp_endpoint> gbp_endpoint::m_db;
25 gbp_endpoint::event_handler gbp_endpoint::m_evh;
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");
33 gbp_endpoint::flags_t::flags_t(int v, const std::string& s)
34 : enum_base<gbp_endpoint::flags_t>(v, s)
38 gbp_endpoint::gbp_endpoint(
40 const std::vector<boost::asio::ip::address>& ip_addrs,
41 const mac_address_t& mac,
42 const gbp_endpoint_group& epg,
44 : m_hdl(handle_t::INVALID)
45 , m_itf(itf.singular())
48 , m_epg(epg.singular())
53 gbp_endpoint::gbp_endpoint(const gbp_endpoint& gbpe)
59 , m_flags(gbpe.m_flags)
63 gbp_endpoint::~gbp_endpoint()
66 m_db.release(key(), this);
69 const gbp_endpoint::key_t
70 gbp_endpoint::key() const
72 return (std::make_pair(m_itf->key(), m_mac));
76 gbp_endpoint::operator==(const gbp_endpoint& gbpe) const
78 return ((key() == gbpe.key()) && (m_epg == gbpe.m_epg) &&
79 (m_flags == gbpe.m_flags));
86 HW::enqueue(new gbp_endpoint_cmds::delete_cmd(m_hdl));
92 gbp_endpoint::replay()
95 HW::enqueue(new gbp_endpoint_cmds::create_cmd(
96 m_hdl, m_itf->handle(), m_ips, m_mac, m_epg->sclass(), m_flags));
101 gbp_endpoint::to_string() const
103 std::ostringstream s;
104 s << "gbp-endpoint:[" << m_itf->to_string() << ", ips:[";
106 for (auto ip : m_ips)
109 s << "], " << m_mac.to_string() << ", epg:" << m_epg->to_string() << "]";
115 gbp_endpoint::update(const gbp_endpoint& r)
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));
123 std::shared_ptr<gbp_endpoint>
124 gbp_endpoint::find_or_add(const gbp_endpoint& temp)
126 return (m_db.find_or_add(temp.key(), temp));
129 std::shared_ptr<gbp_endpoint>
130 gbp_endpoint::find(const key_t& k)
132 return (m_db.find(k));
135 std::shared_ptr<gbp_endpoint>
136 gbp_endpoint::singular() const
138 return find_or_add(*this);
142 gbp_endpoint::dump(std::ostream& os)
147 gbp_endpoint::event_handler::event_handler()
149 OM::register_listener(this);
150 inspect::register_handler({ "gbp-endpoint" }, "GBP Endpoints", this);
154 gbp_endpoint::event_handler::handle_replay()
160 gbp_endpoint::event_handler::handle_populate(const client_db::key_t& key)
162 std::shared_ptr<gbp_endpoint_cmds::dump_cmd> cmd =
163 std::make_shared<gbp_endpoint_cmds::dump_cmd>();
168 for (auto& record : *cmd) {
169 auto& payload = record.get_payload();
171 std::vector<boost::asio::ip::address> addresses;
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);
181 VOM_LOG(log_level_t::DEBUG) << "data: " << payload.endpoint.sw_if_index;
184 gbp_endpoint gbpe(*itf, addresses, mac, *epg);
185 OM::commit(key, gbpe);
187 VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
189 VOM_LOG(log_level_t::ERROR)
190 << "no interface:" << payload.endpoint.sw_if_index
191 << "or sclass:" << payload.endpoint.sclass;
197 gbp_endpoint::event_handler::order() const
199 return (dependency_t::ENTRY);
203 gbp_endpoint::event_handler::show(std::ostream& os)
209 operator<<(std::ostream& os, const gbp_endpoint::key_t& key)
211 os << key.first << "," << key.second;
219 * fd.io coding-style-patch-verification: ON
222 * eval: (c-set-style "mozilla")