GBP: add allowed ethertypes to contracts
[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 void
66 route_domain::sweep()
67 {
68   if (m_hw_v4) {
69     HW::enqueue(
70       new route_domain_cmds::delete_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
71   }
72   if (m_hw_v6) {
73     HW::enqueue(
74       new route_domain_cmds::delete_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
75   }
76   HW::write();
77 }
78
79 void
80 route_domain::replay()
81 {
82   if (m_hw_v4) {
83     HW::enqueue(
84       new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
85   }
86   if (m_hw_v6) {
87     HW::enqueue(
88       new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
89   }
90 }
91
92 route_domain::~route_domain()
93 {
94   sweep();
95
96   // not in the DB anymore.
97   m_db.release(m_table_id, this);
98 }
99
100 std::string
101 route_domain::to_string() const
102 {
103   std::ostringstream s;
104   s << "route-domain:["
105     << "table-id:" << m_table_id << " v4:" << m_hw_v4.to_string()
106     << " v6:" << m_hw_v6.to_string() << "]";
107
108   return (s.str());
109 }
110
111 std::shared_ptr<route_domain>
112 route_domain::find(const key_t& k)
113 {
114   return (m_db.find(k));
115 }
116
117 void
118 route_domain::update(const route_domain& desired)
119 {
120   /*
121  * create the table if it is not yet created
122  */
123   if (rc_t::OK != m_hw_v4.rc()) {
124     HW::enqueue(
125       new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id));
126   }
127   if (rc_t::OK != m_hw_v6.rc()) {
128     HW::enqueue(
129       new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id));
130   }
131 }
132
133 std::shared_ptr<route_domain>
134 route_domain::get_default()
135 {
136   route_domain rd(route::DEFAULT_TABLE);
137
138   return (find_or_add(rd));
139 }
140
141 std::shared_ptr<route_domain>
142 route_domain::find_or_add(const route_domain& temp)
143 {
144   return (m_db.find_or_add(temp.m_table_id, temp));
145 }
146
147 std::shared_ptr<route_domain>
148 route_domain::singular() const
149 {
150   return find_or_add(*this);
151 }
152
153 void
154 route_domain::dump(std::ostream& os)
155 {
156   db_dump(m_db, os);
157 }
158
159 void
160 route_domain::event_handler::handle_populate(const client_db::key_t& key)
161 {
162 }
163
164 route_domain::event_handler::event_handler()
165 {
166   OM::register_listener(this);
167   inspect::register_handler({ "rd", "route-domain" }, "Route Domains", this);
168 }
169
170 void
171 route_domain::event_handler::handle_replay()
172 {
173   m_db.replay();
174 }
175
176 dependency_t
177 route_domain::event_handler::order() const
178 {
179   return (dependency_t::TABLE);
180 }
181
182 void
183 route_domain::event_handler::show(std::ostream& os)
184 {
185   db_dump(m_db, os);
186 }
187
188 }; // namespace VOPM
189
190 /*
191  * fd.io coding-style-patch-verification: ON
192  *
193  * Local Variables:
194  * eval: (c-set-style "mozilla")
195  * End:
196  */