VOM: logging, populate and stats fixes
[vpp.git] / src / vpp-api / vom / bridge_domain.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/bridge_domain.hpp"
17 #include "vom/bridge_domain_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
20
21 namespace VOM {
22
23 const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::ON(1,
24                                                                         "on");
25 const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::OFF(0,
26                                                                          "off");
27
28 bridge_domain::learning_mode_t::learning_mode_t(int v, const std::string& s)
29   : enum_base<bridge_domain::learning_mode_t>(v, s)
30 {
31 }
32
33 /**
34  * A DB of al the interfaces, key on the name
35  */
36 singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
37
38 bridge_domain::event_handler bridge_domain::m_evh;
39
40 /**
41  * Construct a new object matching the desried state
42  */
43 bridge_domain::bridge_domain(uint32_t id, const learning_mode_t& lmode)
44   : m_id(id)
45   , m_learning_mode(lmode)
46 {
47 }
48
49 bridge_domain::bridge_domain(const bridge_domain& o)
50   : m_id(o.m_id)
51   , m_learning_mode(o.m_learning_mode)
52 {
53 }
54
55 const bridge_domain::key_t&
56 bridge_domain::key() const
57 {
58   return (m_id.data());
59 }
60
61 uint32_t
62 bridge_domain::id() const
63 {
64   return (m_id.data());
65 }
66
67 bool
68 bridge_domain::operator==(const bridge_domain& b) const
69 {
70   return (id() == b.id());
71 }
72
73 void
74 bridge_domain::sweep()
75 {
76   if (rc_t::OK == m_id.rc()) {
77     HW::enqueue(new bridge_domain_cmds::delete_cmd(m_id));
78   }
79   HW::write();
80 }
81
82 void
83 bridge_domain::replay()
84 {
85   if (rc_t::OK == m_id.rc()) {
86     HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
87   }
88 }
89
90 bridge_domain::~bridge_domain()
91 {
92   sweep();
93
94   // not in the DB anymore.
95   m_db.release(m_id.data(), this);
96 }
97
98 std::string
99 bridge_domain::to_string() const
100 {
101   std::ostringstream s;
102   s << "bridge-domain:[" << m_id.to_string() << "]";
103
104   return (s.str());
105 }
106
107 std::shared_ptr<bridge_domain>
108 bridge_domain::find(const key_t& key)
109 {
110   return (m_db.find(key));
111 }
112
113 void
114 bridge_domain::update(const bridge_domain& desired)
115 {
116   /*
117    * the desired state is always that the interface should be created
118    */
119   if (rc_t::OK != m_id.rc()) {
120     HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
121   }
122 }
123
124 std::shared_ptr<bridge_domain>
125 bridge_domain::find_or_add(const bridge_domain& temp)
126 {
127   return (m_db.find_or_add(temp.m_id.data(), temp));
128 }
129
130 std::shared_ptr<bridge_domain>
131 bridge_domain::singular() const
132 {
133   return find_or_add(*this);
134 }
135
136 void
137 bridge_domain::dump(std::ostream& os)
138 {
139   m_db.dump(os);
140 }
141
142 void
143 bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
144 {
145   /*
146    * dump VPP Bridge domains
147    */
148   std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd =
149     std::make_shared<bridge_domain_cmds::dump_cmd>();
150
151   HW::enqueue(cmd);
152   HW::write();
153
154   for (auto& record : *cmd) {
155     auto& payload = record.get_payload();
156
157     bridge_domain bd(payload.bd_id);
158
159     VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
160
161     /*
162      * Write each of the discovered bridge-domains into the OM,
163      * but disable the HW Command q whilst we do, so that no
164      * commands are sent to VPP
165      */
166     OM::commit(key, bd);
167
168     /**
169      * For each interface in the BD construct an l2_binding
170      */
171     for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
172       std::shared_ptr<interface> itf =
173         interface::find(payload.sw_if_details[ii].sw_if_index);
174       l2_binding l2(*itf, bd);
175       OM::commit(key, l2);
176     }
177   }
178 }
179
180 bridge_domain::event_handler::event_handler()
181 {
182   OM::register_listener(this);
183   inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
184 }
185
186 void
187 bridge_domain::event_handler::handle_replay()
188 {
189   m_db.replay();
190 }
191
192 dependency_t
193 bridge_domain::event_handler::order() const
194 {
195   return (dependency_t::TABLE);
196 }
197
198 void
199 bridge_domain::event_handler::show(std::ostream& os)
200 {
201   m_db.dump(os);
202 }
203 }
204
205 /*
206  * fd.io coding-style-patch-verification: ON
207  *
208  * Local Variables:
209  * eval: (c-set-style "mozilla")
210  * End:
211  */