blob: 6df10763e7309c6a72deb3072ee5daea64930f4a [file] [log] [blame]
Andreas Papazois1dff77c2016-02-16 16:27:33 +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 SM-X devices.
39 */
40public class InterfaceConfigCiscoSmXImpl 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 reply = session.requestSync(addInterfaceToVlanBuilder(intf, vlanId)).trim();
62 } catch (NetconfException e) {
63 log.error("Failed to configure VLAN ID {} on device {} port {}.",
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 crafted 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 = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
80 rpc.append("<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 * Configures an interface as trunk for 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 addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
116 NetconfController controller = checkNotNull(handler()
117 .get(NetconfController.class));
118
119 NetconfSession session = controller.getDevicesMap().get(handler()
120 .data().deviceId()).getSession();
121 String reply;
122 try {
123 reply = session.requestSync(addTrunkInterface(intf, vlanId)).trim();
124 } catch (NetconfException e) {
125 log.error("Failed to configure VLAN ID {} on device {} port {}.",
126 vlanId, deviceId, intf, e);
127 return false;
128 }
129
130 return XmlConfigParser.configSuccess(XmlConfigParser.loadXml(
131 new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
132 }
133
134 /**
135 * Builds a request crafted to configure an interface as trunk for VLAN.
136 * @param intf the name of the interface
137 * @param vlanId the VLAN ID
138 * @return the request string.
139 */
140 private String addTrunkInterface(String intf, VlanId vlanId) {
141 StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
142 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
143 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
144 rpc.append("<edit-config>");
145 rpc.append("<target>");
146 rpc.append("<running/>");
147 rpc.append("</target>");
148 rpc.append("<config>");
149 rpc.append("<xml-config-data>");
150 rpc.append("<Device-Configuration><interface><Param>");
151 rpc.append(intf);
152 rpc.append("</Param>");
153 rpc.append("<ConfigIf-Configuration>");
154 rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation></trunk></switchport>");
155 rpc.append("<switchport><trunk><allowed><vlan><VLANIDsAllowedVLANsPortTrunkingMode>");
156 rpc.append(vlanId);
157 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk></switchport>");
158 rpc.append("<switchport><mode><trunk/></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