VOM: support for pipes
[vpp.git] / extras / vom / vom / pipe.cpp
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 #include "vom/pipe.hpp"
17 #include "vom/interface_factory.hpp"
18 #include "vom/pipe_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 typedef enum end_t_ {
24   EAST = 0,
25   WEST,
26 } end_t;
27 #define N_ENDS (WEST + 1)
28
29 pipe::event_handler pipe::m_evh;
30
31 static const std::string
32 pipe_mk_name(uint32_t instance)
33 {
34   return ("pipe" + std::to_string(instance));
35 }
36
37 /**
38  * Construct a new object matching the desried state
39  */
40 pipe::pipe(uint32_t instance, admin_state_t state)
41   : interface(pipe_mk_name(instance), type_t::PIPE, state)
42   , m_instance(instance)
43 {
44 }
45
46 pipe::~pipe()
47 {
48   sweep();
49   release();
50 }
51
52 pipe::pipe(const pipe& o)
53   : interface(o)
54   , m_instance(o.m_instance)
55 {
56 }
57
58 std::string
59 pipe::to_string(void) const
60 {
61   std::ostringstream s;
62
63   s << "[pipe: " << interface::to_string() << " instance:" << m_instance
64     << " ends:[" << m_hdl_pair.rc().to_string() << " "
65     << m_hdl_pair.data().first << ", " << m_hdl_pair.data().second << "]]";
66
67   return (s.str());
68 }
69
70 std::queue<cmd*>&
71 pipe::mk_create_cmd(std::queue<cmd*>& q)
72 {
73   q.push(new pipe_cmds::create_cmd(m_hdl, m_name, m_instance, m_hdl_pair));
74
75   return (q);
76 }
77
78 std::queue<cmd*>&
79 pipe::mk_delete_cmd(std::queue<cmd*>& q)
80 {
81   q.push(new pipe_cmds::delete_cmd(m_hdl, m_hdl_pair));
82
83   return (q);
84 }
85
86 std::shared_ptr<pipe>
87 pipe::singular() const
88 {
89   return std::dynamic_pointer_cast<pipe>(singular_i());
90 }
91
92 std::shared_ptr<interface>
93 pipe::singular_i() const
94 {
95   return m_db.find_or_add(key(), *this);
96 }
97
98 std::shared_ptr<pipe>
99 pipe::find(const key_t& k)
100 {
101   return std::dynamic_pointer_cast<pipe>(m_db.find(k));
102 }
103
104 std::shared_ptr<interface>
105 pipe::west()
106 {
107   if (!m_ends[WEST]) {
108     if (rc_t::OK == m_hdl_pair.rc()) {
109       m_ends[WEST] = pipe_end(*this, WEST).singular();
110       m_ends[WEST]->set(m_hdl_pair.data().first);
111     }
112   }
113
114   return (m_ends[WEST]);
115 }
116
117 std::shared_ptr<interface>
118 pipe::east()
119 {
120   if (!m_ends[EAST]) {
121     if (rc_t::OK == m_hdl_pair.rc()) {
122       m_ends[EAST] = pipe_end(*this, EAST).singular();
123       m_ends[EAST]->set(m_hdl_pair.data().first);
124     }
125   }
126
127   return (m_ends[EAST]);
128 }
129
130 pipe::pipe_end::pipe_end(const pipe& p, uint8_t id)
131   : interface(p.name() + "." + std::to_string(id),
132               interface::type_t::PIPE_END,
133               interface::admin_state_t::UP)
134   , m_pipe(p.singular())
135 {
136 }
137
138 std::queue<cmd*>&
139 pipe::pipe_end::mk_create_cmd(std::queue<cmd*>& q)
140 {
141   return (q);
142 }
143
144 std::queue<cmd*>&
145 pipe::pipe_end::mk_delete_cmd(std::queue<cmd*>& q)
146 {
147   return (q);
148 }
149
150 void
151 pipe::set_ends(const handle_pair_t& p)
152 {
153   if (handle_t::INVALID != p.first && handle_t::INVALID != p.second) {
154     m_hdl_pair = { p, rc_t::OK };
155   } else {
156     m_hdl_pair = { p, rc_t::INVALID };
157   }
158 }
159
160 pipe::event_handler::event_handler()
161 {
162   OM::register_listener(this);
163   inspect::register_handler({ "pipe" }, "pipes", this);
164 }
165
166 void
167 pipe::event_handler::handle_replay()
168 {
169   // m_db.replay();
170 }
171
172 void
173 pipe::event_handler::handle_populate(const client_db::key_t& key)
174 {
175   std::shared_ptr<pipe_cmds::dump_cmd> cmd =
176     std::make_shared<pipe_cmds::dump_cmd>();
177
178   HW::enqueue(cmd);
179   HW::write();
180
181   for (auto& record : *cmd) {
182     std::shared_ptr<pipe> sp;
183
184     sp = interface_factory::new_pipe_interface(record.get_payload());
185
186     VOM_LOG(log_level_t::DEBUG) << " pipe-dump: " << sp->to_string();
187     OM::commit(key, *sp);
188   }
189 }
190
191 dependency_t
192 pipe::event_handler::order() const
193 {
194   return (dependency_t::VIRTUAL_INTERFACE);
195 }
196
197 void
198 pipe::event_handler::show(std::ostream& os)
199 {
200   db_dump(m_db, os);
201 }
202
203 }; // namespace VOM
204
205 /*
206  * fd.io coding-style-patch-verification: ON
207  *
208  * Local Variables:
209  * eval: (c-set-style "mozilla")
210  * End:
211  */