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