VOM: support for pipes
[vpp.git] / extras / vom / vom / pipe.hpp
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 #ifndef __VOM_PIPE_H__
17 #define __VOM_PIPE_H__
18
19 #include "vom/interface.hpp"
20
21 namespace VOM {
22 /**
23  * A Pipe interface.
24  * A pipe is composed for 3 'interfaces'.
25  *  1) the 'parent' interface - this is used as the 'key' for the pipe
26  *  2) the two 'ends' of the pipe - these are used to RX/TX packets
27  *     form/to. The ends are retreivable using the east()/west() functions.
28  *     The east and west end are exactly equivalent, they are merely
29  *     named differently for logical purposes.
30  */
31 class pipe : public interface
32 {
33 public:
34   typedef std::pair<handle_t, handle_t> handle_pair_t;
35
36   /**
37    * Construct a new object matching the desried state
38    */
39   pipe(uint32_t instance, admin_state_t state);
40
41   /**
42    * Destructor
43    */
44   ~pipe();
45
46   /**
47    * Copy Constructor
48    */
49   pipe(const pipe& o);
50
51   /**
52    * comparison operator - for UT
53    */
54   bool operator==(const pipe& s) const;
55
56   /**
57    * Return the matching 'singular instance' of the sub-interface
58    */
59   std::shared_ptr<pipe> singular() const;
60
61   /**
62    * Find a subinterface from its key
63    */
64   static std::shared_ptr<pipe> find(const key_t& k);
65
66   /**
67    * The interface that is the east end of the pipe
68    */
69   std::shared_ptr<interface> east();
70
71   /**
72    * The interface that is the west end of the pipe.
73    * The east and west end are exactly equivalent, they are merely
74    * named differently for logical purposes.
75    */
76   std::shared_ptr<interface> west();
77
78   virtual std::string to_string(void) const;
79
80   void set_ends(const handle_pair_t& p);
81
82 private:
83   /**
84    * The interface type that forms the ends of the pipe
85    */
86   class pipe_end : public interface
87   {
88   public:
89     pipe_end(const pipe& p, uint8_t id);
90
91   private:
92     virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
93     virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
94
95     std::shared_ptr<pipe> m_pipe;
96   };
97
98   /**
99 *Class definition for listeners to OM events
100 */
101   class event_handler : public OM::listener, public inspect::command_handler
102   {
103   public:
104     event_handler();
105     virtual ~event_handler() = default;
106
107     /**
108      * Handle a populate event
109      */
110     void handle_populate(const client_db::key_t& key);
111
112     /**
113      * Handle a replay event
114      */
115     void handle_replay();
116
117     /**
118      * Show the object in the Singular DB
119      */
120     void show(std::ostream& os);
121
122     /**
123      * Get the sortable Id of the listener
124      */
125     dependency_t order() const;
126   };
127   static event_handler m_evh;
128
129   /**
130    * Return the matching 'instance' of the pipe
131    *  over-ride from the base class
132    */
133   std::shared_ptr<interface> singular_i() const;
134
135   /**
136    * Virtual functions to construct an interface create commands.
137    */
138   virtual std::queue<cmd*>& mk_create_cmd(std::queue<cmd*>& cmds);
139
140   /**
141    * Virtual functions to construct an interface delete commands.
142    */
143   virtual std::queue<cmd*>& mk_delete_cmd(std::queue<cmd*>& cmds);
144
145   /**
146    * the handles that are set during the create command
147    */
148   HW::item<handle_pair_t> m_hdl_pair;
149
150   /**
151    * The ends of the pipe
152    */
153   std::shared_ptr<interface> m_ends[2];
154
155   /**
156    * Instance number
157    */
158   uint32_t m_instance;
159 };
160
161 }; // namespace VOM
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "mozilla")
168  * End:
169  */
170
171 #endif