blob: f173fd7a2502b98161420fe1fbbd2e6f70129225 [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 {
21/**
22 * A DB of al the interfaces, key on the name
23 */
24singular_db<route::table_id_t, route_domain> route_domain::m_db;
25
26/**
27 * Construct a new object matching the desried state
28 */
29route_domain::route_domain(route::table_id_t id)
30 : m_hw_v4(true)
31 , m_hw_v6(true)
32 , m_table_id(id)
33{
34}
35
36route_domain::route_domain(const route_domain& o)
37 : m_hw_v4(o.m_hw_v4)
38 , m_hw_v6(o.m_hw_v6)
39 , m_table_id(o.m_table_id)
40{
41}
42
43route::table_id_t
44route_domain::table_id() const
45{
46 return (m_table_id);
47}
48
49route_domain::key_t
50route_domain::key() const
51{
52 return (table_id());
53}
54
55void
56route_domain::sweep()
57{
58 if (m_hw_v4) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070059 HW::enqueue(
60 new route_domain_cmds::delete_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070061 }
62 if (m_hw_v6) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070063 HW::enqueue(
64 new route_domain_cmds::delete_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070065 }
66 HW::write();
67}
68
69void
70route_domain::replay()
71{
72 if (m_hw_v4) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070073 HW::enqueue(
74 new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070075 }
76 if (m_hw_v6) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070077 HW::enqueue(
78 new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -070079 }
80}
81
82route_domain::~route_domain()
83{
84 sweep();
85
86 // not in the DB anymore.
87 m_db.release(m_table_id, this);
88}
89
90std::string
91route_domain::to_string() const
92{
93 std::ostringstream s;
94 s << "route-domain:["
95 << "table-id:" << m_table_id << " v4:" << m_hw_v4 << " v6:" << m_hw_v6
96 << "]";
97
98 return (s.str());
99}
100
101std::shared_ptr<route_domain>
102route_domain::find(const route_domain& temp)
103{
104 std::shared_ptr<route_domain> rd;
105
106 auto it = m_db.cbegin();
107
108 while (it != m_db.cend()) {
109 /*
110 * The key in the DB is a pair of the interface's name and prefix.
111 * If the keys match, save the L3-config
112 */
113 auto key = it->first;
114
115 if (temp.table_id() == key) {
116 rd = it->second.lock();
117 break;
118 }
119
120 ++it;
121 }
122
123 return (rd);
124}
125
126void
127route_domain::update(const route_domain& desired)
128{
129 /*
130 * create the table if it is not yet created
131 */
132 if (rc_t::OK != m_hw_v4.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700133 HW::enqueue(
134 new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700135 }
136 if (rc_t::OK != m_hw_v6.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700137 HW::enqueue(
138 new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700139 }
140}
141
142std::shared_ptr<route_domain>
143route_domain::get_default()
144{
145 route_domain rd(route::DEFAULT_TABLE);
146
147 return (find_or_add(rd));
148}
149
150std::shared_ptr<route_domain>
151route_domain::find_or_add(const route_domain& temp)
152{
153 return (m_db.find_or_add(temp.m_table_id, temp));
154}
155
156std::shared_ptr<route_domain>
157route_domain::singular() const
158{
159 return find_or_add(*this);
160}
161
162void
163route_domain::dump(std::ostream& os)
164{
165 m_db.dump(os);
166}
167}
168/*
169 * fd.io coding-style-patch-verification: ON
170 *
171 * Local Variables:
172 * eval: (c-set-style "mozilla")
173 * End:
174 */