VOM: handle null for iterator in dump cmd
[vpp.git] / src / vpp-api / vom / dump_cmd.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_DUMP_CMD_H__
17 #define __VOM_DUMP_CMD_H__
18
19 #include <future>
20
21 #include "vom/cmd.hpp"
22 #include "vom/hw.hpp"
23
24 #include <vapi/vapi.hpp>
25
26 namespace VOM {
27 /**
28  * A function type def for calculating a message's size
29  */
30 typedef unsigned int (*get_msg_size_t)(void*);
31
32 /**
33  * A base class for VPP dump commands.
34  * Dump commands are one of the sub-set of command types to VPP. Here the
35  * client
36  * makes a read request on the resource and VPP responds with all the
37  * records.
38  * This command is executed synchronously. Once complete the client can
39  * 'pop'
40  * the records from the command object
41  */
42 template <typename MSG>
43 class dump_cmd : public cmd
44 {
45 public:
46   typedef MSG msg_t;
47   typedef typename MSG::resp_type record_t;
48
49   typedef typename vapi::Result_set<typename MSG::resp_type>::const_iterator
50     const_iterator;
51
52   /**
53    * Default Constructor
54    */
55   dump_cmd()
56     : cmd()
57   {
58   }
59
60   /**
61    * Destructor
62    */
63   virtual ~dump_cmd() {}
64
65   /**
66    * Constant iterator to the start of the records retunred during the dump
67    */
68   const_iterator begin()
69   {
70     /*
71      * m_dump is NULL during client UT when the commands are not issued.
72      */
73     if (!m_dump)
74       return const_iterator();
75     return (m_dump->get_result_set().begin());
76   }
77
78   /**
79    * Constant iterator to the end of the records retunred during the dump
80    */
81   const_iterator end()
82   {
83     if (!m_dump)
84       return const_iterator();
85     return (m_dump->get_result_set().end());
86   }
87
88   /**
89    * Wait for the issue of the command to complete
90    */
91   rc_t wait()
92   {
93     std::future_status status;
94     std::future<rc_t> result;
95
96     result = m_promise.get_future();
97     status = result.wait_for(std::chrono::seconds(5));
98
99     if (status != std::future_status::ready) {
100       return (rc_t::TIMEOUT);
101     }
102
103     return (result.get());
104   }
105
106   /**
107    * Call operator called when the dump is complete
108    */
109   vapi_error_e operator()(MSG& d)
110   {
111     m_promise.set_value(rc_t::OK);
112
113     return (VAPI_OK);
114   }
115
116   /**
117    * Retire/cancel a long running command
118    */
119   virtual void retire(connection& con) {}
120
121 protected:
122   /**
123    * The underlying promise that implements the synchornous nature
124    * of the command issue
125    */
126   std::promise<rc_t> m_promise;
127
128   /**
129    * Dump commands should not be issued whilst the HW is disabled
130    */
131   void succeeded() {}
132
133   /**
134    * The HW::cmd_q is a friend so it can call suceedded.
135    */
136   friend class HW::cmd_q;
137
138   /**
139    * The VAPI event registration
140    */
141   std::unique_ptr<MSG> m_dump;
142 };
143 };
144
145 /*
146  * fd.io coding-style-patch-verification: ON
147  *
148  * Local Variables:
149  * eval: (c-set-style "mozilla")
150  * End:
151  */
152
153 #endif