CNlucius | ae5c396 | 2015-08-21 14:33:26 +0800 | [diff] [blame] | 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.Collections;
|
| 20 | import java.util.HashSet;
|
| 21 | import java.util.Set;
|
| 22 |
|
| 23 | import org.onlab.packet.IpAddress;
|
| 24 | import org.onosproject.net.DeviceId;
|
| 25 | import org.onosproject.net.PortNumber;
|
| 26 | import org.onosproject.net.behaviour.BridgeConfig;
|
| 27 | import org.onosproject.net.behaviour.BridgeDescription;
|
| 28 | import org.onosproject.net.behaviour.BridgeName;
|
| 29 | import org.onosproject.net.behaviour.DefaultBridgeDescription;
|
| 30 | import org.onosproject.net.device.DefaultPortDescription;
|
| 31 | import org.onosproject.net.device.PortDescription;
|
| 32 | import org.onosproject.net.driver.AbstractHandlerBehaviour;
|
| 33 | import org.onosproject.net.driver.DriverHandler;
|
| 34 | import org.onosproject.ovsdb.controller.OvsdbBridge;
|
| 35 | import org.onosproject.ovsdb.controller.OvsdbClientService;
|
| 36 | import org.onosproject.ovsdb.controller.OvsdbController;
|
| 37 | import org.onosproject.ovsdb.controller.OvsdbNodeId;
|
| 38 | import org.onosproject.ovsdb.controller.OvsdbPort;
|
| 39 |
|
| 40 | import com.google.common.collect.Sets;
|
| 41 |
|
| 42 | /**
|
| 43 | * The implementation of BridageConfig.
|
| 44 | */
|
| 45 | public class OvsdbBridgeConfig extends AbstractHandlerBehaviour
|
| 46 | implements BridgeConfig {
|
| 47 |
|
| 48 | @Override
|
| 49 | public void addBridge(BridgeName bridgeName) {
|
| 50 | DriverHandler handler = handler();
|
| 51 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 52 | clientService.createBridge(bridgeName.name());
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void deleteBridge(BridgeName bridgeName) {
|
| 57 | DriverHandler handler = handler();
|
| 58 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 59 | clientService.dropBridge(bridgeName.name());
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public Collection<BridgeDescription> getBridges() {
|
| 64 | DriverHandler handler = handler();
|
| 65 | DeviceId deviceId = handler.data().deviceId();
|
| 66 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 67 | Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
|
| 68 | Collection<BridgeDescription> bridges = Sets.newHashSet();
|
| 69 | ovsdbSet.forEach(o -> {
|
| 70 | BridgeName bridgeName = BridgeName
|
| 71 | .bridgeName(o.bridgeName().value());
|
| 72 | DeviceId ownDeviceId = DeviceId.deviceId("of:" + o.datapathId().value());
|
| 73 | BridgeDescription description = new DefaultBridgeDescription(bridgeName,
|
| 74 | deviceId,
|
| 75 | ownDeviceId);
|
| 76 | bridges.add(description);
|
| 77 | });
|
| 78 | return bridges == null ? Collections.emptySet() : bridges;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void addPort(PortDescription port) {
|
| 83 | DriverHandler handler = handler();
|
| 84 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 85 | Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
|
| 86 | if (ovsdbSet != null && ovsdbSet.size() > 0) {
|
| 87 | OvsdbBridge bridge = ovsdbSet.iterator().next();
|
| 88 | clientService.createPort(bridge.bridgeName().toString(), port
|
| 89 | .portNumber().toString());
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void deletePort(PortDescription port) {
|
| 95 | DriverHandler handler = handler();
|
| 96 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 97 | Set<OvsdbBridge> ovsdbSet = clientService.getBridges();
|
| 98 | if (ovsdbSet != null && ovsdbSet.size() > 0) {
|
| 99 | OvsdbBridge bridge = ovsdbSet.iterator().next();
|
| 100 | clientService.dropPort(bridge.bridgeName().toString(), port
|
| 101 | .portNumber().toString());
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public Collection<PortDescription> getPorts() {
|
| 107 | DriverHandler handler = handler();
|
| 108 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 109 | Set<OvsdbPort> ovsdbSet = clientService.getPorts();
|
| 110 | Collection<PortDescription> ports = Sets.newHashSet();
|
| 111 | ovsdbSet.forEach(o -> {
|
| 112 | PortNumber port = PortNumber.portNumber(o.portNumber().value());
|
| 113 | PortDescription description = new DefaultPortDescription(port, true);
|
| 114 | ports.add(description);
|
| 115 | });
|
| 116 | return ports;
|
| 117 | }
|
| 118 |
|
| 119 | // OvsdbNodeId(IP:port) is used in the adaptor while DeviceId(ovsdb:IP:port)
|
| 120 | // is used in the core. So DeviceId need be changed to OvsdbNodeId.
|
| 121 | private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
|
| 122 | int lastColon = deviceId.toString().lastIndexOf(":");
|
| 123 | int fistColon = deviceId.toString().indexOf(":");
|
| 124 | String ip = deviceId.toString().substring(fistColon + 1, lastColon);
|
| 125 | String port = deviceId.toString().substring(lastColon + 1);
|
| 126 | IpAddress ipAddress = IpAddress.valueOf(ip);
|
Sho SHIMIZU | 4346d60 | 2015-08-24 18:11:56 -0700 | [diff] [blame^] | 127 | long portL = Long.parseLong(port);
|
CNlucius | ae5c396 | 2015-08-21 14:33:26 +0800 | [diff] [blame] | 128 | return new OvsdbNodeId(ipAddress, portL);
|
| 129 | }
|
| 130 |
|
| 131 | // Used for getting OvsdbClientService.
|
| 132 | private OvsdbClientService getOvsdbClientService(DriverHandler handler) {
|
| 133 | OvsdbController ovsController = handler.get(OvsdbController.class);
|
| 134 | DeviceId deviceId = handler.data().deviceId();
|
| 135 | OvsdbNodeId nodeId = changeDeviceIdToNodeId(deviceId);
|
| 136 | return ovsController.getOvsdbClient(nodeId);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public Set<PortNumber> getPortNumbers() {
|
| 141 | Set<PortNumber> ports = new HashSet<>();
|
| 142 | DriverHandler handler = handler();
|
| 143 | OvsdbClientService clientService = getOvsdbClientService(handler);
|
| 144 | Set<OvsdbPort> ovsdbSet = clientService.getPorts();
|
| 145 | ovsdbSet.forEach(o -> {
|
| 146 | PortNumber port = PortNumber.portNumber(o.portNumber().value(),
|
| 147 | o.portName().value());
|
| 148 | ports.add(port);
|
| 149 | });
|
| 150 | return ports;
|
| 151 | }
|
| 152 | }
|