1208c29aaSNeale Ranns/*
2208c29aaSNeale Ranns * Copyright (c) 2017 Cisco and/or its affiliates.
3208c29aaSNeale Ranns * Licensed under the Apache License, Version 2.0 (the "License");
4208c29aaSNeale Ranns * you may not use this file except in compliance with the License.
5208c29aaSNeale Ranns * You may obtain a copy of the License at:
6208c29aaSNeale Ranns *
7208c29aaSNeale Ranns *     http://www.apache.org/licenses/LICENSE-2.0
8208c29aaSNeale Ranns *
9208c29aaSNeale Ranns * Unless required by applicable law or agreed to in writing, software
10208c29aaSNeale Ranns * distributed under the License is distributed on an "AS IS" BASIS,
11208c29aaSNeale Ranns * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12208c29aaSNeale Ranns * See the License for the specific language governing permissions and
13208c29aaSNeale Ranns * limitations under the License.
14208c29aaSNeale Ranns */
15208c29aaSNeale Ranns
16208c29aaSNeale Ranns#include "vom/pipe.hpp"
17208c29aaSNeale Ranns#include "vom/interface_factory.hpp"
18208c29aaSNeale Ranns#include "vom/pipe_cmds.hpp"
19208c29aaSNeale Ranns#include "vom/singular_db_funcs.hpp"
20208c29aaSNeale Ranns
21208c29aaSNeale Rannsnamespace VOM {
22208c29aaSNeale Ranns
23208c29aaSNeale Rannstypedef enum end_t_ {
24208c29aaSNeale Ranns  EAST = 0,
25208c29aaSNeale Ranns  WEST,
26208c29aaSNeale Ranns} end_t;
27208c29aaSNeale Ranns#define N_ENDS (WEST + 1)
28208c29aaSNeale Ranns
29208c29aaSNeale Rannspipe::event_handler pipe::m_evh;
30208c29aaSNeale Ranns
31208c29aaSNeale Rannsstatic const std::string
32208c29aaSNeale Rannspipe_mk_name(uint32_t instance)
33208c29aaSNeale Ranns{
34208c29aaSNeale Ranns  return ("pipe" + std::to_string(instance));
35208c29aaSNeale Ranns}
36208c29aaSNeale Ranns
37208c29aaSNeale Ranns/**
38208c29aaSNeale Ranns * Construct a new object matching the desried state
39208c29aaSNeale Ranns */
40208c29aaSNeale Rannspipe::pipe(uint32_t instance, admin_state_t state)
41208c29aaSNeale Ranns  : interface(pipe_mk_name(instance), type_t::PIPE, state)
42208c29aaSNeale Ranns  , m_instance(instance)
43208c29aaSNeale Ranns{
44208c29aaSNeale Ranns}
45208c29aaSNeale Ranns
46208c29aaSNeale Rannspipe::~pipe()
47208c29aaSNeale Ranns{
48208c29aaSNeale Ranns  sweep();
49208c29aaSNeale Ranns  release();
50208c29aaSNeale Ranns}
51208c29aaSNeale Ranns
52208c29aaSNeale Rannspipe::pipe(const pipe& o)
53208c29aaSNeale Ranns  : interface(o)
54208c29aaSNeale Ranns  , m_instance(o.m_instance)
55208c29aaSNeale Ranns{
56208c29aaSNeale Ranns}
57208c29aaSNeale Ranns
58208c29aaSNeale Rannsstd::string
59208c29aaSNeale Rannspipe::to_string(void) const
60208c29aaSNeale Ranns{
61208c29aaSNeale Ranns  std::ostringstream s;
62208c29aaSNeale Ranns
63208c29aaSNeale Ranns  s << "[pipe: " << interface::to_string() << " instance:" << m_instance
64208c29aaSNeale Ranns    << " ends:[" << m_hdl_pair.rc().to_string() << " "
65208c29aaSNeale Ranns    << m_hdl_pair.data().first << ", " << m_hdl_pair.data().second << "]]";
66208c29aaSNeale Ranns
67208c29aaSNeale Ranns  return (s.str());
68208c29aaSNeale Ranns}
69208c29aaSNeale Ranns
70208c29aaSNeale Rannsstd::queue<cmd*>&
71208c29aaSNeale Rannspipe::mk_create_cmd(std::queue<cmd*>& q)
72208c29aaSNeale Ranns{
73208c29aaSNeale Ranns  q.push(new pipe_cmds::create_cmd(m_hdl, m_name, m_instance, m_hdl_pair));
74208c29aaSNeale Ranns
75208c29aaSNeale Ranns  return (q);
76208c29aaSNeale Ranns}
77208c29aaSNeale Ranns
78208c29aaSNeale Rannsstd::queue<cmd*>&
79208c29aaSNeale Rannspipe::mk_delete_cmd(std::queue<cmd*>& q)
80208c29aaSNeale Ranns{
81208c29aaSNeale Ranns  q.push(new pipe_cmds::delete_cmd(m_hdl, m_hdl_pair));
82208c29aaSNeale Ranns
83208c29aaSNeale Ranns  return (q);
84208c29aaSNeale Ranns}
85208c29aaSNeale Ranns
86208c29aaSNeale Rannsstd::shared_ptr<pipe>
87208c29aaSNeale Rannspipe::singular() const
88208c29aaSNeale Ranns{
89208c29aaSNeale Ranns  return std::dynamic_pointer_cast<pipe>(singular_i());
90208c29aaSNeale Ranns}
91208c29aaSNeale Ranns
92208c29aaSNeale Rannsstd::shared_ptr<interface>
93208c29aaSNeale Rannspipe::singular_i() const
94208c29aaSNeale Ranns{
95208c29aaSNeale Ranns  return m_db.find_or_add(key(), *this);
96208c29aaSNeale Ranns}
97208c29aaSNeale Ranns
98208c29aaSNeale Rannsstd::shared_ptr<pipe>
99208c29aaSNeale Rannspipe::find(const key_t& k)
100208c29aaSNeale Ranns{
101208c29aaSNeale Ranns  return std::dynamic_pointer_cast<pipe>(m_db.find(k));
102208c29aaSNeale Ranns}
103208c29aaSNeale Ranns
104208c29aaSNeale Rannsstd::shared_ptr<interface>
105208c29aaSNeale Rannspipe::west()
106208c29aaSNeale Ranns{
107208c29aaSNeale Ranns  if (!m_ends[WEST]) {
108208c29aaSNeale Ranns    if (rc_t::OK == m_hdl_pair.rc()) {
109208c29aaSNeale Ranns      m_ends[WEST] = pipe_end(*this, WEST).singular();
110208c29aaSNeale Ranns      m_ends[WEST]->set(m_hdl_pair.data().first);
111208c29aaSNeale Ranns    }
112208c29aaSNeale Ranns  }
113208c29aaSNeale Ranns
114208c29aaSNeale Ranns  return (m_ends[WEST]);
115208c29aaSNeale Ranns}
116208c29aaSNeale Ranns
117208c29aaSNeale Rannsstd::shared_ptr<interface>
118208c29aaSNeale Rannspipe::east()
119208c29aaSNeale Ranns{
120208c29aaSNeale Ranns  if (!m_ends[EAST]) {
121208c29aaSNeale Ranns    if (rc_t::OK == m_hdl_pair.rc()) {
122208c29aaSNeale Ranns      m_ends[EAST] = pipe_end(*this, EAST).singular();
123208c29aaSNeale Ranns      m_ends[EAST]->set(m_hdl_pair.data().first);
124208c29aaSNeale Ranns    }
125208c29aaSNeale Ranns  }
126208c29aaSNeale Ranns
127208c29aaSNeale Ranns  return (m_ends[EAST]);
128208c29aaSNeale Ranns}
129208c29aaSNeale Ranns
130208c29aaSNeale Rannspipe::pipe_end::pipe_end(const pipe& p, uint8_t id)
131208c29aaSNeale Ranns  : interface(p.name() + "." + std::to_string(id),
132208c29aaSNeale Ranns              interface::type_t::PIPE_END,
133208c29aaSNeale Ranns              interface::admin_state_t::UP)
134208c29aaSNeale Ranns  , m_pipe(p.singular())
135208c29aaSNeale Ranns{
136208c29aaSNeale Ranns}
137208c29aaSNeale Ranns
138208c29aaSNeale Rannsstd::queue<cmd*>&
139208c29aaSNeale Rannspipe::pipe_end::mk_create_cmd(std::queue<cmd*>& q)
140208c29aaSNeale Ranns{
141208c29aaSNeale Ranns  return (q);
142208c29aaSNeale Ranns}
143208c29aaSNeale Ranns
144208c29aaSNeale Rannsstd::queue<cmd*>&
145208c29aaSNeale Rannspipe::pipe_end::mk_delete_cmd(std::queue<cmd*>& q)
146208c29aaSNeale Ranns{
147208c29aaSNeale Ranns  return (q);
148208c29aaSNeale Ranns}
149208c29aaSNeale Ranns
150208c29aaSNeale Rannsvoid
151208c29aaSNeale Rannspipe::set_ends(const handle_pair_t& p)
152208c29aaSNeale Ranns{
153208c29aaSNeale Ranns  if (handle_t::INVALID != p.first && handle_t::INVALID != p.second) {
154208c29aaSNeale Ranns    m_hdl_pair = { p, rc_t::OK };
155208c29aaSNeale Ranns  } else {
156208c29aaSNeale Ranns    m_hdl_pair = { p, rc_t::INVALID };
157208c29aaSNeale Ranns  }
158208c29aaSNeale Ranns}
159208c29aaSNeale Ranns
160208c29aaSNeale Rannspipe::event_handler::event_handler()
161208c29aaSNeale Ranns{
162208c29aaSNeale Ranns  OM::register_listener(this);
163208c29aaSNeale Ranns  inspect::register_handler({ "pipe" }, "pipes", this);
164208c29aaSNeale Ranns}
165208c29aaSNeale Ranns
166208c29aaSNeale Rannsvoid
167208c29aaSNeale Rannspipe::event_handler::handle_replay()
168208c29aaSNeale Ranns{
169208c29aaSNeale Ranns  // m_db.replay();
170208c29aaSNeale Ranns}
171208c29aaSNeale Ranns
172208c29aaSNeale Rannsvoid
173208c29aaSNeale Rannspipe::event_handler::handle_populate(const client_db::key_t& key)
174208c29aaSNeale Ranns{
175208c29aaSNeale Ranns  std::shared_ptr<pipe_cmds::dump_cmd> cmd =
176208c29aaSNeale Ranns    std::make_shared<pipe_cmds::dump_cmd>();
177208c29aaSNeale Ranns
178208c29aaSNeale Ranns  HW::enqueue(cmd);
179208c29aaSNeale Ranns  HW::write();
180208c29aaSNeale Ranns
181208c29aaSNeale Ranns  for (auto& record : *cmd) {
182208c29aaSNeale Ranns    std::shared_ptr<pipe> sp;
183208c29aaSNeale Ranns
184208c29aaSNeale Ranns    sp = interface_factory::new_pipe_interface(record.get_payload());
185208c29aaSNeale Ranns
186208c29aaSNeale Ranns    VOM_LOG(log_level_t::DEBUG) << " pipe-dump: " << sp->to_string();
187208c29aaSNeale Ranns    OM::commit(key, *sp);
188208c29aaSNeale Ranns  }
189208c29aaSNeale Ranns}
190208c29aaSNeale Ranns
191208c29aaSNeale Rannsdependency_t
192208c29aaSNeale Rannspipe::event_handler::order() const
193208c29aaSNeale Ranns{
194208c29aaSNeale Ranns  return (dependency_t::VIRTUAL_INTERFACE);
195208c29aaSNeale Ranns}
196208c29aaSNeale Ranns
197208c29aaSNeale Rannsvoid
198208c29aaSNeale Rannspipe::event_handler::show(std::ostream& os)
199208c29aaSNeale Ranns{
200208c29aaSNeale Ranns  db_dump(m_db, os);
201208c29aaSNeale Ranns}
202208c29aaSNeale Ranns
203208c29aaSNeale Ranns}; // namespace VOM
204208c29aaSNeale Ranns
205208c29aaSNeale Ranns/*
206208c29aaSNeale Ranns * fd.io coding-style-patch-verification: ON
207208c29aaSNeale Ranns *
208208c29aaSNeale Ranns * Local Variables:
209208c29aaSNeale Ranns * eval: (c-set-style "mozilla")
210208c29aaSNeale Ranns * End:
211208c29aaSNeale Ranns */
212