vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / extras / vom / vom / l3_binding.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/l3_binding.hpp"
17 #include "vom/l3_binding_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21 singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db;
22
23 l3_binding::event_handler l3_binding::m_evh;
24
25 /**
26  * Construct a new object matching the desried state
27  */
28 l3_binding::l3_binding(const interface& itf, const route::prefix_t& pfx)
29   : m_itf(itf.singular())
30   , m_pfx(pfx)
31   , m_binding(true, rc_t::NOOP)
32 {
33 }
34
35 l3_binding::l3_binding(const l3_binding& o)
36   : m_itf(o.m_itf)
37   , m_pfx(o.m_pfx)
38   , m_binding(o.m_binding)
39 {
40 }
41
42 l3_binding::~l3_binding()
43 {
44   sweep();
45
46   // not in the DB anymore.
47   m_db.release(key(), this);
48 }
49
50 bool
51 l3_binding::operator==(const l3_binding& l) const
52 {
53   return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf));
54 }
55
56 const l3_binding::key_t
57 l3_binding::key() const
58 {
59   return (make_pair(m_itf->key(), m_pfx));
60 }
61
62 void
63 l3_binding::sweep()
64 {
65   if (m_binding) {
66     HW::enqueue(
67       new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
68   }
69   HW::write();
70 }
71
72 void
73 l3_binding::replay()
74 {
75   if (m_binding) {
76     HW::enqueue(
77       new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
78   }
79 }
80
81 const route::prefix_t&
82 l3_binding::prefix() const
83 {
84   return (m_pfx);
85 }
86
87 const interface&
88 l3_binding::itf() const
89 {
90   return (*m_itf);
91 }
92
93 l3_binding::const_iterator_t
94 l3_binding::cbegin()
95 {
96   return m_db.begin();
97 }
98
99 l3_binding::const_iterator_t
100 l3_binding::cend()
101 {
102   return m_db.end();
103 }
104
105 std::string
106 l3_binding::to_string() const
107 {
108   std::ostringstream s;
109   s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
110     << " " << m_binding.to_string() << "]";
111
112   return (s.str());
113 }
114
115 void
116 l3_binding::update(const l3_binding& desired)
117 {
118   /*
119    * no updates for the binding. chaning the interface or the prefix is a change
120    * to the
121    * key, hence a new object
122    */
123   if (!m_binding) {
124     HW::enqueue(
125       new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
126   }
127 }
128
129 std::shared_ptr<l3_binding>
130 l3_binding::find_or_add(const l3_binding& temp)
131 {
132   return (m_db.find_or_add(temp.key(), temp));
133 }
134
135 std::shared_ptr<l3_binding>
136 l3_binding::find(const key_t& k)
137 {
138   return (m_db.find(k));
139 }
140
141 std::shared_ptr<l3_binding>
142 l3_binding::singular() const
143 {
144   return find_or_add(*this);
145 }
146
147 void
148 l3_binding::dump(std::ostream& os)
149 {
150   db_dump(m_db, os);
151 }
152
153 std::ostream&
154 operator<<(std::ostream& os, const l3_binding::key_t& key)
155 {
156   os << "[" << key.first << ", " << key.second << "]";
157
158   return (os);
159 }
160
161 l3_binding::event_handler::event_handler()
162 {
163   OM::register_listener(this);
164   inspect::register_handler({ "l3" }, "L3 bindings", this);
165 }
166
167 void
168 l3_binding::event_handler::handle_replay()
169 {
170   m_db.replay();
171 }
172
173 void
174 l3_binding::event_handler::handle_populate(const client_db::key_t& key)
175 {
176   /**
177    * This is done while populating the interfaces
178    */
179 }
180
181 dependency_t
182 l3_binding::event_handler::order() const
183 {
184   return (dependency_t::BINDING);
185 }
186
187 void
188 l3_binding::event_handler::show(std::ostream& os)
189 {
190   db_dump(m_db, os);
191 }
192 }
193
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "mozilla")
199  * End:
200  */