fib: fib api updates
[vpp.git] / extras / vom / vom / route_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/route_domain.hpp"
17 #include "vom/cmd.hpp"
18 #include "vom/route_domain_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 route_domain::event_handler route_domain::m_evh;
24
25 /**
26  * A DB of al the interfaces, key on the name
27  */
28 singular_db<route::table_id_t, route_domain> route_domain::m_db;
29
30 /**
31  * Construct a new object matching the desried state
32  */
33 route_domain::route_domain(route::table_id_t id)
34   : m_hw_v4(true)
35   , m_hw_v6(true)
36   , m_table_id(id)
37 {
38 }
39
40 route_domain::route_domain(const route_domain& o)
41   : m_hw_v4(o.m_hw_v4)
42   , m_hw_v6(o.m_hw_v6)
43   , m_table_id(o.m_table_id)
44 {
45 }
46
47 bool
48 route_domain::operator==(const route_domain& r) const
49 {
50   return (m_table_id == r.m_table_id);
51 }
52
53 route::table_id_t
54 route_domain::table_id() const
55 {
56   return (m_table_id);
57 }
58
59 route_domain::key_t
60 route_domain::key() const
61 {
62   return (table_id());
63 }
64
65 route_domain::const_iterator_t
66 route_domain::cbegin()
67 {
68   return m_db.begin();
69 }
70
71 route_domain::const_iterator_t
72 route_domain::cend()
73 {
74   return m_db.end();
75 }
76
77 void
78 route_domain::sweep()
79 {
80   if (m_hw_v4) {
81     HW::enqueue(
82       new route_domain_cmds::delete_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
83   }
84   if (m_hw_v6) {
85     HW::enqueue(
86       new route_domain_cmds::delete_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
87   }
88   HW::write();
89 }
90
91 void
92 route_domain::replay()
93 {
94   if (m_hw_v4) {
95     HW::enqueue(
96       new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
97   }
98   if (m_hw_v6) {
99     HW::enqueue(
100       new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
101   }
102 }
103
104 route_domain::~route_domain()
105 {
106   sweep();
107
108   // not in the DB anymore.
109   m_db.release(m_table_id, this);
110 }
111
112 std::string
113 route_domain::to_string() const
114 {
115   std::ostringstream s;
116   s << "route-domain:["
117     << "table-id:" << m_table_id << " v4:" << m_hw_v4.to_string()
118     << " v6:" << m_hw_v6.to_string() << "]";
119
120   return (s.str());
121 }
122
123 std::shared_ptr<route_domain>
124 route_domain::find(const key_t& k)
125 {
126   return (m_db.find(k));
127 }
128
129 void
130 route_domain::update(const route_domain& desired)
131 {
132   /*
133  * create the table if it is not yet created
134  */
135   if (rc_t::OK != m_hw_v4.rc()) {
136     HW::enqueue(
137       new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
138   }
139   if (rc_t::OK != m_hw_v6.rc()) {
140     HW::enqueue(
141       new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
142   }
143 }
144
145 std::shared_ptr<route_domain>
146 route_domain::get_default()
147 {
148   route_domain rd(route::DEFAULT_TABLE);
149
150   return (find_or_add(rd));
151 }
152
153 std::shared_ptr<route_domain>
154 route_domain::find_or_add(const route_domain& temp)
155 {
156   return (m_db.find_or_add(temp.m_table_id, temp));
157 }
158
159 std::shared_ptr<route_domain>
160 route_domain::singular() const
161 {
162   return find_or_add(*this);
163 }
164
165 void
166 route_domain::dump(std::ostream& os)
167 {
168   db_dump(m_db, os);
169 }
170
171 void
172 route_domain::event_handler::handle_populate(const client_db::key_t& key)
173 {
174   std::shared_ptr<route_domain_cmds::dump_cmd> cmd =
175     std::make_shared<route_domain_cmds::dump_cmd>();
176
177   HW::enqueue(cmd);
178   HW::write();
179
180   for (auto& record : *cmd) {
181     auto& payload = record.get_payload();
182
183     route_domain rd(payload.table.table_id);
184
185     VOM_LOG(log_level_t::DEBUG) << "ip-table-dump: " << rd.to_string();
186
187     /*
188      * Write each of the discovered interfaces into the OM,
189      * but disable the HW Command q whilst we do, so that no
190      * commands are sent to VPP
191      */
192     OM::commit(key, rd);
193   }
194 }
195
196 route_domain::event_handler::event_handler()
197 {
198   OM::register_listener(this);
199   inspect::register_handler({ "rd", "route-domain" }, "Route Domains", this);
200 }
201
202 void
203 route_domain::event_handler::handle_replay()
204 {
205   m_db.replay();
206 }
207
208 dependency_t
209 route_domain::event_handler::order() const
210 {
211   return (dependency_t::TABLE);
212 }
213
214 void
215 route_domain::event_handler::show(std::ostream& os)
216 {
217   db_dump(m_db, os);
218 }
219
220 }; // namespace VOPM
221
222 /*
223  * fd.io coding-style-patch-verification: ON
224  *
225  * Local Variables:
226  * eval: (c-set-style "mozilla")
227  * End:
228  */