blob: 08357faa6c56b305f4c5b74cd16afcf91f79ba8c [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/route_domain.hpp"
17#include "vom/cmd.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070018#include "vom/route_domain_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070019
20namespace VOM {
Neale Ranns10e7a9f2017-11-14 08:40:43 -080021
22route_domain::event_handler route_domain::m_evh;
23
Neale Ranns812ed392017-10-16 04:20:13 -070024/**
25 * A DB of al the interfaces, key on the name
26 */
27singular_db<route::table_id_t, route_domain> route_domain::m_db;
28
29/**
30 * Construct a new object matching the desried state
31 */
32route_domain::route_domain(route::table_id_t id)
33 : m_hw_v4(true)
34 , m_hw_v6(true)
35 , m_table_id(id)
36{
37}
38
39route_domain::route_domain(const route_domain& o)
40 : m_hw_v4(o.m_hw_v4)
41 , m_hw_v6(o.m_hw_v6)
42 , m_table_id(o.m_table_id)
43{
44}
45
46route::table_id_t
47route_domain::table_id() const
48{
49 return (m_table_id);
50}
51
52route_domain::key_t
53route_domain::key() const
54{
55 return (table_id());
56}
57
58void
59route_domain::sweep()
60{
61 if (m_hw_v4) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070062 HW::enqueue(
63 new route_domain_cmds::delete_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070064 }
65 if (m_hw_v6) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070066 HW::enqueue(
67 new route_domain_cmds::delete_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070068 }
69 HW::write();
70}
71
72void
73route_domain::replay()
74{
75 if (m_hw_v4) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070076 HW::enqueue(
77 new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070078 }
79 if (m_hw_v6) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070080 HW::enqueue(
81 new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070082 }
83}
84
85route_domain::~route_domain()
86{
87 sweep();
88
89 // not in the DB anymore.
90 m_db.release(m_table_id, this);
91}
92
93std::string
94route_domain::to_string() const
95{
96 std::ostringstream s;
97 s << "route-domain:["
98 << "table-id:" << m_table_id << " v4:" << m_hw_v4 << " v6:" << m_hw_v6
99 << "]";
100
101 return (s.str());
102}
103
104std::shared_ptr<route_domain>
105route_domain::find(const route_domain& temp)
106{
107 std::shared_ptr<route_domain> rd;
108
109 auto it = m_db.cbegin();
110
111 while (it != m_db.cend()) {
112 /*
113 * The key in the DB is a pair of the interface's name and prefix.
114 * If the keys match, save the L3-config
115 */
116 auto key = it->first;
117
118 if (temp.table_id() == key) {
119 rd = it->second.lock();
120 break;
121 }
122
123 ++it;
124 }
125
126 return (rd);
127}
128
129void
130route_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()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700136 HW::enqueue(
137 new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700138 }
139 if (rc_t::OK != m_hw_v6.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700140 HW::enqueue(
141 new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700142 }
143}
144
145std::shared_ptr<route_domain>
146route_domain::get_default()
147{
148 route_domain rd(route::DEFAULT_TABLE);
149
150 return (find_or_add(rd));
151}
152
153std::shared_ptr<route_domain>
154route_domain::find_or_add(const route_domain& temp)
155{
156 return (m_db.find_or_add(temp.m_table_id, temp));
157}
158
159std::shared_ptr<route_domain>
160route_domain::singular() const
161{
162 return find_or_add(*this);
163}
164
165void
166route_domain::dump(std::ostream& os)
167{
168 m_db.dump(os);
169}
Neale Ranns10e7a9f2017-11-14 08:40:43 -0800170
171void
172route_domain::event_handler::handle_populate(const client_db::key_t& key)
173{
Neale Ranns812ed392017-10-16 04:20:13 -0700174}
Neale Ranns10e7a9f2017-11-14 08:40:43 -0800175
176route_domain::event_handler::event_handler()
177{
178 OM::register_listener(this);
179 inspect::register_handler({ "rd", "route-domain" }, "Route Domains", this);
180}
181
182void
183route_domain::event_handler::handle_replay()
184{
185 m_db.replay();
186}
187
188dependency_t
189route_domain::event_handler::order() const
190{
191 return (dependency_t::TABLE);
192}
193
194void
195route_domain::event_handler::show(std::ostream& os)
196{
197 m_db.dump(os);
198}
199
200}; // namespace VOPM
201
Neale Ranns812ed392017-10-16 04:20:13 -0700202/*
203 * fd.io coding-style-patch-verification: ON
204 *
205 * Local Variables:
206 * eval: (c-set-style "mozilla")
207 * End:
208 */