VOM: acl: Some necessary fixes
[vpp.git] / src / vpp-api / vom / acl_ethertype.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/acl_ethertype.hpp"
17 #include "vom/acl_ethertype_cmds.hpp"
18
19 namespace VOM {
20 namespace ACL {
21
22 ethertype_rule_t::ethertype_rule_t(const ethertype_t& eth,
23                                    const direction_t& dir)
24   : m_eth(eth)
25   , m_dir(dir)
26 {
27 }
28
29 std::string
30 ethertype_rule_t::to_string() const
31 {
32   std::ostringstream s;
33
34   s << "["
35     << "ethertype:" << m_eth.to_string() << " dir:" << m_dir.to_string()
36     << "],";
37
38   return (s.str());
39 }
40
41 bool
42 ethertype_rule_t::operator<(const ethertype_rule_t& other) const
43 {
44   return (m_dir > other.m_dir);
45 }
46
47 bool
48 ethertype_rule_t::operator==(const ethertype_rule_t& other) const
49 {
50   return (m_dir == other.m_dir && m_eth == other.m_eth);
51 }
52
53 uint16_t
54 ethertype_rule_t::getEthertype() const
55 {
56   return m_eth.value();
57 }
58
59 const direction_t&
60 ethertype_rule_t::getDirection() const
61 {
62   return m_dir;
63 }
64
65 /**
66  * A DB of all acl ethertype bindings configs
67  */
68 singular_db<interface::key_t, acl_ethertype> acl_ethertype::m_db;
69
70 acl_ethertype::event_handler acl_ethertype::m_evh;
71
72 acl_ethertype::acl_ethertype(const interface& itf,
73                              const acl_ethertype::ethertype_rules_t& le)
74   : m_itf(itf.singular())
75   , m_le(le)
76   , m_binding(true)
77 {
78 }
79
80 acl_ethertype::acl_ethertype(const acl_ethertype& o)
81   : m_itf(o.m_itf)
82   , m_le(o.m_le)
83   , m_binding(o.m_binding)
84 {
85 }
86
87 acl_ethertype::~acl_ethertype()
88 {
89   sweep();
90
91   // not in the DB anymore.
92   m_db.release(m_itf->key(), this);
93 }
94
95 void
96 acl_ethertype::sweep()
97 {
98 }
99
100 const acl_ethertype::key_t&
101 acl_ethertype::key() const
102 {
103   return (m_itf->key());
104 }
105
106 bool
107 acl_ethertype::operator==(const acl_ethertype& other) const
108 {
109   return (m_itf->key() == other.m_itf->key() && m_le == other.m_le);
110 }
111
112 std::shared_ptr<acl_ethertype>
113 acl_ethertype::find(const key_t& key)
114 {
115   return (m_db.find(key));
116 }
117
118 void
119 acl_ethertype::dump(std::ostream& os)
120 {
121   m_db.dump(os);
122 }
123
124 void
125 acl_ethertype::replay()
126 {
127   if (m_binding) {
128     HW::enqueue(
129       new acl_ethertype_cmds::bind_cmd(m_binding, m_itf->handle(), m_le));
130   }
131 }
132
133 std::string
134 acl_ethertype::to_string() const
135 {
136   std::ostringstream s;
137   s << "Acl-Ethertype:" << m_itf->to_string() << " ethertype-rules:";
138   auto it = m_le.cbegin();
139   while (it != m_le.cend()) {
140     s << it->to_string();
141     ++it;
142   }
143   s << " rules-size:" << m_le.size();
144
145   return (s.str());
146 }
147
148 void
149 acl_ethertype::update(const acl_ethertype& desired)
150 {
151   /*
152    * always update the instance with the latest rules
153    */
154   if (!m_binding || desired.m_le != m_le) {
155     HW::enqueue(
156       new acl_ethertype_cmds::bind_cmd(m_binding, m_itf->handle(), m_le));
157   }
158
159   m_le = desired.m_le;
160 }
161
162 std::shared_ptr<acl_ethertype>
163 acl_ethertype::find_or_add(const acl_ethertype& temp)
164 {
165   return (m_db.find_or_add(temp.m_itf->key(), temp));
166 }
167
168 std::shared_ptr<acl_ethertype>
169 acl_ethertype::singular() const
170 {
171   return find_or_add(*this);
172 }
173
174 acl_ethertype::event_handler::event_handler()
175 {
176   OM::register_listener(this);
177   inspect::register_handler({ "acl-ethertype" }, "ACL Ethertype bindings",
178                             this);
179 }
180
181 void
182 acl_ethertype::event_handler::handle_replay()
183 {
184   m_db.replay();
185 }
186
187 void
188 acl_ethertype::event_handler::handle_populate(const client_db::key_t& key)
189 {
190   // FIXME
191 }
192
193 dependency_t
194 acl_ethertype::event_handler::order() const
195 {
196   return (dependency_t::BINDING);
197 }
198
199 void
200 acl_ethertype::event_handler::show(std::ostream& os)
201 {
202   m_db.dump(os);
203 }
204 };
205 };
206 /*
207  * fd.io coding-style-patch-verification: ON
208  *
209  * Local Variables:
210  * eval: (c-set-style "mozilla")
211  * End:
212  */