blob: 05d51bbf0b1915f2546cc9023275512bb8432356 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/lldp_binding_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20/**
21 * A DB of all LLDP configs
22 */
23singular_db<interface::key_type, lldp_binding> lldp_binding::m_db;
24
25lldp_binding::event_handler lldp_binding::m_evh;
26
27lldp_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
34lldp_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
41lldp_binding::~lldp_binding()
42{
43 sweep();
44
45 // not in the DB anymore.
46 m_db.release(m_itf->key(), this);
47}
48
49void
50lldp_binding::sweep()
51{
52 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070053 HW::enqueue(new lldp_binding_cmds::unbind_cmd(m_binding, m_itf->handle()));
Neale Ranns812ed392017-10-16 04:20:13 -070054 }
55 HW::write();
56}
57
58void
59lldp_binding::dump(std::ostream& os)
60{
61 m_db.dump(os);
62}
63
64void
65lldp_binding::replay()
66{
67 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070068 HW::enqueue(
69 new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
Neale Ranns812ed392017-10-16 04:20:13 -070070 }
71}
72
73std::string
74lldp_binding::to_string() const
75{
76 std::ostringstream s;
77 s << "Lldp-binding: " << m_itf->to_string() << " port_desc:" << m_port_desc
78 << " " << m_binding.to_string();
79
80 return (s.str());
81}
82
83void
84lldp_binding::update(const lldp_binding& desired)
85{
86 /*
87 * the desired state is always that the interface should be created
88 */
89 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070090 HW::enqueue(
91 new lldp_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_port_desc));
Neale Ranns812ed392017-10-16 04:20:13 -070092 }
93}
94
95std::shared_ptr<lldp_binding>
96lldp_binding::find_or_add(const lldp_binding& temp)
97{
98 return (m_db.find_or_add(temp.m_itf->key(), temp));
99}
100
101std::shared_ptr<lldp_binding>
102lldp_binding::singular() const
103{
104 return find_or_add(*this);
105}
106
107lldp_binding::event_handler::event_handler()
108{
109 OM::register_listener(this);
110 inspect::register_handler({ "lldp" }, "LLDP bindings", this);
111}
112
113void
114lldp_binding::event_handler::handle_replay()
115{
116 m_db.replay();
117}
118
119void
120lldp_binding::event_handler::handle_populate(const client_db::key_t& key)
121{
122 // FIXME
123}
124
125dependency_t
126lldp_binding::event_handler::order() const
127{
128 return (dependency_t::BINDING);
129}
130
131void
132lldp_binding::event_handler::show(std::ostream& os)
133{
134 m_db.dump(os);
135}
136}
137
138/*
139 * fd.io coding-style-patch-verification: ON
140 *
141 * Local Variables:
142 * eval: (c-set-style "mozilla")
143 * End:
144 */