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