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