c72b5f2e8b0fa43efdcf7ba71d5500ffa5504c37
[vpp.git] / src / vpp-api / vom / arp_proxy_config.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/arp_proxy_config.hpp"
17 #include "vom/arp_proxy_config_cmds.hpp"
18
19 namespace VOM {
20 singular_db<arp_proxy_config::key_t, arp_proxy_config> arp_proxy_config::m_db;
21
22 arp_proxy_config::event_handler arp_proxy_config::m_evh;
23
24 arp_proxy_config::arp_proxy_config(const boost::asio::ip::address_v4& low,
25                                    const boost::asio::ip::address_v4& high)
26   : m_low(low)
27   , m_high(high)
28   , m_config(true)
29 {
30 }
31
32 arp_proxy_config::arp_proxy_config(const arp_proxy_config& o)
33   : m_low(o.m_low)
34   , m_high(o.m_high)
35   , m_config(o.m_config)
36 {
37 }
38
39 arp_proxy_config::~arp_proxy_config()
40 {
41   sweep();
42
43   // not in the DB anymore.
44   m_db.release(std::make_pair(m_low, m_high), this);
45 }
46
47 void
48 arp_proxy_config::sweep()
49 {
50   if (m_config) {
51     HW::enqueue(
52       new arp_proxy_config_cmds::unconfig_cmd(m_config, m_low, m_high));
53   }
54   HW::write();
55 }
56
57 void
58 arp_proxy_config::dump(std::ostream& os)
59 {
60   m_db.dump(os);
61 }
62
63 void
64 arp_proxy_config::replay()
65 {
66   if (m_config) {
67     HW::enqueue(new arp_proxy_config_cmds::config_cmd(m_config, m_low, m_high));
68   }
69 }
70
71 std::string
72 arp_proxy_config::to_string() const
73 {
74   std::ostringstream s;
75   s << "ARP-proxy:"
76     << " low:" << m_low.to_string() << " high:" << m_high.to_string();
77
78   return (s.str());
79 }
80
81 void
82 arp_proxy_config::update(const arp_proxy_config& desired)
83 {
84   if (!m_config) {
85     HW::enqueue(new arp_proxy_config_cmds::config_cmd(m_config, m_low, m_high));
86   }
87 }
88
89 std::shared_ptr<arp_proxy_config>
90 arp_proxy_config::find_or_add(const arp_proxy_config& temp)
91 {
92   return (m_db.find_or_add(std::make_pair(temp.m_low, temp.m_high), temp));
93 }
94
95 std::shared_ptr<arp_proxy_config>
96 arp_proxy_config::singular() const
97 {
98   return find_or_add(*this);
99 }
100
101 arp_proxy_config::event_handler::event_handler()
102 {
103   OM::register_listener(this);
104   inspect::register_handler({ "arp-proxy-config" }, "ARP Proxy configurations",
105                             this);
106 }
107
108 void
109 arp_proxy_config::event_handler::handle_replay()
110 {
111   m_db.replay();
112 }
113
114 void
115 arp_proxy_config::event_handler::handle_populate(const client_db::key_t& key)
116 {
117   // VPP provides no dump for ARP proxy.
118 }
119
120 dependency_t
121 arp_proxy_config::event_handler::order() const
122 {
123   return (dependency_t::GLOBAL);
124 }
125
126 void
127 arp_proxy_config::event_handler::show(std::ostream& os)
128 {
129   m_db.dump(os);
130 }
131
132 std::ostream&
133 operator<<(std::ostream& os, const arp_proxy_config::key_t& key)
134 {
135   os << "[" << key.first << ", " << key.second << "]";
136
137   return (os);
138 }
139 }
140 /*
141  * fd.io coding-style-patch-verification: ON
142  *
143  * Local Variables:
144  * eval: (c-set-style "mozilla")
145  * End:
146  */