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