Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / lldp_binding.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_LLDP_BINDING_H__
17 #define __VOM_LLDP_BINDING_H__
18
19 #include "vom/dump_cmd.hpp"
20 #include "vom/hw.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
24 #include "vom/om.hpp"
25 #include "vom/rpc_cmd.hpp"
26 #include "vom/singular_db.hpp"
27 #include "vom/sub_interface.hpp"
28
29 #include <vapi/lldp.api.vapi.hpp>
30
31 namespace VOM {
32 /**
33  * A representation of LLDP client configuration on an interface
34  */
35 class lldp_binding : public object_base
36 {
37 public:
38   /**
39    * Construct a new object matching the desried state
40    */
41   lldp_binding(const interface& itf, const std::string& hostname);
42
43   /**
44    * Copy Constructor
45    */
46   lldp_binding(const lldp_binding& o);
47   /**
48    * Destructor
49    */
50   ~lldp_binding();
51
52   /**
53    * Return the 'singular' of the LLDP binding that matches this object
54    */
55   std::shared_ptr<lldp_binding> singular() const;
56
57   /**
58    * convert to string format for debug purposes
59    */
60   std::string to_string() const;
61
62   /**
63    * Dump all LLDP bindings into the stream provided
64    */
65   static void dump(std::ostream& os);
66
67   /**
68    * A command class that binds the LLDP config to the interface
69    */
70   class bind_cmd
71     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_set_lldp>
72   {
73   public:
74     /**
75      * Constructor
76      */
77     bind_cmd(HW::item<bool>& item,
78              const handle_t& itf,
79              const std::string& port_desc);
80
81     /**
82      * Issue the command to VPP/HW
83      */
84     rc_t issue(connection& con);
85     /**
86      * convert to string format for debug purposes
87      */
88     std::string to_string() const;
89
90     /**
91      * Comparison operator - only used for UT
92      */
93     bool operator==(const bind_cmd& i) const;
94
95   private:
96     /**
97      * Reference to the HW::item of the interface to bind
98      */
99     const handle_t& m_itf;
100
101     /**
102      * The LLDP client's hostname
103      */
104     const std::string m_port_desc;
105   };
106
107   /**
108    * A cmd class that Unbinds Lldp Config from an interface
109    */
110   class unbind_cmd
111     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Sw_interface_set_lldp>
112   {
113   public:
114     /**
115      * Constructor
116      */
117     unbind_cmd(HW::item<bool>& item, const handle_t& itf);
118
119     /**
120      * Issue the command to VPP/HW
121      */
122     rc_t issue(connection& con);
123     /**
124      * convert to string format for debug purposes
125      */
126     std::string to_string() const;
127
128     /**
129      * Comparison operator - only used for UT
130      */
131     bool operator==(const unbind_cmd& i) const;
132
133   private:
134     /**
135      * Reference to the HW::item of the interface to unbind
136      */
137     const handle_t& m_itf;
138   };
139
140 private:
141   /**
142    * Class definition for listeners to OM events
143    */
144   class event_handler : public OM::listener, public inspect::command_handler
145   {
146   public:
147     event_handler();
148     virtual ~event_handler() = default;
149
150     /**
151      * Handle a populate event
152      */
153     void handle_populate(const client_db::key_t& key);
154
155     /**
156      * Handle a replay event
157      */
158     void handle_replay();
159
160     /**
161      * Show the object in the Singular DB
162      */
163     void show(std::ostream& os);
164
165     /**
166      * Get the sortable Id of the listener
167      */
168     dependency_t order() const;
169   };
170
171   /**
172    * event_handler to register with OM
173    */
174   static event_handler m_evh;
175
176   /**
177    * Enquue commonds to the VPP command Q for the update
178    */
179   void update(const lldp_binding& obj);
180
181   /**
182    * Find or add LLDP binding to the OM
183    */
184   static std::shared_ptr<lldp_binding> find_or_add(const lldp_binding& temp);
185
186   /*
187    * It's the OM class that calls singular()
188    */
189   friend class OM;
190
191   /**
192    * It's the singular_db class that calls replay()
193    */
194   friend class singular_db<interface::key_type, lldp_binding>;
195
196   /**
197    * Sweep/reap the object if still stale
198    */
199   void sweep(void);
200
201   /**
202    * replay the object to create it in hardware
203    */
204   void replay(void);
205
206   /**
207    * A reference counting pointer to the interface on which LLDP config
208    * resides. By holding the reference here, we can guarantee that
209    * this object will outlive the interface
210    */
211   const std::shared_ptr<interface> m_itf;
212
213   /**
214    * The port-description in the LLDP configuration
215    */
216   const std::string m_port_desc;
217
218   /**
219    * HW configuration for the binding. The bool representing the
220    * do/don't bind.
221    */
222   HW::item<bool> m_binding;
223
224   /**
225    * A map of all Lldp bindings keyed against the interface.
226    */
227   static singular_db<interface::key_type, lldp_binding> m_db;
228 };
229 };
230
231 /*
232  * fd.io coding-style-patch-verification: ON
233  *
234  * Local Variables:
235  * eval: (c-set-style "mozilla")
236  * End:
237  */
238
239 #endif