VPP Object Model (VOM)
[vpp.git] / src / vpp-api / vom / lldp_binding.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/lldp_binding.hpp"
17 #include "vom/cmd.hpp"
18
19 namespace VOM {
20 /**
21  * A DB of all LLDP configs
22  */
23 singular_db<interface::key_type, lldp_binding> lldp_binding::m_db;
24
25 lldp_binding::event_handler lldp_binding::m_evh;
26
27 lldp_binding::lldp_binding(const interface& itf, const std::string& port_desc)
28   : m_itf(itf.singular())
29   , m_port_desc(port_desc)
30   , m_binding(0)
31 {
32 }
33
34 lldp_binding::lldp_binding(const lldp_binding& o)
35   : m_itf(o.m_itf)
36   , m_port_desc(o.m_port_desc)
37   , m_binding(0)
38 {
39 }
40
41 lldp_binding::~lldp_binding()
42 {
43   sweep();
44
45   // not in the DB anymore.
46   m_db.release(m_itf->key(), this);
47 }
48
49 void
50 lldp_binding::sweep()
51 {
52   if (m_binding) {
53     HW::enqueue(new unbind_cmd(m_binding, m_itf->handle()));
54   }
55   HW::write();
56 }
57
58 void
59 lldp_binding::dump(std::ostream& os)
60 {
61   m_db.dump(os);
62 }
63
64 void
65 lldp_binding::replay()
66 {
67   if (m_binding) {
68     HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_port_desc));
69   }
70 }
71
72 std::string
73 lldp_binding::to_string() const
74 {
75   std::ostringstream s;
76   s << "Lldp-binding: " << m_itf->to_string() << " port_desc:" << m_port_desc
77     << " " << m_binding.to_string();
78
79   return (s.str());
80 }
81
82 void
83 lldp_binding::update(const lldp_binding& desired)
84 {
85   /*
86  * the desired state is always that the interface should be created
87  */
88   if (!m_binding) {
89     HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_port_desc));
90   }
91 }
92
93 std::shared_ptr<lldp_binding>
94 lldp_binding::find_or_add(const lldp_binding& temp)
95 {
96   return (m_db.find_or_add(temp.m_itf->key(), temp));
97 }
98
99 std::shared_ptr<lldp_binding>
100 lldp_binding::singular() const
101 {
102   return find_or_add(*this);
103 }
104
105 lldp_binding::event_handler::event_handler()
106 {
107   OM::register_listener(this);
108   inspect::register_handler({ "lldp" }, "LLDP bindings", this);
109 }
110
111 void
112 lldp_binding::event_handler::handle_replay()
113 {
114   m_db.replay();
115 }
116
117 void
118 lldp_binding::event_handler::handle_populate(const client_db::key_t& key)
119 {
120   // FIXME
121 }
122
123 dependency_t
124 lldp_binding::event_handler::order() const
125 {
126   return (dependency_t::BINDING);
127 }
128
129 void
130 lldp_binding::event_handler::show(std::ostream& os)
131 {
132   m_db.dump(os);
133 }
134 }
135
136 /*
137  * fd.io coding-style-patch-verification: ON
138  *
139  * Local Variables:
140  * eval: (c-set-style "mozilla")
141  * End:
142  */