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:
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/l2_xconnect.hpp"
17 #include "vom/l2_xconnect_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
22 * A DB of all the L2 x-connect Configs
24 singular_db<l2_xconnect::key_t, l2_xconnect> l2_xconnect::m_db;
26 l2_xconnect::event_handler l2_xconnect::m_evh;
29 * Construct a new object matching the desried state
31 l2_xconnect::l2_xconnect(const interface& east_itf, const interface& west_itf)
32 : m_east_itf(east_itf.singular())
33 , m_west_itf(west_itf.singular())
39 l2_xconnect::l2_xconnect(const l2_xconnect& o)
40 : m_east_itf(o.m_east_itf)
41 , m_west_itf(o.m_west_itf)
42 , m_xconnect_east(o.m_xconnect_east)
43 , m_xconnect_west(o.m_xconnect_west)
47 const l2_xconnect::key_t
48 l2_xconnect::key() const
50 if (m_east_itf->name() < m_west_itf->name())
51 return (std::make_pair(m_east_itf->key(), m_west_itf->key()));
52 return (std::make_pair(m_west_itf->key(), m_east_itf->key()));
56 l2_xconnect::operator==(const l2_xconnect& l) const
58 return ((*m_east_itf == *l.m_east_itf) && (*m_west_itf == *l.m_west_itf));
61 std::shared_ptr<l2_xconnect>
62 l2_xconnect::find(const key_t& key)
64 return (m_db.find(key));
70 if (m_xconnect_east && m_xconnect_west &&
71 handle_t::INVALID != m_east_itf->handle() &&
72 handle_t::INVALID != m_west_itf->handle()) {
73 HW::enqueue(new l2_xconnect_cmds::unbind_cmd(
74 m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
75 HW::enqueue(new l2_xconnect_cmds::unbind_cmd(
76 m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
85 if (m_xconnect_east && m_xconnect_west &&
86 handle_t::INVALID != m_east_itf->handle() &&
87 handle_t::INVALID != m_west_itf->handle()) {
88 HW::enqueue(new l2_xconnect_cmds::bind_cmd(
89 m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
90 HW::enqueue(new l2_xconnect_cmds::bind_cmd(
91 m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
95 l2_xconnect::~l2_xconnect()
99 // not in the DB anymore.
100 m_db.release(key(), this);
104 l2_xconnect::to_string() const
106 std::ostringstream s;
107 s << "L2-xconnect:[" << m_east_itf->to_string() << " "
108 << m_west_itf->to_string() << " " << m_xconnect_east.to_string() << " "
109 << m_xconnect_west.to_string() << "]";
115 l2_xconnect::update(const l2_xconnect& desired)
118 * the desired state is always that the interface should be created
120 if (rc_t::OK != m_xconnect_east.rc() && rc_t::OK != m_xconnect_west.rc()) {
121 HW::enqueue(new l2_xconnect_cmds::bind_cmd(
122 m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
123 HW::enqueue(new l2_xconnect_cmds::bind_cmd(
124 m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
128 std::shared_ptr<l2_xconnect>
129 l2_xconnect::find_or_add(const l2_xconnect& temp)
131 return (m_db.find_or_add(temp.key(), temp));
134 std::shared_ptr<l2_xconnect>
135 l2_xconnect::singular() const
137 return find_or_add(*this);
141 l2_xconnect::dump(std::ostream& os)
146 l2_xconnect::event_handler::event_handler()
148 OM::register_listener(this);
149 inspect::register_handler({ "l2-xconnect" }, "L2 xconnects", this);
153 l2_xconnect::event_handler::handle_replay()
159 l2_xconnect::event_handler::handle_populate(const client_db::key_t& key)
162 * This needs to be done here
164 std::shared_ptr<l2_xconnect_cmds::dump_cmd> cmd =
165 std::make_shared<l2_xconnect_cmds::dump_cmd>();
170 for (auto& x_record : *cmd) {
171 auto& payload = x_record.get_payload();
173 VOM_LOG(log_level_t::DEBUG) << "l2-xconnect dump: "
174 << " east-itf: " << payload.rx_sw_if_index
175 << " west-itf: " << payload.tx_sw_if_index;
177 std::shared_ptr<interface> east_itf =
178 interface::find(payload.rx_sw_if_index);
179 std::shared_ptr<interface> west_itf =
180 interface::find(payload.tx_sw_if_index);
182 if (east_itf && west_itf) {
183 if (east_itf->name() > west_itf->name())
185 l2_xconnect l2_xc(*east_itf, *west_itf);
186 OM::commit(key, l2_xc);
192 l2_xconnect::event_handler::order() const
194 return (dependency_t::BINDING);
198 l2_xconnect::event_handler::show(std::ostream& os)
205 * fd.io coding-style-patch-verification: ON
208 * eval: (c-set-style "mozilla")