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