VOM reshuffle
[vpp.git] / src / vpp-api / vom / bridge_domain_entry.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/bridge_domain_entry.hpp"
17 #include "vom/bridge_domain_entry_cmds.hpp"
18
19 namespace VOM {
20 singular_db<bridge_domain_entry::key_t, bridge_domain_entry>
21   bridge_domain_entry::m_db;
22
23 bridge_domain_entry::event_handler bridge_domain_entry::m_evh;
24
25 bridge_domain_entry::bridge_domain_entry(const bridge_domain& bd,
26                                          const mac_address_t& mac,
27                                          const interface& tx_itf)
28   : m_hw(false)
29   , m_mac(mac)
30   , m_bd(bd.singular())
31   , m_tx_itf(tx_itf.singular())
32 {
33 }
34
35 bridge_domain_entry::bridge_domain_entry(const mac_address_t& mac,
36                                          const interface& tx_itf)
37   : m_hw(false)
38   , m_mac(mac)
39   , m_bd(nullptr)
40   , m_tx_itf(tx_itf.singular())
41 {
42   /*
43    * the entry goes in the default bridge-domain
44    */
45   bridge_domain bd(bridge_domain::DEFAULT_TABLE);
46
47   m_bd = bd.singular();
48 }
49
50 bridge_domain_entry::bridge_domain_entry(const bridge_domain_entry& bde)
51   : m_hw(bde.m_hw)
52   , m_mac(bde.m_mac)
53   , m_bd(bde.m_bd)
54   , m_tx_itf(bde.m_tx_itf)
55 {
56 }
57
58 bridge_domain_entry::~bridge_domain_entry()
59 {
60   sweep();
61
62   // not in the DB anymore.
63   m_db.release(std::make_pair(m_bd->id(), m_mac), this);
64 }
65
66 void
67 bridge_domain_entry::sweep()
68 {
69   if (m_hw) {
70     HW::enqueue(
71       new bridge_domain_entry_cmds::delete_cmd(m_hw, m_mac, m_bd->id()));
72   }
73   HW::write();
74 }
75
76 void
77 bridge_domain_entry::replay()
78 {
79   if (m_hw) {
80     HW::enqueue(new bridge_domain_entry_cmds::create_cmd(
81       m_hw, m_mac, m_bd->id(), m_tx_itf->handle()));
82   }
83 }
84 std::string
85 bridge_domain_entry::to_string() const
86 {
87   std::ostringstream s;
88   s << "bridge-domain-entry:[" << m_bd->to_string() << ", " << m_mac.to_string()
89     << ", tx:" << m_tx_itf->name() << "]";
90
91   return (s.str());
92 }
93
94 void
95 bridge_domain_entry::update(const bridge_domain_entry& r)
96 {
97   /*
98    * create the table if it is not yet created
99    */
100   if (rc_t::OK != m_hw.rc()) {
101     HW::enqueue(new bridge_domain_entry_cmds::create_cmd(
102       m_hw, m_mac, m_bd->id(), m_tx_itf->handle()));
103   }
104 }
105
106 std::shared_ptr<bridge_domain_entry>
107 bridge_domain_entry::find_or_add(const bridge_domain_entry& temp)
108 {
109   return (m_db.find_or_add(std::make_pair(temp.m_bd->id(), temp.m_mac), temp));
110 }
111
112 std::shared_ptr<bridge_domain_entry>
113 bridge_domain_entry::singular() const
114 {
115   return find_or_add(*this);
116 }
117
118 void
119 bridge_domain_entry::dump(std::ostream& os)
120 {
121   m_db.dump(os);
122 }
123
124 bridge_domain_entry::event_handler::event_handler()
125 {
126   OM::register_listener(this);
127   inspect::register_handler({ "bd-entry" },
128                             "bridge domain entry configurations", this);
129 }
130
131 void
132 bridge_domain_entry::event_handler::handle_replay()
133 {
134   m_db.replay();
135 }
136
137 void
138 bridge_domain_entry::event_handler::handle_populate(const client_db::key_t& key)
139 {
140   std::shared_ptr<bridge_domain_entry_cmds::dump_cmd> cmd(
141     new bridge_domain_entry_cmds::dump_cmd());
142
143   HW::enqueue(cmd);
144   HW::write();
145
146   for (auto& record : *cmd) {
147     auto& payload = record.get_payload();
148
149     std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
150     std::shared_ptr<bridge_domain> bd = bridge_domain::find(payload.bd_id);
151     mac_address_t mac(payload.mac);
152     bridge_domain_entry bd_e(*bd, mac, *itf);
153
154     VOM_LOG(log_level_t::DEBUG) << "bd-entry-dump: " << bd->to_string()
155                                 << mac.to_string() << itf->to_string();
156
157     /*
158      * Write each of the discovered interfaces into the OM,
159      * but disable the HW Command q whilst we do, so that no
160      * commands are sent to VPP
161      */
162     OM::commit(key, bd_e);
163   }
164 }
165
166 dependency_t
167 bridge_domain_entry::event_handler::order() const
168 {
169   return (dependency_t::ENTRY);
170 }
171
172 void
173 bridge_domain_entry::event_handler::show(std::ostream& os)
174 {
175   m_db.dump(os);
176 }
177
178 std::ostream&
179 operator<<(std::ostream& os, const bridge_domain_entry::key_t& key)
180 {
181   os << "[" << key.first << ", " << key.second.to_string() << "]";
182
183   return (os);
184 }
185 }
186
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "mozilla")
192  * End:
193  */