FD.io VPP  v21.01.1
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"
19 
20 namespace VOM {
21 /**
22  * A DB of all LLDP configs
23  */
24 singular_db<interface::key_t, lldp_binding> lldp_binding::m_db;
25 
26 lldp_binding::event_handler lldp_binding::m_evh;
27 
28 lldp_binding::lldp_binding(const interface& itf, const std::string& port_desc)
29  : m_itf(itf.singular())
30  , m_port_desc(port_desc)
31  , m_binding(0)
32 {
33 }
34 
36  : m_itf(o.m_itf)
37  , m_port_desc(o.m_port_desc)
38  , m_binding(0)
39 {
40 }
41 
43 {
44  sweep();
45 
46  // not in the DB anymore.
47  m_db.release(m_itf->key(), this);
48 }
49 
50 bool
52 {
53  return ((key() == l.key()) && (m_port_desc == l.m_port_desc));
54 }
55 
58 {
59  return (m_itf->key());
60 }
61 
62 void
63 lldp_binding::sweep()
64 {
65  if (m_binding) {
66  HW::enqueue(new lldp_binding_cmds::unbind_cmd(m_binding, m_itf->handle()));
67  }
68  HW::write();
69 }
70 
71 void
72 lldp_binding::dump(std::ostream& os)
73 {
74  db_dump(m_db, os);
75 }
76 
77 void
78 lldp_binding::replay()
79 {
80  if (m_binding) {
82  new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
83  }
84 }
85 
88 {
89  std::ostringstream s;
90  s << "Lldp-binding: " << m_itf->to_string() << " port_desc:" << m_port_desc
91  << " " << m_binding.to_string();
92 
93  return (s.str());
94 }
95 
96 void
97 lldp_binding::update(const lldp_binding& desired)
98 {
99  /*
100  * the desired state is always that the interface should be created
101  */
102  if (!m_binding) {
103  HW::enqueue(
104  new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
105  }
106 }
107 
108 std::shared_ptr<lldp_binding>
109 lldp_binding::find_or_add(const lldp_binding& temp)
110 {
111  return (m_db.find_or_add(temp.m_itf->key(), temp));
112 }
113 
114 std::shared_ptr<lldp_binding>
116 {
117  return (m_db.find(k));
118 }
119 
120 std::shared_ptr<lldp_binding>
122 {
123  return find_or_add(*this);
124 }
125 
127 {
128  OM::register_listener(this);
129  inspect::register_handler({ "lldp" }, "LLDP bindings", this);
130 }
131 
132 void
133 lldp_binding::event_handler::handle_replay()
134 {
135  m_db.replay();
136 }
137 
138 void
139 lldp_binding::event_handler::handle_populate(const client_db::key_t& key)
140 {
141  // FIXME
142 }
143 
145 lldp_binding::event_handler::order() const
146 {
147  return (dependency_t::BINDING);
148 }
149 
150 void
151 lldp_binding::event_handler::show(std::ostream& os)
152 {
153  db_dump(m_db, os);
154 }
155 }
156 
157 /*
158  * fd.io coding-style-patch-verification: OFF
159  *
160  * Local Variables:
161  * eval: (c-set-style "mozilla")
162  * End:
163  */
A representation of LLDP client configuration on an interface.
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
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
const char *const string
Definition: cJSON.h:172
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.
std::string to_string() const
convert to string format for debug purposes
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
std::shared_ptr< lldp_binding > singular() const
Return the &#39;singular&#39; of the LLDP binding that matches this object.
~lldp_binding()
Destructor.
A cmd class that Unbinds Lldp Config from an interface.
A representation of an interface in VPP.
Definition: interface.hpp:41
void event_handler(void *tls_async)
Definition: tls_async.c:334
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:212
A command class that binds the LLDP config to the interface.
Then L2/objects that bind to interfaces, BD, ACLS, etc.
bool operator==(const lldp_binding &b) const
Comparison operator.
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:104
const key_t & key() const
Return this object&#39;s key.
interface::key_t key_t
Typedef for the key of a LLDP binding.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127