Andreas Papazois
Committed by Gerrit Code Review

[GEANT] Interface configuration for Cisco devices.

Change-Id: Ieb9979c3c4d7ebbf996d787a1528ba0548342572
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.cli.net;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.net.DeviceId;
24 +import org.onosproject.net.behaviour.InterfaceConfig;
25 +import org.onosproject.net.driver.DriverHandler;
26 +import org.onosproject.net.driver.DriverService;
27 +
28 +/**
29 + * Configures a device interface.
30 + */
31 +@Command(scope = "onos", name = "device-add-interface",
32 + description = "Configures a device interface")
33 +public class DeviceInterfaceAddCommand extends AbstractShellCommand {
34 +
35 + private static final String CONFIG_VLAN_SUCCESS =
36 + "VLAN %s set on device %s interface %s.";
37 + private static final String CONFIG_VLAN_FAILURE =
38 + "Failed to set VLAN %s on device %s interface %s.";
39 +
40 + private static final String CONFIG_TRUNK_SUCCESS =
41 + "Trunk mode set for VLAN %s on device %s interface %s.";
42 + private static final String CONFIG_TRUNK_FAILURE =
43 + "Failed to set trunk mode for VLAN %s on device %s interface %s.";
44 +
45 + @Argument(index = 0, name = "uri", description = "Device ID",
46 + required = true, multiValued = false)
47 + private String uri = null;
48 +
49 + @Argument(index = 1, name = "interface",
50 + description = "Interface name",
51 + required = true, multiValued = false)
52 + private String portName = null;
53 +
54 + @Argument(index = 2, name = "vlan",
55 + description = "VLAN ID",
56 + required = true, multiValued = false)
57 + private String vlanString = null;
58 +
59 + @Option(name = "-t", aliases = "--trunk",
60 + description = "Configure interface as trunk for VLAN",
61 + required = false, multiValued = false)
62 + private boolean trunkMode = false;
63 +
64 + @Override
65 + protected void execute() {
66 + DriverService service = get(DriverService.class);
67 + DeviceId deviceId = DeviceId.deviceId(uri);
68 + DriverHandler h = service.createHandler(deviceId);
69 + InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
70 +
71 + VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString));
72 +
73 + if (trunkMode) {
74 + // Trunk mode to be enabled for VLAN.
75 + if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanId)) {
76 + print(CONFIG_TRUNK_SUCCESS, vlanId, deviceId, portName);
77 + } else {
78 + print(CONFIG_TRUNK_FAILURE, vlanId, deviceId, portName);
79 + }
80 + return;
81 + }
82 +
83 + // VLAN to be added to interface.
84 + if (interfaceConfig.addInterfaceToVlan(deviceId, portName, vlanId)) {
85 + print(CONFIG_VLAN_SUCCESS, vlanId, deviceId, portName);
86 + } else {
87 + print(CONFIG_VLAN_FAILURE, vlanId, deviceId, portName);
88 + }
89 + }
90 +
91 +}
...@@ -155,6 +155,12 @@ ...@@ -155,6 +155,12 @@
155 </completers> 155 </completers>
156 </command> 156 </command>
157 <command> 157 <command>
158 + <action class="org.onosproject.cli.net.DeviceInterfaceAddCommand"/>
159 + <completers>
160 + <ref component-id="deviceIdCompleter"/>
161 + </completers>
162 + </command>
163 + <command>
158 <action class="org.onosproject.cli.net.AddMeter"/> 164 <action class="org.onosproject.cli.net.AddMeter"/>
159 <completers> 165 <completers>
160 <ref component-id="deviceIdCompleter"/> 166 <ref component-id="deviceIdCompleter"/>
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.behaviour;
17 +
18 +import org.onlab.packet.VlanId;
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.driver.HandlerBehaviour;
21 +
22 +/**
23 + * Means to configure interfaces on devices.
24 + */
25 +public interface InterfaceConfig extends HandlerBehaviour {
26 +
27 + /**
28 + * Adds an interface to a VLAN.
29 + * @param deviceId the device ID
30 + * @param intf the name of the interface
31 + * @param vlanId the VLAN ID
32 + * @return the result of operation
33 + */
34 + boolean addInterfaceToVlan(DeviceId deviceId, String intf, VlanId vlanId);
35 +
36 + /**
37 + * Configures an interface as trunk for VLAN.
38 + * @param deviceId the device ID
39 + * @param intf the name of the interface
40 + * @param vlanId the VLAN ID
41 + * @return the result of operation
42 + */
43 + boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId);
44 +
45 + /* TODO Addition of more methods to make the behavior symmetrical.
46 + Methods removeVlanFromInterface, getInterfacesForVlan, getVlansForInterface
47 + should be added to complete the behavior.
48 + */
49 +}
1 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
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 +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
18 + <feature name="${project.artifactId}" version="${project.version}"
19 + description="${project.description}">
20 + <feature>onos-api</feature>
21 + <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
22 +
23 + <bundle>mvn:${project.groupId}/onos-drivers-utilities/${project.version}</bundle>
24 + <bundle>mvn:${project.groupId}/onos-netconf-api/${project.version}</bundle>
25 + </feature>
26 +</features>
1 +<?xml version="1.0" encoding="UTF-8"?>
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 +<project xmlns="http://maven.apache.org/POM/4.0.0"
19 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21 + <parent>
22 + <artifactId>onos-drivers-general</artifactId>
23 + <groupId>org.onosproject</groupId>
24 + <version>1.5.0-SNAPSHOT</version>
25 + </parent>
26 + <modelVersion>4.0.0</modelVersion>
27 +
28 + <artifactId>onos-drivers-cisco</artifactId>
29 + <packaging>bundle</packaging>
30 +
31 + <description>Cisco device drivers</description>
32 +
33 + <properties>
34 + <onos.app.name>org.onosproject.drivers.cisco</onos.app.name>
35 + </properties>
36 +
37 + <dependencies>
38 + <dependency>
39 + <groupId>org.onosproject</groupId>
40 + <artifactId>onos-drivers-utilities</artifactId>
41 + <version>${project.version}</version>
42 + </dependency>
43 + <dependency>
44 + <groupId>org.onosproject</groupId>
45 + <artifactId>onos-netconf-api</artifactId>
46 + <version>${project.version}</version>
47 + </dependency>
48 + </dependencies>
49 +
50 +</project>
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.drivers.cisco;
18 +
19 +import org.apache.felix.scr.annotations.Component;
20 +import org.onosproject.net.driver.AbstractDriverLoader;
21 +
22 +/**
23 + * Loader for Cisco device drivers.
24 + */
25 +@Component(immediate = true)
26 +public class CiscoDriversLoader extends AbstractDriverLoader {
27 + public CiscoDriversLoader() {
28 + super("/cisco-drivers.xml");
29 + }
30 +}
1 +/*
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 +
19 +package org.onosproject.drivers.cisco;
20 +
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.drivers.utilities.XmlConfigParser;
23 +import org.onosproject.net.DeviceId;
24 +import org.onosproject.net.behaviour.InterfaceConfig;
25 +import org.onosproject.net.driver.AbstractHandlerBehaviour;
26 +import org.onosproject.netconf.NetconfController;
27 +import org.onosproject.netconf.NetconfException;
28 +import org.onosproject.netconf.NetconfSession;
29 +import org.slf4j.Logger;
30 +
31 +import java.io.ByteArrayInputStream;
32 +import java.nio.charset.StandardCharsets;
33 +
34 +import static com.google.common.base.Preconditions.checkNotNull;
35 +import static org.slf4j.LoggerFactory.getLogger;
36 +
37 +/**
38 + * Configures interfaces on Cisco SM-X devices.
39 + */
40 +public 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 +
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Package for Cisco device drivers.
19 + */
20 +package org.onosproject.drivers.cisco;
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="UTF-8"?>
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 +<drivers>
18 + <driver name="cisco-netconf" extends="netconf" manufacturer="Cisco"
19 + hwVersion="SM-X" swVersion="IOS 15">
20 + <behaviour api="org.onosproject.net.behaviour.InterfaceConfig"
21 + impl="org.onosproject.drivers.cisco.InterfaceConfigCiscoSmXImpl"/>
22 + </driver>
23 +</drivers>
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
35 <module>default</module> 35 <module>default</module>
36 <module>ciena</module> 36 <module>ciena</module>
37 <module>fujitsu</module> 37 <module>fujitsu</module>
38 + <module>cisco</module>
38 <module>netconf</module> 39 <module>netconf</module>
39 <module>ovsdb</module> 40 <module>ovsdb</module>
40 <module>utilities</module> 41 <module>utilities</module>
......
...@@ -226,4 +226,18 @@ public final class XmlConfigParser { ...@@ -226,4 +226,18 @@ public final class XmlConfigParser {
226 return speed / 1000; 226 return speed / 1000;
227 } 227 }
228 //TODO implement mor methods for parsing configuration when you need them 228 //TODO implement mor methods for parsing configuration when you need them
229 +
230 + /**
231 + * Parses a config reply and returns the result.
232 + * @param reply a tree-like source
233 + * @return the configuration result
234 + */
235 + public static boolean configSuccess(HierarchicalConfiguration reply) {
236 + if (reply != null) {
237 + if (reply.containsKey("ok")) {
238 + return true;
239 + }
240 + }
241 + return false;
242 + }
229 } 243 }
......