Use IP and MAC API types for neighbors
[vpp.git] / extras / vom / vom / neighbour_cmds.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/neighbour_cmds.hpp"
17 #include "vom/api_types.hpp"
18
19 namespace VOM {
20 namespace neighbour_cmds {
21 create_cmd::create_cmd(HW::item<bool>& item,
22                        handle_t itf,
23                        const mac_address_t& mac,
24                        const boost::asio::ip::address& ip_addr)
25   : rpc_cmd(item)
26   , m_itf(itf)
27   , m_mac(mac)
28   , m_ip_addr(ip_addr)
29 {
30 }
31
32 bool
33 create_cmd::operator==(const create_cmd& other) const
34 {
35   return ((m_mac == other.m_mac) && (m_ip_addr == other.m_ip_addr) &&
36           (m_itf == other.m_itf));
37 }
38
39 rc_t
40 create_cmd::issue(connection& con)
41 {
42   msg_t req(con.ctx(), std::ref(*this));
43
44   auto& payload = req.get_request().get_payload();
45   payload.is_add = 1;
46   payload.neighbor.sw_if_index = m_itf.value();
47   payload.neighbor.flags = IP_API_NEIGHBOR_FLAG_STATIC;
48
49   to_api(m_mac, payload.neighbor.mac_address);
50   to_api(m_ip_addr, payload.neighbor.ip_address);
51
52   VAPI_CALL(req.execute());
53
54   return (wait());
55 }
56
57 std::string
58 create_cmd::to_string() const
59 {
60   std::ostringstream s;
61   s << "nieghbour-create: " << m_hw_item.to_string()
62     << " itf:" << m_itf.to_string() << " mac:" << m_mac.to_string()
63     << " ip:" << m_ip_addr.to_string();
64
65   return (s.str());
66 }
67
68 delete_cmd::delete_cmd(HW::item<bool>& item,
69                        handle_t itf,
70                        const mac_address_t& mac,
71                        const boost::asio::ip::address& ip_addr)
72   : rpc_cmd(item)
73   , m_itf(itf)
74   , m_mac(mac)
75   , m_ip_addr(ip_addr)
76 {
77 }
78
79 bool
80 delete_cmd::operator==(const delete_cmd& other) const
81 {
82   return ((m_mac == other.m_mac) && (m_ip_addr == other.m_ip_addr) &&
83           (m_itf == other.m_itf));
84 }
85
86 rc_t
87 delete_cmd::issue(connection& con)
88 {
89   msg_t req(con.ctx(), std::ref(*this));
90
91   auto& payload = req.get_request().get_payload();
92   payload.is_add = 0;
93   payload.neighbor.sw_if_index = m_itf.value();
94   payload.neighbor.flags = IP_API_NEIGHBOR_FLAG_STATIC;
95
96   to_api(m_mac, payload.neighbor.mac_address);
97   to_api(m_ip_addr, payload.neighbor.ip_address);
98
99   VAPI_CALL(req.execute());
100
101   wait();
102   m_hw_item.set(rc_t::NOOP);
103
104   return rc_t::OK;
105 }
106
107 std::string
108 delete_cmd::to_string() const
109 {
110   std::ostringstream s;
111   s << "neighbour-delete: " << m_hw_item.to_string()
112     << " itf:" << m_itf.to_string() << " mac:" << m_mac.to_string()
113     << " ip:" << m_ip_addr.to_string();
114
115   return (s.str());
116 }
117
118 dump_cmd::dump_cmd(const handle_t& hdl, const l3_proto_t& proto)
119   : m_itf(hdl)
120   , m_proto(proto)
121 {
122 }
123
124 dump_cmd::dump_cmd(const dump_cmd& d)
125   : m_itf(d.m_itf)
126   , m_proto(d.m_proto)
127 {
128 }
129
130 bool
131 dump_cmd::operator==(const dump_cmd& other) const
132 {
133   return (true);
134 }
135
136 rc_t
137 dump_cmd::issue(connection& con)
138 {
139   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
140
141   auto& payload = m_dump->get_request().get_payload();
142   payload.sw_if_index = m_itf.value();
143   payload.is_ipv6 = m_proto.is_ipv6();
144
145   VAPI_CALL(m_dump->execute());
146
147   wait();
148
149   return rc_t::OK;
150 }
151
152 std::string
153 dump_cmd::to_string() const
154 {
155   std::ostringstream s;
156
157   s << "neighbour-dump: " << m_itf.to_string() << " " << m_proto.to_string();
158
159   return (s.str());
160 }
161 } // namespace neighbour_cmds
162 } // namespace vom
163
164 /*
165  * fd.io coding-style-patch-verification: ON
166  *
167  * Local Variables:
168  * eval: (c-set-style "mozilla")
169  * End:
170  */