blob: 7bb02d77fbc4ec24ee278bfbaf728e143cf8fe14 [file] [log] [blame]
Andreas Papazois92e4a042016-02-24 15:29:30 +02001/*
2 *
Brian O'Connor5ab426f2016-04-09 01:19:45 -07003 * * Copyright 2016-present Open Networking Laboratory
Andreas Papazois92e4a042016-02-24 15:29:30 +02004 * *
5 * * Licensed under the Apache License, Version 2.0 (the "License");
6 * * you may not use this file except in compliance with the License.
7 * * You may obtain a copy of the License at
8 * *
9 * * http://www.apache.org/licenses/LICENSE-2.0
10 * *
11 * * Unless required by applicable law or agreed to in writing, software
12 * * distributed under the License is distributed on an "AS IS" BASIS,
13 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * * See the License for the specific language governing permissions and
15 * * limitations under the License.
16 *
17 */
18
19package org.onosproject.drivers.cisco;
20
21import org.onlab.packet.VlanId;
22import org.onosproject.drivers.utilities.XmlConfigParser;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.InterfaceConfig;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.netconf.NetconfController;
27import org.onosproject.netconf.NetconfException;
28import org.onosproject.netconf.NetconfSession;
29import org.slf4j.Logger;
30
31import java.io.ByteArrayInputStream;
32import java.nio.charset.StandardCharsets;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Configures interfaces on Cisco IOS devices.
39 */
40public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
41 implements InterfaceConfig {
42
43 private final Logger log = getLogger(getClass());
44
45 /**
46 * Adds an interface to a VLAN.
47 * @param deviceId the device ID
48 * @param intf the name of the interface
49 * @param vlanId the VLAN ID
50 * @return the result of operation
51 */
52 @Override
53 public boolean addInterfaceToVlan(DeviceId deviceId, String intf, VlanId vlanId) {
54 NetconfController controller = checkNotNull(handler()
55 .get(NetconfController.class));
56
57 NetconfSession session = controller.getDevicesMap().get(handler()
58 .data().deviceId()).getSession();
59 String reply;
60 try {
Andreas Papazois9492abb2016-03-11 09:38:41 +020061 reply = session.requestSync(addInterfaceToVlanBuilder(intf, vlanId));
Andreas Papazois92e4a042016-02-24 15:29:30 +020062 } catch (NetconfException e) {
63 log.error("Failed to configure VLAN ID {} on device {} interface {}.",
64 vlanId, deviceId, intf, e);
65 return false;
66 }
67
68 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
69 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
70 }
71
72 /**
73 * Builds a request to add an interface to a VLAN.
74 * @param intf the name of the interface
75 * @param vlanId the VLAN ID
76 * @return the request string.
77 */
78 private String addInterfaceToVlanBuilder(String intf, VlanId vlanId) {
79 StringBuilder rpc =
80 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
81 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
82 rpc.append("<edit-config>");
83 rpc.append("<target>");
84 rpc.append("<running/>");
85 rpc.append("</target>");
86 rpc.append("<config>");
87 rpc.append("<xml-config-data>");
88 rpc.append("<Device-Configuration><interface><Param>");
89 rpc.append(intf);
90 rpc.append("</Param>");
91 rpc.append("<ConfigIf-Configuration>");
92 rpc.append("<switchport><access><vlan><VLANIDVLANPortAccessMode>");
93 rpc.append(vlanId);
94 rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>");
95 rpc.append("<switchport><mode><access/></mode></switchport>");
96 rpc.append("</ConfigIf-Configuration>");
97 rpc.append("</interface>");
98 rpc.append("</Device-Configuration>");
99 rpc.append("</xml-config-data>");
100 rpc.append("</config>");
101 rpc.append("</edit-config>");
102 rpc.append("</rpc>");
103
104 return rpc.toString();
105 }
106
107 /**
108 * Removes an interface from a VLAN.
109 * @param deviceId the device ID
110 * @param intf the name of the interface
111 * @param vlanId the VLAN ID
112 * @return the result of operation
113 */
114 @Override
115 public boolean removeInterfaceFromVlan(DeviceId deviceId, String intf,
116 VlanId vlanId) {
117 NetconfController controller = checkNotNull(handler()
118 .get(NetconfController.class));
119
120 NetconfSession session = controller.getDevicesMap().get(handler()
121 .data().deviceId()).getSession();
122 String reply;
123 try {
Andreas Papazois9492abb2016-03-11 09:38:41 +0200124 reply = session.requestSync(removeInterfaceFromVlanBuilder(intf, vlanId));
Andreas Papazois92e4a042016-02-24 15:29:30 +0200125 } catch (NetconfException e) {
126 log.error("Failed to remove VLAN ID {} from device {} interface {}.",
127 vlanId, deviceId, intf, e);
128 return false;
129 }
130
131 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
132 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
133 }
134
135 /**
136 * Builds a request to remove an interface from a VLAN.
137 * @param intf the name of the interface
138 * @param vlanId the VLAN ID
139 * @return the request string.
140 */
141 private String removeInterfaceFromVlanBuilder(String intf, VlanId vlanId) {
142 StringBuilder rpc =
143 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
144 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
145 rpc.append("<edit-config>");
146 rpc.append("<target>");
147 rpc.append("<running/>");
148 rpc.append("</target>");
149 rpc.append("<config>");
150 rpc.append("<xml-config-data>");
151 rpc.append("<Device-Configuration><interface><Param>");
152 rpc.append(intf);
153 rpc.append("</Param>");
154 rpc.append("<ConfigIf-Configuration>");
155 rpc.append("<switchport operation=\"delete\"><access><vlan><VLANIDVLANPortAccessMode>");
156 rpc.append(vlanId);
157 rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>");
158 rpc.append("<switchport operation=\"delete\"><mode><access/></mode></switchport>");
159 rpc.append("</ConfigIf-Configuration>");
160 rpc.append("</interface>");
161 rpc.append("</Device-Configuration>");
162 rpc.append("</xml-config-data>");
163 rpc.append("</config>");
164 rpc.append("</edit-config>");
165 rpc.append("</rpc>");
166
167 return rpc.toString();
168 }
169
170 /**
171 * Configures an interface as trunk for VLAN.
172 * @param deviceId the device ID
173 * @param intf the name of the interface
174 * @param vlanId the VLAN ID
175 * @return the result of operation
176 */
177 @Override
178 public boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
179 NetconfController controller = checkNotNull(handler()
180 .get(NetconfController.class));
181
182 NetconfSession session = controller.getDevicesMap().get(handler()
183 .data().deviceId()).getSession();
184 String reply;
185 try {
Andreas Papazois9492abb2016-03-11 09:38:41 +0200186 reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanId));
Andreas Papazois92e4a042016-02-24 15:29:30 +0200187 } catch (NetconfException e) {
188 log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.",
189 vlanId, deviceId, intf, e);
190 return false;
191 }
192
193 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
194 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
195 }
196
197 /**
198 * Builds a request to configure an interface as trunk for VLAN.
199 * @param intf the name of the interface
200 * @param vlanId the VLAN ID
201 * @return the request string.
202 */
203 private String addTrunkInterfaceBuilder(String intf, VlanId vlanId) {
204 StringBuilder rpc =
205 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
206 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
207 rpc.append("<edit-config>");
208 rpc.append("<target>");
209 rpc.append("<running/>");
210 rpc.append("</target>");
211 rpc.append("<config>");
212 rpc.append("<xml-config-data>");
213 rpc.append("<Device-Configuration><interface><Param>");
214 rpc.append(intf);
215 rpc.append("</Param>");
216 rpc.append("<ConfigIf-Configuration>");
217 rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>");
218 rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>");
219 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
220 rpc.append(vlanId);
221 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>");
222 rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>");
223 rpc.append("</ConfigIf-Configuration>");
224 rpc.append("</interface>");
225 rpc.append("</Device-Configuration>");
226 rpc.append("</xml-config-data>");
227 rpc.append("</config>");
228 rpc.append("</edit-config>");
229 rpc.append("</rpc>");
230
231 return rpc.toString();
232 }
233
234 /**
235 * Removes trunk mode configuration for VLAN from an interface.
236 * @param deviceId the device ID
237 * @param intf the name of the interface
238 * @param vlanId the VLAN ID
239 * @return the result of operation
240 */
241 @Override
242 public boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
243 NetconfController controller = checkNotNull(handler()
244 .get(NetconfController.class));
245
246 NetconfSession session = controller.getDevicesMap().get(handler()
247 .data().deviceId()).getSession();
248 String reply;
249 try {
Andreas Papazois9492abb2016-03-11 09:38:41 +0200250 reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanId));
Andreas Papazois92e4a042016-02-24 15:29:30 +0200251 } catch (NetconfException e) {
252 log.error("Failed to remove trunk mode for VLAN ID {} on device {} interface {}.",
253 vlanId, deviceId, intf, e);
254 return false;
255 }
256
257 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
258 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
259}
260
261 /**
262 * Builds a request to remove trunk mode configuration for VLAN from an interface.
263 * @param intf the name of the interface
264 * @param vlanId the VLAN ID
265 * @return the request string.
266 */
267 private String removeTrunkInterfaceBuilder(String intf, VlanId vlanId) {
268 StringBuilder rpc =
269 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
270 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
271 rpc.append("<edit-config>");
272 rpc.append("<target>");
273 rpc.append("<running/>");
274 rpc.append("</target>");
275 rpc.append("<config>");
276 rpc.append("<xml-config-data>");
277 rpc.append("<Device-Configuration><interface><Param>");
278 rpc.append(intf);
279 rpc.append("</Param>");
280 rpc.append("<ConfigIf-Configuration>");
281 rpc.append("<switchport><mode operation=\"delete\"><trunk/></mode></switchport>");
282 rpc.append("<switchport><trunk operation=\"delete\"><encapsulation>");
283 rpc.append("<dot1q/></encapsulation></trunk></switchport>");
284 rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>");
285 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
286 rpc.append(vlanId);
287 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>");
288 rpc.append("</trunk></switchport></ConfigIf-Configuration>");
289 rpc.append("</interface>");
290 rpc.append("</Device-Configuration>");
291 rpc.append("</xml-config-data>");
292 rpc.append("</config>");
293 rpc.append("</edit-config>");
294 rpc.append("</rpc>");
295
296 return rpc.toString();
297 }
298
299}
300