FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
lldp_binding.cpp
Go to the documentation of this file.
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"
18 
19 namespace VOM {
20 /**
21  * A DB of all LLDP configs
22  */
23 singular_db<interface::key_t, 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 
35  : m_itf(o.m_itf)
36  , m_port_desc(o.m_port_desc)
37  , m_binding(0)
38 {
39 }
40 
42 {
43  sweep();
44 
45  // not in the DB anymore.
46  m_db.release(m_itf->key(), this);
47 }
48 
49 bool
51 {
52  return ((key() == l.key()) && (m_port_desc == l.m_port_desc));
53 }
54 
57 {
58  return (m_itf->key());
59 }
60 
61 void
62 lldp_binding::sweep()
63 {
64  if (m_binding) {
65  HW::enqueue(new lldp_binding_cmds::unbind_cmd(m_binding, m_itf->handle()));
66  }
67  HW::write();
68 }
69 
70 void
71 lldp_binding::dump(std::ostream& os)
72 {
73  m_db.dump(os);
74 }
75 
76 void
77 lldp_binding::replay()
78 {
79  if (m_binding) {
81  new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
82  }
83 }
84 
85 std::string
87 {
88  std::ostringstream s;
89  s << "Lldp-binding: " << m_itf->to_string() << " port_desc:" << m_port_desc
90  << " " << m_binding.to_string();
91 
92  return (s.str());
93 }
94 
95 void
96 lldp_binding::update(const lldp_binding& desired)
97 {
98  /*
99  * the desired state is always that the interface should be created
100  */
101  if (!m_binding) {
102  HW::enqueue(
103  new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
104  }
105 }
106 
107 std::shared_ptr<lldp_binding>
108 lldp_binding::find_or_add(const lldp_binding& temp)
109 {
110  return (m_db.find_or_add(temp.m_itf->key(), temp));
111 }
112 
113 std::shared_ptr<lldp_binding>
115 {
116  return (m_db.find(k));
117 }
118 
119 std::shared_ptr<lldp_binding>
121 {
122  return find_or_add(*this);
123 }
124 
125 lldp_binding::event_handler::event_handler()
126 {
127  OM::register_listener(this);
128  inspect::register_handler({ "lldp" }, "LLDP bindings", this);
129 }
130 
131 void
132 lldp_binding::event_handler::handle_replay()
133 {
134  m_db.replay();
135 }
136 
137 void
138 lldp_binding::event_handler::handle_populate(const client_db::key_t& key)
139 {
140  // FIXME
141 }
142 
144 lldp_binding::event_handler::order() const
145 {
146  return (dependency_t::BINDING);
147 }
148 
149 void
150 lldp_binding::event_handler::show(std::ostream& os)
151 {
152  m_db.dump(os);
153 }
154 }
155 
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "mozilla")
161  * End:
162  */
A representation of LLDP client configuration on an interface.
static void dump(std::ostream &os)
Dump all LLDP bindings into the stream provided.
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static std::shared_ptr< lldp_binding > find(const key_t &k)
Find or add LLDP binding based on its key.
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:225
std::string to_string() const
convert to string format for debug purposes
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
bool operator==(const lldp_binding &b) const
Comparison operator.
~lldp_binding()
Destructor.
A cmd class that Unbinds Lldp Config from an interface.
A representation of an interface in VPP.
Definition: interface.hpp:41
const key_t & key() const
Return this object&#39;s key.
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:189
A command class that binds the LLDP config to the interface.
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
lldp_binding(const interface &itf, const std::string &hostname)
Construct a new object matching the desried state.
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
interface::key_t key_t
Typedef for the key of a LLDP binding.
std::shared_ptr< lldp_binding > singular() const
Return the &#39;singular&#39; of the LLDP binding that matches this object.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:124