andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [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 | |
| 17 | package org.onosproject.driver.ovsdb; |
| 18 | |
Satish K | 2e83516 | 2015-11-22 15:15:41 +0530 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.behaviour.ControllerConfig; |
| 25 | import org.onosproject.net.behaviour.ControllerInfo; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.onosproject.net.driver.DriverHandler; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbBridge; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 31 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 32 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkState; |
| 40 | import static org.onlab.util.Tools.delay; |
| 41 | |
| 42 | /** |
| 43 | * Implementation of controller config which allows to get and set controllers. |
| 44 | */ |
| 45 | public class OvsdbControllerConfig extends AbstractHandlerBehaviour implements ControllerConfig { |
| 46 | @Override |
| 47 | public List<ControllerInfo> getControllers() { |
| 48 | DriverHandler handler = handler(); |
| 49 | OvsdbClientService clientService = getOvsdbClientService(handler); |
| 50 | Set<ControllerInfo> controllers = clientService.getControllers( |
| 51 | handler().data().deviceId()); |
| 52 | return new ArrayList<>(controllers); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void setControllers(List<ControllerInfo> controllers) { |
| 57 | DriverHandler handler = handler(); |
| 58 | OvsdbClientService clientService = getOvsdbClientService(handler); |
| 59 | if (!clientService.getControllers(handler().data().deviceId()) |
Satish K | 2e83516 | 2015-11-22 15:15:41 +0530 | [diff] [blame] | 60 | .equals(ImmutableSet.copyOf(controllers))) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 61 | clientService.setControllersWithDeviceId(handler(). |
| 62 | data().deviceId(), controllers); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Used for getting OvsdbClientService. |
| 67 | private OvsdbClientService getOvsdbClientService(DriverHandler handler) { |
| 68 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 69 | DeviceService deviceService = handler.get(DeviceService.class); |
| 70 | DeviceId ofDeviceId = handler.data().deviceId(); |
| 71 | String[] mgmtAddress = deviceService.getDevice(ofDeviceId) |
| 72 | .annotations().value(AnnotationKeys.MANAGEMENT_ADDRESS).split(":"); |
| 73 | String targetIp = mgmtAddress[0]; |
| 74 | TpPort targetPort = null; |
| 75 | if (mgmtAddress.length > 1) { |
| 76 | targetPort = TpPort.tpPort(Integer.parseInt(mgmtAddress[1])); |
| 77 | } |
| 78 | |
| 79 | List<OvsdbNodeId> nodeIds = ovsController.getNodeIds().stream() |
| 80 | .filter(nodeId -> nodeId.getIpAddress().equals(targetIp)) |
| 81 | .collect(Collectors.toList()); |
| 82 | if (nodeIds.size() == 0) { |
| 83 | //TODO decide what port? |
| 84 | ovsController.connect(IpAddress.valueOf(targetIp), |
| 85 | targetPort == null ? TpPort.tpPort(6640) : targetPort); |
| 86 | delay(1000); //FIXME... connect is async |
| 87 | } |
| 88 | List<OvsdbClientService> clientServices = ovsController.getNodeIds().stream() |
| 89 | .filter(nodeId -> nodeId.getIpAddress().equals(targetIp)) |
| 90 | .map(ovsController::getOvsdbClient) |
| 91 | .filter(cs -> cs.getBridges().stream().anyMatch(b -> dpidMatches(b, ofDeviceId))) |
| 92 | .collect(Collectors.toList()); |
| 93 | checkState(clientServices.size() > 0, "No clientServices found"); |
| 94 | //FIXME add connection to management address if null --> done ? |
| 95 | return clientServices.size() > 0 ? clientServices.get(0) : null; |
| 96 | } |
| 97 | |
| 98 | private static boolean dpidMatches(OvsdbBridge bridge, DeviceId deviceId) { |
| 99 | String bridgeDpid = "of:" + bridge.datapathId().value(); |
| 100 | String ofDpid = deviceId.toString(); |
| 101 | return bridgeDpid.equals(ofDpid); |
| 102 | } |
| 103 | } |