VOM: acl: Some necessary fixes
[vpp.git] / src / vpp-api / vom / acl_list_cmds.hpp
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 #ifndef __VOM_ACL_LIST_CMDS_H__
17 #define __VOM_ACL_LIST_CMDS_H__
18
19 #include "vom/acl_list.hpp"
20 #include "vom/dump_cmd.hpp"
21 #include "vom/rpc_cmd.hpp"
22
23 #include <vapi/acl.api.vapi.hpp>
24
25 namespace VOM {
26 namespace ACL {
27 namespace list_cmds {
28 /**
29  * A command class that Create the list
30  */
31 template <typename RULE, typename UPDATE>
32 class update_cmd
33   : public rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, UPDATE>
34 {
35 public:
36   typedef typename list<RULE>::rules_t cmd_rules_t;
37   typedef typename list<RULE>::key_t cmd_key_t;
38
39   /**
40    * Constructor
41    */
42   update_cmd(HW::item<handle_t>& item,
43              const cmd_key_t& key,
44              const cmd_rules_t& rules)
45     : rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, UPDATE>(item)
46     , m_key(key)
47     , m_rules(rules)
48   {
49   }
50
51   /**
52    * Issue the command to VPP/HW
53    */
54   rc_t issue(connection& con);
55
56   /**
57    * convert to string format for debug purposes
58    */
59   std::string to_string() const
60   {
61     std::ostringstream s;
62     s << "ACL-list-update:[ " << this->item().to_string() << " rule-list:[";
63     for (auto rule : m_rules) {
64       s << " " << rule.to_string();
65     }
66     s << "]]";
67
68     return (s.str());
69   }
70
71   /**
72    * Comparison operator - only used for UT
73    */
74   bool operator==(const update_cmd& other) const
75   {
76     return ((m_key == other.m_key) && (m_rules == other.m_rules));
77   }
78
79   void complete()
80   {
81     std::shared_ptr<list<RULE>> sp = list<RULE>::find(m_key);
82     if (sp && this->item()) {
83       list<RULE>::add(this->item().data(), sp);
84     }
85   }
86
87   void succeeded()
88   {
89     rpc_cmd<HW::item<handle_t>, HW::item<handle_t>, UPDATE>::succeeded();
90     complete();
91   }
92
93   /**
94    * A callback function for handling ACL creates
95    */
96   virtual vapi_error_e operator()(UPDATE& reply)
97   {
98     int acl_index = reply.get_response().get_payload().acl_index;
99     int retval = reply.get_response().get_payload().retval;
100
101     VOM_LOG(log_level_t::DEBUG) << this->to_string() << " " << retval;
102
103     HW::item<handle_t> res(acl_index, rc_t::from_vpp_retval(retval));
104
105     this->fulfill(res);
106
107     return (VAPI_OK);
108   }
109
110 private:
111   /**
112    * The key.
113    */
114   const cmd_key_t& m_key;
115
116   /**
117    * The rules
118    */
119   const cmd_rules_t& m_rules;
120 };
121
122 /**
123  * A cmd class that Deletes an ACL
124  */
125 template <typename DELETE>
126 class delete_cmd : public rpc_cmd<HW::item<handle_t>, rc_t, DELETE>
127 {
128 public:
129   /**
130    * Constructor
131    */
132   delete_cmd(HW::item<handle_t>& item)
133     : rpc_cmd<HW::item<handle_t>, rc_t, DELETE>(item)
134   {
135   }
136
137   /**
138    * Issue the command to VPP/HW
139    */
140   rc_t issue(connection& con) { return (rc_t::INVALID); }
141
142   /**
143    * convert to string format for debug purposes
144    */
145   std::string to_string() const
146   {
147     std::ostringstream s;
148     s << "ACL-list-delete: " << this->item().to_string();
149
150     return (s.str());
151   }
152
153   /**
154    * Comparison operator - only used for UT
155    */
156   bool operator==(const delete_cmd& other) const
157   {
158     return (this->item().data() == other.item().data());
159   }
160 };
161
162 /**
163  * A cmd class that Dumps all the ACLs
164  */
165 template <typename DUMP>
166 class dump_cmd : public VOM::dump_cmd<DUMP>
167 {
168 public:
169   /**
170    * Constructor
171    */
172   dump_cmd() = default;
173   dump_cmd(const dump_cmd& d) = default;
174
175   /**
176    * Issue the command to VPP/HW
177    */
178   rc_t issue(connection& con) { return rc_t::INVALID; }
179
180   /**
181    * convert to string format for debug purposes
182    */
183   std::string to_string() const { return ("acl-list-dump"); }
184
185 private:
186   /**
187    * HW reutrn code
188    */
189   HW::item<bool> item;
190 };
191
192 /**
193  * Typedef the L3 ACL commands
194  */
195 typedef update_cmd<l3_rule, vapi::Acl_add_replace> l3_update_cmd;
196 typedef delete_cmd<vapi::Acl_del> l3_delete_cmd;
197 typedef dump_cmd<vapi::Acl_dump> l3_dump_cmd;
198
199 /**
200  * Typedef the L2 ACL commands
201  */
202 typedef update_cmd<l2_rule, vapi::Macip_acl_add> l2_update_cmd;
203 typedef delete_cmd<vapi::Macip_acl_del> l2_delete_cmd;
204 typedef dump_cmd<vapi::Macip_acl_dump> l2_dump_cmd;
205
206 }; // namespace list_cmds
207 }; // namespace ACL
208 }; // namespace VOM
209
210 /*
211  * fd.io coding-style-patch-verification: ON
212  *
213  * Local Variables:
214  * eval: (c-set-style "mozilla")
215  * End:
216  */
217
218 #endif