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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_ACL_LIST_H__
17 #define __VOM_ACL_LIST_H__
21 #include "vom/acl_l2_rule.hpp"
22 #include "vom/acl_l3_rule.hpp"
23 #include "vom/acl_types.hpp"
25 #include "vom/inspect.hpp"
27 #include "vom/singular_db.hpp"
32 * An ACL list comprises a set of match actions rules to be applied to
34 * A list is bound to a given interface.
36 template <typename RULE>
37 class list : public object_base
41 * The KEY can be used to uniquely identify the ACL.
42 * (other choices for keys, like the summation of the properties
43 * of the rules, are rather too cumbersome to use
45 typedef std::string key_t;
48 * The rule container type
50 typedef std::multiset<RULE> rules_t;
53 * Construct a new object matching the desried state
55 list(const key_t& key)
56 : m_hdl(handle_t::INVALID)
61 list(const handle_t& hdl, const key_t& key)
67 list(const key_t& key, const rules_t& rules)
68 : m_hdl(handle_t::INVALID)
90 m_db.release(m_key, this);
94 * Return the 'sigular instance' of the ACL that matches this object
96 std::shared_ptr<list> singular() const { return find_or_add(*this); }
99 * Dump all ACLs into the stream provided
101 static void dump(std::ostream& os) { m_db.dump(os); }
104 * convert to string format for debug purposes
106 std::string to_string() const
108 std::ostringstream s;
109 s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
111 for (auto rule : m_rules) {
112 s << rule.to_string() << " ";
121 * Insert priority sorted a rule into the list
123 void insert(const RULE& rule) { m_rules.insert(rule); }
126 * Remove a rule from the list
128 void remove(const RULE& rule) { m_rules.erase(rule); }
131 * Return the VPP assign handle
133 const handle_t& handle() const { return (singular()->handle_i()); }
135 static std::shared_ptr<list> find(const handle_t& handle)
137 return (m_hdl_db[handle].lock());
140 static std::shared_ptr<list> find(const key_t& key)
142 return (m_db.find(key));
145 static void add(const key_t& key, const HW::item<handle_t>& item)
147 std::shared_ptr<list> sp = find(key);
150 m_hdl_db[item.data()] = sp;
154 static void remove(const HW::item<handle_t>& item)
156 m_hdl_db.erase(item.data());
159 const key_t& key() const { return m_key; }
161 const rules_t& rules() const { return m_rules; }
164 * Comparison operator - for UT
166 bool operator==(const list& l) const
168 return (key() == l.key() && rules() == l.rules());
173 * Class definition for listeners to OM events
175 class event_handler : public OM::listener, public inspect::command_handler
180 virtual ~event_handler() = default;
183 * Handle a populate event
185 void handle_populate(const client_db::key_t& key);
188 * Handle a replay event
190 void handle_replay() { m_db.replay(); }
193 * Show the object in the Singular DB
195 void show(std::ostream& os);
198 * Get the sortable Id of the listener
200 dependency_t order() const { return (dependency_t::ACL); }
204 * event_handler to register with OM
206 static event_handler m_evh;
209 * Enqueue commands to the VPP command Q for the update
211 void update(const list& obj);
216 HW::item<handle_t> m_hdl;
219 * Find or add the sigular instance in the DB
221 static std::shared_ptr<list> find_or_add(const list& temp)
223 return (m_db.find_or_add(temp.key(), temp));
227 * return the acl-list's handle in the singular instance
229 const handle_t& handle_i() const { return (m_hdl.data()); }
232 * It's the VOM::OM class that updates call update
234 friend class VOM::OM;
237 * It's the VOM::singular_db class that calls replay()
239 friend class singular_db<key_t, list>;
242 * Sweep/reap the object if still stale
247 * Replay the objects state to HW
252 * A map of all ACL's against the client's key
254 static singular_db<key_t, list> m_db;
257 * A map of all ACLs keyed against VPP's handle
259 static std::map<handle_t, std::weak_ptr<list>> m_hdl_db;
262 * The Key is a user defined identifer for this ACL
267 * A sorted list of the rules
273 * Typedef the L3 ACL type
275 typedef list<l3_rule> l3_list;
278 * Typedef the L2 ACL type
280 typedef list<l2_rule> l2_list;
283 * Definition of the static singular_db for ACL Lists
285 template <typename RULE>
286 singular_db<typename ACL::list<RULE>::key_t, ACL::list<RULE>> list<RULE>::m_db;
289 * Definition of the static per-handle DB for ACL Lists
291 template <typename RULE>
292 std::map<handle_t, std::weak_ptr<ACL::list<RULE>>> list<RULE>::m_hdl_db;
294 template <typename RULE>
295 typename ACL::list<RULE>::event_handler list<RULE>::m_evh;
300 * fd.io coding-style-patch-verification: ON
303 * eval: (c-set-style "mozilla")