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 gbp_endpoint::gbp_endpoint(
29 const std::vector<boost::asio::ip::address>& ip_addrs,
30 const mac_address_t& mac,
31 const gbp_endpoint_group& epg)
32 : m_hdl(handle_t::INVALID)
33 , m_itf(itf.singular())
36 , m_epg(epg.singular())
40 gbp_endpoint::gbp_endpoint(const gbp_endpoint& gbpe)
49 gbp_endpoint::~gbp_endpoint()
52 m_db.release(key(), this);
55 const gbp_endpoint::key_t
56 gbp_endpoint::key() const
58 return (std::make_pair(m_itf->key(), m_mac));
62 gbp_endpoint::operator==(const gbp_endpoint& gbpe) const
64 return ((key() == gbpe.key()) && (m_epg == gbpe.m_epg));
71 HW::enqueue(new gbp_endpoint_cmds::delete_cmd(m_hdl));
77 gbp_endpoint::replay()
80 HW::enqueue(new gbp_endpoint_cmds::create_cmd(m_hdl, m_itf->handle(), m_ips,
86 gbp_endpoint::to_string() const
89 s << "gbp-endpoint:[" << m_itf->to_string() << ", ips:[";
94 s << "], " << m_mac.to_string() << ", epg:" << m_epg->to_string() << "]";
100 gbp_endpoint::update(const gbp_endpoint& r)
102 if (rc_t::OK != m_hdl.rc()) {
103 HW::enqueue(new gbp_endpoint_cmds::create_cmd(m_hdl, m_itf->handle(), m_ips,
104 m_mac, m_epg->id()));
108 std::shared_ptr<gbp_endpoint>
109 gbp_endpoint::find_or_add(const gbp_endpoint& temp)
111 return (m_db.find_or_add(temp.key(), temp));
114 std::shared_ptr<gbp_endpoint>
115 gbp_endpoint::find(const key_t& k)
117 return (m_db.find(k));
120 std::shared_ptr<gbp_endpoint>
121 gbp_endpoint::singular() const
123 return find_or_add(*this);
127 gbp_endpoint::dump(std::ostream& os)
132 gbp_endpoint::event_handler::event_handler()
134 OM::register_listener(this);
135 inspect::register_handler({ "gbp-endpoint" }, "GBP Endpoints", this);
139 gbp_endpoint::event_handler::handle_replay()
145 gbp_endpoint::event_handler::handle_populate(const client_db::key_t& key)
147 std::shared_ptr<gbp_endpoint_cmds::dump_cmd> cmd =
148 std::make_shared<gbp_endpoint_cmds::dump_cmd>();
153 for (auto& record : *cmd) {
154 auto& payload = record.get_payload();
156 std::vector<boost::asio::ip::address> addresses;
158 for (uint8_t n = 0; n < payload.endpoint.n_ips; n++)
159 addresses.push_back(from_api(payload.endpoint.ips[n]));
160 std::shared_ptr<interface> itf =
161 interface::find(payload.endpoint.sw_if_index);
162 std::shared_ptr<gbp_endpoint_group> epg =
163 gbp_endpoint_group::find(payload.endpoint.epg_id);
164 mac_address_t mac = from_api(payload.endpoint.mac);
166 VOM_LOG(log_level_t::DEBUG) << "data: " << payload.endpoint.sw_if_index;
169 gbp_endpoint gbpe(*itf, addresses, mac, *epg);
170 OM::commit(key, gbpe);
172 VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
174 VOM_LOG(log_level_t::ERROR)
175 << "no interface:" << payload.endpoint.sw_if_index
176 << "or epg:" << payload.endpoint.epg_id;
182 gbp_endpoint::event_handler::order() const
184 return (dependency_t::ENTRY);
188 gbp_endpoint::event_handler::show(std::ostream& os)
194 operator<<(std::ostream& os, const gbp_endpoint::key_t& key)
196 os << key.first << "," << key.second;
204 * fd.io coding-style-patch-verification: ON
207 * eval: (c-set-style "mozilla")