VPP Object Model (VOM)
[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/cmd.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
20 #include "vom/logger.hpp"
21
22 namespace VOM {
23 /**
24  * A DB of al the interfaces, key on the name
25  */
26 singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
27
28 bridge_domain::event_handler bridge_domain::m_evh;
29
30 /**
31  * Construct a new object matching the desried state
32  */
33 bridge_domain::bridge_domain(uint32_t id)
34   : m_id(id)
35 {
36 }
37
38 bridge_domain::bridge_domain(const bridge_domain& o)
39   : m_id(o.m_id)
40 {
41 }
42
43 uint32_t
44 bridge_domain::id() const
45 {
46   return (m_id.data());
47 }
48
49 void
50 bridge_domain::sweep()
51 {
52   if (rc_t::OK == m_id.rc()) {
53     HW::enqueue(new delete_cmd(m_id));
54   }
55   HW::write();
56 }
57
58 void
59 bridge_domain::replay()
60 {
61   if (rc_t::OK == m_id.rc()) {
62     HW::enqueue(new create_cmd(m_id));
63   }
64 }
65
66 bridge_domain::~bridge_domain()
67 {
68   sweep();
69
70   // not in the DB anymore.
71   m_db.release(m_id.data(), this);
72 }
73
74 std::string
75 bridge_domain::to_string() const
76 {
77   std::ostringstream s;
78   s << "bridge-domain:[" << m_id.to_string() << "]";
79
80   return (s.str());
81 }
82
83 std::shared_ptr<bridge_domain>
84 bridge_domain::find(uint32_t id)
85 {
86   /*
87  * Loop throught the entire map looking for matching interface.
88  * not the most efficient algorithm, but it will do for now. The
89  * number of L3 configs is low and this is only called during bootup
90  */
91   std::shared_ptr<bridge_domain> bd;
92
93   auto it = m_db.cbegin();
94
95   while (it != m_db.cend()) {
96     /*
97  * The key in the DB is a pair of the interface's name and prefix.
98  * If the keys match, save the L3-config
99  */
100     auto key = it->first;
101
102     if (id == key) {
103       bd = it->second.lock();
104       break;
105     }
106
107     ++it;
108   }
109
110   return (bd);
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 create_cmd(m_id));
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::dump_cmd> cmd(new bridge_domain::dump_cmd());
149
150   HW::enqueue(cmd);
151   HW::write();
152
153   for (auto& record : *cmd) {
154     auto& payload = record.get_payload();
155
156     bridge_domain bd(payload.bd_id);
157
158     VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
159
160     /*
161  * Write each of the discovered interfaces into the OM,
162  * but disable the HW Command q whilst we do, so that no
163  * commands are sent to VPP
164  */
165     OM::commit(key, bd);
166
167     /**
168  * For each interface in the BD construct an l2_binding
169  */
170     for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
171       std::shared_ptr<interface> itf =
172         interface::find(payload.sw_if_details[ii].sw_if_index);
173       l2_binding l2(*itf, bd);
174       OM::commit(key, l2);
175     }
176   }
177 }
178
179 bridge_domain::event_handler::event_handler()
180 {
181   OM::register_listener(this);
182   inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
183 }
184
185 void
186 bridge_domain::event_handler::handle_replay()
187 {
188   m_db.replay();
189 }
190
191 dependency_t
192 bridge_domain::event_handler::order() const
193 {
194   return (dependency_t::TABLE);
195 }
196
197 void
198 bridge_domain::event_handler::show(std::ostream& os)
199 {
200   m_db.dump(os);
201 }
202 }
203
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "mozilla")
209  * End:
210  */