blob: 4a9cced6ac49a6287981479138727c69dd1e633b [file] [log] [blame]
Andreas Papazois92e4a042016-02-24 15:29:30 +02001/*
2 *
3 * * Copyright 2016 Open Networking Laboratory
4 * *
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 {
61 //TODO remove XML triming if preceeds in Session
62 reply = session.requestSync(addInterfaceToVlanBuilder(intf, vlanId)).trim();
63 } catch (NetconfException e) {
64 log.error("Failed to configure VLAN ID {} on device {} interface {}.",
65 vlanId, deviceId, intf, e);
66 return false;
67 }
68
69 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
70 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
71 }
72
73 /**
74 * Builds a request to add an interface to a VLAN.
75 * @param intf the name of the interface
76 * @param vlanId the VLAN ID
77 * @return the request string.
78 */
79 private String addInterfaceToVlanBuilder(String intf, VlanId vlanId) {
80 StringBuilder rpc =
81 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
82 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
83 rpc.append("<edit-config>");
84 rpc.append("<target>");
85 rpc.append("<running/>");
86 rpc.append("</target>");
87 rpc.append("<config>");
88 rpc.append("<xml-config-data>");
89 rpc.append("<Device-Configuration><interface><Param>");
90 rpc.append(intf);
91 rpc.append("</Param>");
92 rpc.append("<ConfigIf-Configuration>");
93 rpc.append("<switchport><access><vlan><VLANIDVLANPortAccessMode>");
94 rpc.append(vlanId);
95 rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>");
96 rpc.append("<switchport><mode><access/></mode></switchport>");
97 rpc.append("</ConfigIf-Configuration>");
98 rpc.append("</interface>");
99 rpc.append("</Device-Configuration>");
100 rpc.append("</xml-config-data>");
101 rpc.append("</config>");
102 rpc.append("</edit-config>");
103 rpc.append("</rpc>");
104
105 return rpc.toString();
106 }
107
108 /**
109 * Removes an interface from a VLAN.
110 * @param deviceId the device ID
111 * @param intf the name of the interface
112 * @param vlanId the VLAN ID
113 * @return the result of operation
114 */
115 @Override
116 public boolean removeInterfaceFromVlan(DeviceId deviceId, String intf,
117 VlanId vlanId) {
118 NetconfController controller = checkNotNull(handler()
119 .get(NetconfController.class));
120
121 NetconfSession session = controller.getDevicesMap().get(handler()
122 .data().deviceId()).getSession();
123 String reply;
124 try {
125 //TODO remove XML triming if preceeds in Session
126 reply = session.requestSync(removeInterfaceFromVlanBuilder(intf, vlanId)).trim();
127 } catch (NetconfException e) {
128 log.error("Failed to remove VLAN ID {} from device {} interface {}.",
129 vlanId, deviceId, intf, e);
130 return false;
131 }
132
133 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
134 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
135 }
136
137 /**
138 * Builds a request to remove an interface from a VLAN.
139 * @param intf the name of the interface
140 * @param vlanId the VLAN ID
141 * @return the request string.
142 */
143 private String removeInterfaceFromVlanBuilder(String intf, VlanId vlanId) {
144 StringBuilder rpc =
145 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
146 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
147 rpc.append("<edit-config>");
148 rpc.append("<target>");
149 rpc.append("<running/>");
150 rpc.append("</target>");
151 rpc.append("<config>");
152 rpc.append("<xml-config-data>");
153 rpc.append("<Device-Configuration><interface><Param>");
154 rpc.append(intf);
155 rpc.append("</Param>");
156 rpc.append("<ConfigIf-Configuration>");
157 rpc.append("<switchport operation=\"delete\"><access><vlan><VLANIDVLANPortAccessMode>");
158 rpc.append(vlanId);
159 rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>");
160 rpc.append("<switchport operation=\"delete\"><mode><access/></mode></switchport>");
161 rpc.append("</ConfigIf-Configuration>");
162 rpc.append("</interface>");
163 rpc.append("</Device-Configuration>");
164 rpc.append("</xml-config-data>");
165 rpc.append("</config>");
166 rpc.append("</edit-config>");
167 rpc.append("</rpc>");
168
169 return rpc.toString();
170 }
171
172 /**
173 * Configures an interface as trunk for VLAN.
174 * @param deviceId the device ID
175 * @param intf the name of the interface
176 * @param vlanId the VLAN ID
177 * @return the result of operation
178 */
179 @Override
180 public boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
181 NetconfController controller = checkNotNull(handler()
182 .get(NetconfController.class));
183
184 NetconfSession session = controller.getDevicesMap().get(handler()
185 .data().deviceId()).getSession();
186 String reply;
187 try {
188 //TODO remove XML triming if preceeds in Session
189 reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanId)).trim();
190 } catch (NetconfException e) {
191 log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.",
192 vlanId, deviceId, intf, e);
193 return false;
194 }
195
196 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
197 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
198 }
199
200 /**
201 * Builds a request to configure an interface as trunk for VLAN.
202 * @param intf the name of the interface
203 * @param vlanId the VLAN ID
204 * @return the request string.
205 */
206 private String addTrunkInterfaceBuilder(String intf, VlanId vlanId) {
207 StringBuilder rpc =
208 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
209 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
210 rpc.append("<edit-config>");
211 rpc.append("<target>");
212 rpc.append("<running/>");
213 rpc.append("</target>");
214 rpc.append("<config>");
215 rpc.append("<xml-config-data>");
216 rpc.append("<Device-Configuration><interface><Param>");
217 rpc.append(intf);
218 rpc.append("</Param>");
219 rpc.append("<ConfigIf-Configuration>");
220 rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>");
221 rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>");
222 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
223 rpc.append(vlanId);
224 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>");
225 rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>");
226 rpc.append("</ConfigIf-Configuration>");
227 rpc.append("</interface>");
228 rpc.append("</Device-Configuration>");
229 rpc.append("</xml-config-data>");
230 rpc.append("</config>");
231 rpc.append("</edit-config>");
232 rpc.append("</rpc>");
233
234 return rpc.toString();
235 }
236
237 /**
238 * Removes trunk mode configuration for VLAN from an interface.
239 * @param deviceId the device ID
240 * @param intf the name of the interface
241 * @param vlanId the VLAN ID
242 * @return the result of operation
243 */
244 @Override
245 public boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
246 NetconfController controller = checkNotNull(handler()
247 .get(NetconfController.class));
248
249 NetconfSession session = controller.getDevicesMap().get(handler()
250 .data().deviceId()).getSession();
251 String reply;
252 try {
253 //TODO remove XML triming if preceeds in Session
254 reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanId)).trim();
255 } catch (NetconfException e) {
256 log.error("Failed to remove trunk mode for VLAN ID {} on device {} interface {}.",
257 vlanId, deviceId, intf, e);
258 return false;
259 }
260
261 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
262 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
263}
264
265 /**
266 * Builds a request to remove trunk mode configuration for VLAN from an interface.
267 * @param intf the name of the interface
268 * @param vlanId the VLAN ID
269 * @return the request string.
270 */
271 private String removeTrunkInterfaceBuilder(String intf, VlanId vlanId) {
272 StringBuilder rpc =
273 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
274 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
275 rpc.append("<edit-config>");
276 rpc.append("<target>");
277 rpc.append("<running/>");
278 rpc.append("</target>");
279 rpc.append("<config>");
280 rpc.append("<xml-config-data>");
281 rpc.append("<Device-Configuration><interface><Param>");
282 rpc.append(intf);
283 rpc.append("</Param>");
284 rpc.append("<ConfigIf-Configuration>");
285 rpc.append("<switchport><mode operation=\"delete\"><trunk/></mode></switchport>");
286 rpc.append("<switchport><trunk operation=\"delete\"><encapsulation>");
287 rpc.append("<dot1q/></encapsulation></trunk></switchport>");
288 rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>");
289 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
290 rpc.append(vlanId);
291 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>");
292 rpc.append("</trunk></switchport></ConfigIf-Configuration>");
293 rpc.append("</interface>");
294 rpc.append("</Device-Configuration>");
295 rpc.append("</xml-config-data>");
296 rpc.append("</config>");
297 rpc.append("</edit-config>");
298 rpc.append("</rpc>");
299
300 return rpc.toString();
301 }
302
303}
304