VOM: ACL: Add Object Model for acl ethertype
[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                              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& old)
150 {
151   /*
152    * always update the instance with the latest rules
153    */
154   if (!m_binding || old.m_le != m_le) {
155     HW::enqueue(
156       new acl_ethertype_cmds::bind_cmd(m_binding, m_itf->handle(), m_le));
157   }
158 }
159
160 std::shared_ptr<acl_ethertype>
161 acl_ethertype::find_or_add(const acl_ethertype& temp)
162 {
163   return (m_db.find_or_add(temp.m_itf->key(), temp));
164 }
165
166 std::shared_ptr<acl_ethertype>
167 acl_ethertype::singular() const
168 {
169   return find_or_add(*this);
170 }
171
172 acl_ethertype::event_handler::event_handler()
173 {
174   OM::register_listener(this);
175   inspect::register_handler({ "acl-ethertype" }, "ACL Ethertype bindings",
176                             this);
177 }
178
179 void
180 acl_ethertype::event_handler::handle_replay()
181 {
182   m_db.replay();
183 }
184
185 void
186 acl_ethertype::event_handler::handle_populate(const client_db::key_t& key)
187 {
188   // FIXME
189 }
190
191 dependency_t
192 acl_ethertype::event_handler::order() const
193 {
194   return (dependency_t::BINDING);
195 }
196
197 void
198 acl_ethertype::event_handler::show(std::ostream& os)
199 {
200   m_db.dump(os);
201 }
202 };
203 };
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "mozilla")
209  * End:
210  */