samuel
Committed by Gerrit Code Review

[onos-2116]the implementation of BridgeConfig and modify onos/pom.xml to

add ovsdb module.

Change-Id: I1a1202e4aa4f04d5dbd4aceb6dacccc571e73f39
...@@ -57,7 +57,11 @@ ...@@ -57,7 +57,11 @@
57 <artifactId>onos-core-serializers</artifactId> 57 <artifactId>onos-core-serializers</artifactId>
58 <version>1.3.0-SNAPSHOT</version> 58 <version>1.3.0-SNAPSHOT</version>
59 </dependency> 59 </dependency>
60 - 60 + <dependency>
61 + <groupId>org.onosproject</groupId>
62 + <artifactId>onos-ovsdb-api</artifactId>
63 + <version>${project.version}</version>
64 + </dependency>
61 <dependency> 65 <dependency>
62 <groupId>org.easymock</groupId> 66 <groupId>org.easymock</groupId>
63 <artifactId>easymock</artifactId> 67 <artifactId>easymock</artifactId>
......
1 +/*
2 + * Copyright 2015 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.driver.ovsdb;
17 +
18 +import java.util.Collection;
19 +import java.util.Set;
20 +
21 +import org.onlab.packet.IpAddress;
22 +import org.onosproject.net.DeviceId;
23 +import org.onosproject.net.PortNumber;
24 +import org.onosproject.net.behaviour.BridgeConfig;
25 +import org.onosproject.net.behaviour.BridgeDescription;
26 +import org.onosproject.net.behaviour.BridgeName;
27 +import org.onosproject.net.behaviour.DefaultBridgeDescription;
28 +import org.onosproject.net.device.DefaultPortDescription;
29 +import org.onosproject.net.device.PortDescription;
30 +import org.onosproject.net.driver.AbstractHandlerBehaviour;
31 +import org.onosproject.net.driver.DriverHandler;
32 +import org.onosproject.ovsdb.controller.OvsdbBridge;
33 +import org.onosproject.ovsdb.controller.OvsdbClientService;
34 +import org.onosproject.ovsdb.controller.OvsdbController;
35 +import org.onosproject.ovsdb.controller.OvsdbNodeId;
36 +import org.onosproject.ovsdb.controller.OvsdbPort;
37 +
38 +import com.google.common.collect.Sets;
39 +
40 +/**
41 + * The implementation of BridageConfig.
42 + */
43 +public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
44 + implements BridgeConfig {
45 +
46 + @Override
47 + public void addBridge(BridgeName bridgeName) {
48 + DriverHandler handler = handler();
49 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
50 + ovsdbNode.createBridge(bridgeName.name());
51 + }
52 +
53 + @Override
54 + public void deleteBridge(BridgeName bridgeName) {
55 + DriverHandler handler = handler();
56 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
57 + ovsdbNode.dropBridge(bridgeName.name());
58 + }
59 +
60 + @Override
61 + public Collection<BridgeDescription> getBridges() {
62 + DriverHandler handler = handler();
63 + DeviceId deviceId = handler.data().deviceId();
64 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
65 + Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
66 + Collection<BridgeDescription> bridges = Sets.newHashSet();
67 + ovsdbSet.forEach(o -> {
68 + BridgeName bridgeName = BridgeName.bridgeName(o.bridgeName()
69 + .toString());
70 + DeviceId ownDeviceId = DeviceId.deviceId(o.datapathId().toString());
71 + BridgeDescription description = new DefaultBridgeDescription(
72 + bridgeName,
73 + deviceId,
74 + ownDeviceId);
75 + bridges.add(description);
76 + });
77 + return bridges;
78 + }
79 +
80 + @Override
81 + public void addPort(PortDescription port) {
82 + DriverHandler handler = handler();
83 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
84 + Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
85 + if (ovsdbSet != null && ovsdbSet.size() > 0) {
86 + OvsdbBridge bridge = ovsdbSet.iterator().next();
87 + ovsdbNode.createPort(bridge.bridgeName().toString(), port
88 + .portNumber().toString());
89 + }
90 + }
91 +
92 + @Override
93 + public void deletePort(PortDescription port) {
94 + DriverHandler handler = handler();
95 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
96 + Set<OvsdbBridge> ovsdbSet = ovsdbNode.getBridges();
97 + if (ovsdbSet != null && ovsdbSet.size() > 0) {
98 + OvsdbBridge bridge = ovsdbSet.iterator().next();
99 + ovsdbNode.dropPort(bridge.bridgeName().toString(), port
100 + .portNumber().toString());
101 + }
102 + }
103 +
104 + @Override
105 + public Collection<PortDescription> getPorts() {
106 + DriverHandler handler = handler();
107 + OvsdbClientService ovsdbNode = getOvsdbNode(handler);
108 + Set<OvsdbPort> ovsdbSet = ovsdbNode.getPorts();
109 + Collection<PortDescription> ports = Sets.newHashSet();
110 + ovsdbSet.forEach(o -> {
111 + PortNumber port = PortNumber.portNumber(o.portNumber().value());
112 + PortDescription description = new DefaultPortDescription(port, true);
113 + ports.add(description);
114 + });
115 + return ports;
116 + }
117 +
118 + // OvsdbNodeId(IP:port) is used in the adaptor while DeviceId(ovsdb:IP:port)
119 + // is used in the core. So DeviceId need be changed to OvsdbNodeId.
120 + private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
121 + int lastColon = deviceId.toString().lastIndexOf(":");
122 + int fistColon = deviceId.toString().indexOf(":");
123 + String ip = deviceId.toString().substring(fistColon + 1, lastColon - 1);
124 + String port = deviceId.toString().substring(lastColon + 1);
125 + IpAddress ipAddress = IpAddress.valueOf(ip);
126 + long portL = Long.valueOf(port).longValue();
127 + return new OvsdbNodeId(ipAddress, portL);
128 + }
129 +
130 + private OvsdbClientService getOvsdbNode(DriverHandler handler) {
131 + OvsdbController ovsController = handler.get(OvsdbController.class);
132 + DeviceId deviceId = handler.data().deviceId();
133 + OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
134 + return ovsController.getOvsdbClient(nodeId);
135 + }
136 +}
1 +/*
2 + * Copyright 2015 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 + * Implementations of OVSDB protocol configurations.
19 + */
20 +package org.onosproject.driver.ovsdb;
...\ No newline at end of file ...\ No newline at end of file