GBP V2
[vpp.git] / src / vpp-api / vom / gbp_endpoint_group.cpp
1 /*
2  * Copyright (c) 2018 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/gbp_endpoint_group.hpp"
17 #include "vom/gbp_endpoint_group_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21
22 singular_db<gbp_endpoint_group::key_t, gbp_endpoint_group>
23   gbp_endpoint_group::m_db;
24
25 gbp_endpoint_group::event_handler gbp_endpoint_group::m_evh;
26
27 gbp_endpoint_group::gbp_endpoint_group(epg_id_t epg_id,
28                                        const interface& itf,
29                                        const route_domain& rd,
30                                        const bridge_domain& bd)
31   : m_hw(false)
32   , m_epg_id(epg_id)
33   , m_itf(itf.singular())
34   , m_rd(rd.singular())
35   , m_bd(bd.singular())
36 {
37 }
38
39 gbp_endpoint_group::gbp_endpoint_group(const gbp_endpoint_group& epg)
40   : m_hw(epg.m_hw)
41   , m_epg_id(epg.m_epg_id)
42   , m_itf(epg.m_itf)
43   , m_rd(epg.m_rd)
44   , m_bd(epg.m_bd)
45 {
46 }
47
48 gbp_endpoint_group::~gbp_endpoint_group()
49 {
50   sweep();
51   m_db.release(key(), this);
52 }
53
54 const gbp_endpoint_group::key_t
55 gbp_endpoint_group::key() const
56 {
57   return (m_epg_id);
58 }
59
60 epg_id_t
61 gbp_endpoint_group::id() const
62 {
63   return (m_epg_id);
64 }
65
66 bool
67 gbp_endpoint_group::operator==(const gbp_endpoint_group& gbpe) const
68 {
69   return (key() == gbpe.key() && (m_itf == gbpe.m_itf) && (m_rd == gbpe.m_rd) &&
70           (m_bd == gbpe.m_bd));
71 }
72
73 void
74 gbp_endpoint_group::sweep()
75 {
76   if (m_hw) {
77     HW::enqueue(new gbp_endpoint_group_cmds::delete_cmd(m_hw, m_epg_id));
78   }
79   HW::write();
80 }
81
82 void
83 gbp_endpoint_group::replay()
84 {
85   if (m_hw) {
86     HW::enqueue(new gbp_endpoint_group_cmds::create_cmd(
87       m_hw, m_epg_id, m_bd->id(), m_rd->table_id(), m_itf->handle()));
88   }
89 }
90
91 std::string
92 gbp_endpoint_group::to_string() const
93 {
94   std::ostringstream s;
95   s << "gbp-endpoint-group:["
96     << "epg:" << m_epg_id << ", " << m_itf->to_string() << ", "
97     << m_bd->to_string() << ", " << m_rd->to_string() << "]";
98
99   return (s.str());
100 }
101
102 void
103 gbp_endpoint_group::update(const gbp_endpoint_group& r)
104 {
105   if (rc_t::OK != m_hw.rc()) {
106     HW::enqueue(new gbp_endpoint_group_cmds::create_cmd(
107       m_hw, m_epg_id, m_bd->id(), m_rd->table_id(), m_itf->handle()));
108   }
109 }
110
111 std::shared_ptr<gbp_endpoint_group>
112 gbp_endpoint_group::find_or_add(const gbp_endpoint_group& temp)
113 {
114   return (m_db.find_or_add(temp.key(), temp));
115 }
116
117 std::shared_ptr<gbp_endpoint_group>
118 gbp_endpoint_group::find(const key_t& k)
119 {
120   return (m_db.find(k));
121 }
122
123 std::shared_ptr<gbp_endpoint_group>
124 gbp_endpoint_group::singular() const
125 {
126   return find_or_add(*this);
127 }
128
129 void
130 gbp_endpoint_group::dump(std::ostream& os)
131 {
132   db_dump(m_db, os);
133 }
134
135 gbp_endpoint_group::event_handler::event_handler()
136 {
137   OM::register_listener(this);
138   inspect::register_handler({ "gbp-endpoint-group" }, "GBP Endpoint_Groups",
139                             this);
140 }
141
142 void
143 gbp_endpoint_group::event_handler::handle_replay()
144 {
145   m_db.replay();
146 }
147
148 void
149 gbp_endpoint_group::event_handler::handle_populate(const client_db::key_t& key)
150 {
151   std::shared_ptr<gbp_endpoint_group_cmds::dump_cmd> cmd =
152     std::make_shared<gbp_endpoint_group_cmds::dump_cmd>();
153
154   HW::enqueue(cmd);
155   HW::write();
156
157   for (auto& record : *cmd) {
158     auto& payload = record.get_payload();
159
160     std::shared_ptr<interface> itf =
161       interface::find(payload.epg.uplink_sw_if_index);
162     std::shared_ptr<route_domain> rd =
163       route_domain::find(payload.epg.ip4_table_id);
164     std::shared_ptr<bridge_domain> bd = bridge_domain::find(payload.epg.bd_id);
165
166     VOM_LOG(log_level_t::DEBUG) << "data: [" << payload.epg.uplink_sw_if_index
167                                 << ", " << payload.epg.ip4_table_id << ", "
168                                 << payload.epg.bd_id << "]";
169
170     if (itf && bd && rd) {
171       gbp_endpoint_group gbpe(payload.epg.epg_id, *itf, *rd, *bd);
172       OM::commit(key, gbpe);
173
174       VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
175     }
176   }
177 }
178
179 dependency_t
180 gbp_endpoint_group::event_handler::order() const
181 {
182   return (dependency_t::ACL);
183 }
184
185 void
186 gbp_endpoint_group::event_handler::show(std::ostream& os)
187 {
188   db_dump(m_db, os);
189 }
190 } // namespace VOM
191
192 /*
193  * fd.io coding-style-patch-verification: ON
194  *
195  * Local Variables:
196  * eval: (c-set-style "mozilla")
197  * End:
198  */