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