GBP: use sclass in the DP for policy
[vpp.git] / extras / vom / vom / gbp_endpoint.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/gbp_endpoint.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_endpoint_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 singular_db<gbp_endpoint::key_t, gbp_endpoint> gbp_endpoint::m_db;
24
25 gbp_endpoint::event_handler gbp_endpoint::m_evh;
26
27 gbp_endpoint::gbp_endpoint(
28   const interface& itf,
29   const std::vector<boost::asio::ip::address>& ip_addrs,
30   const mac_address_t& mac,
31   const gbp_endpoint_group& epg)
32   : m_hdl(handle_t::INVALID)
33   , m_itf(itf.singular())
34   , m_ips(ip_addrs)
35   , m_mac(mac)
36   , m_epg(epg.singular())
37 {
38 }
39
40 gbp_endpoint::gbp_endpoint(const gbp_endpoint& gbpe)
41   : m_hdl(gbpe.m_hdl)
42   , m_itf(gbpe.m_itf)
43   , m_ips(gbpe.m_ips)
44   , m_mac(gbpe.m_mac)
45   , m_epg(gbpe.m_epg)
46 {
47 }
48
49 gbp_endpoint::~gbp_endpoint()
50 {
51   sweep();
52   m_db.release(key(), this);
53 }
54
55 const gbp_endpoint::key_t
56 gbp_endpoint::key() const
57 {
58   return (std::make_pair(m_itf->key(), m_mac));
59 }
60
61 bool
62 gbp_endpoint::operator==(const gbp_endpoint& gbpe) const
63 {
64   return ((key() == gbpe.key()) && (m_epg == gbpe.m_epg));
65 }
66
67 void
68 gbp_endpoint::sweep()
69 {
70   if (m_hdl) {
71     HW::enqueue(new gbp_endpoint_cmds::delete_cmd(m_hdl));
72   }
73   HW::write();
74 }
75
76 void
77 gbp_endpoint::replay()
78 {
79   if (m_hdl) {
80     HW::enqueue(new gbp_endpoint_cmds::create_cmd(m_hdl, m_itf->handle(), m_ips,
81                                                   m_mac, m_epg->sclass()));
82   }
83 }
84
85 std::string
86 gbp_endpoint::to_string() const
87 {
88   std::ostringstream s;
89   s << "gbp-endpoint:[" << m_itf->to_string() << ", ips:[";
90
91   for (auto ip : m_ips)
92     s << ip.to_string();
93
94   s << "], " << m_mac.to_string() << ", epg:" << m_epg->to_string() << "]";
95
96   return (s.str());
97 }
98
99 void
100 gbp_endpoint::update(const gbp_endpoint& r)
101 {
102   if (rc_t::OK != m_hdl.rc()) {
103     HW::enqueue(new gbp_endpoint_cmds::create_cmd(m_hdl, m_itf->handle(), m_ips,
104                                                   m_mac, m_epg->sclass()));
105   }
106 }
107
108 std::shared_ptr<gbp_endpoint>
109 gbp_endpoint::find_or_add(const gbp_endpoint& temp)
110 {
111   return (m_db.find_or_add(temp.key(), temp));
112 }
113
114 std::shared_ptr<gbp_endpoint>
115 gbp_endpoint::find(const key_t& k)
116 {
117   return (m_db.find(k));
118 }
119
120 std::shared_ptr<gbp_endpoint>
121 gbp_endpoint::singular() const
122 {
123   return find_or_add(*this);
124 }
125
126 void
127 gbp_endpoint::dump(std::ostream& os)
128 {
129   db_dump(m_db, os);
130 }
131
132 gbp_endpoint::event_handler::event_handler()
133 {
134   OM::register_listener(this);
135   inspect::register_handler({ "gbp-endpoint" }, "GBP Endpoints", this);
136 }
137
138 void
139 gbp_endpoint::event_handler::handle_replay()
140 {
141   m_db.replay();
142 }
143
144 void
145 gbp_endpoint::event_handler::handle_populate(const client_db::key_t& key)
146 {
147   std::shared_ptr<gbp_endpoint_cmds::dump_cmd> cmd =
148     std::make_shared<gbp_endpoint_cmds::dump_cmd>();
149
150   HW::enqueue(cmd);
151   HW::write();
152
153   for (auto& record : *cmd) {
154     auto& payload = record.get_payload();
155
156     std::vector<boost::asio::ip::address> addresses;
157
158     for (uint8_t n = 0; n < payload.endpoint.n_ips; n++)
159       addresses.push_back(from_api(payload.endpoint.ips[n]));
160     std::shared_ptr<interface> itf =
161       interface::find(payload.endpoint.sw_if_index);
162     std::shared_ptr<gbp_endpoint_group> epg =
163       gbp_endpoint_group::find(payload.endpoint.sclass);
164     mac_address_t mac = from_api(payload.endpoint.mac);
165
166     VOM_LOG(log_level_t::DEBUG) << "data: " << payload.endpoint.sw_if_index;
167
168     if (itf && epg) {
169       gbp_endpoint gbpe(*itf, addresses, mac, *epg);
170       OM::commit(key, gbpe);
171
172       VOM_LOG(log_level_t::DEBUG) << "read: " << gbpe.to_string();
173     } else {
174       VOM_LOG(log_level_t::ERROR)
175         << "no interface:" << payload.endpoint.sw_if_index
176         << "or sclass:" << payload.endpoint.sclass;
177     }
178   }
179 }
180
181 dependency_t
182 gbp_endpoint::event_handler::order() const
183 {
184   return (dependency_t::ENTRY);
185 }
186
187 void
188 gbp_endpoint::event_handler::show(std::ostream& os)
189 {
190   db_dump(m_db, os);
191 }
192
193 std::ostream&
194 operator<<(std::ostream& os, const gbp_endpoint::key_t& key)
195 {
196   os << key.first << "," << key.second;
197
198   return os;
199 }
200
201 } // namespace VOM
202
203 /*
204  * fd.io coding-style-patch-verification: ON
205  *
206  * Local Variables:
207  * eval: (c-set-style "mozilla")
208  * End:
209  */