andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 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 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 17 | package org.onosproject.drivers.ovsdb; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 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; |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 31 | import org.onosproject.ovsdb.controller.OvsdbConstant; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 32 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 33 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkArgument; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 41 | import static com.google.common.base.Preconditions.checkState; |
| 42 | import static org.onlab.util.Tools.delay; |
| 43 | |
| 44 | /** |
| 45 | * Implementation of controller config which allows to get and set controllers. |
| 46 | */ |
| 47 | public class OvsdbControllerConfig extends AbstractHandlerBehaviour implements ControllerConfig { |
| 48 | @Override |
| 49 | public List<ControllerInfo> getControllers() { |
| 50 | DriverHandler handler = handler(); |
| 51 | OvsdbClientService clientService = getOvsdbClientService(handler); |
| 52 | Set<ControllerInfo> controllers = clientService.getControllers( |
| 53 | handler().data().deviceId()); |
| 54 | return new ArrayList<>(controllers); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void setControllers(List<ControllerInfo> controllers) { |
| 59 | DriverHandler handler = handler(); |
| 60 | OvsdbClientService clientService = getOvsdbClientService(handler); |
| 61 | if (!clientService.getControllers(handler().data().deviceId()) |
Satish K | 2e83516 | 2015-11-22 15:15:41 +0530 | [diff] [blame] | 62 | .equals(ImmutableSet.copyOf(controllers))) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 63 | clientService.setControllersWithDeviceId(handler(). |
| 64 | data().deviceId(), controllers); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Used for getting OvsdbClientService. |
| 69 | private OvsdbClientService getOvsdbClientService(DriverHandler handler) { |
| 70 | OvsdbController ovsController = handler.get(OvsdbController.class); |
| 71 | DeviceService deviceService = handler.get(DeviceService.class); |
| 72 | DeviceId ofDeviceId = handler.data().deviceId(); |
| 73 | String[] mgmtAddress = deviceService.getDevice(ofDeviceId) |
| 74 | .annotations().value(AnnotationKeys.MANAGEMENT_ADDRESS).split(":"); |
| 75 | String targetIp = mgmtAddress[0]; |
| 76 | TpPort targetPort = null; |
| 77 | if (mgmtAddress.length > 1) { |
| 78 | targetPort = TpPort.tpPort(Integer.parseInt(mgmtAddress[1])); |
| 79 | } |
| 80 | |
| 81 | List<OvsdbNodeId> nodeIds = ovsController.getNodeIds().stream() |
| 82 | .filter(nodeId -> nodeId.getIpAddress().equals(targetIp)) |
| 83 | .collect(Collectors.toList()); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 84 | if (nodeIds.isEmpty()) { |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 85 | //TODO decide what port? |
| 86 | ovsController.connect(IpAddress.valueOf(targetIp), |
Pier Ventre | f5d7236 | 2016-07-17 12:02:14 +0200 | [diff] [blame] | 87 | targetPort == null ? TpPort.tpPort(OvsdbConstant.OVSDBPORT) : targetPort); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 88 | delay(1000); //FIXME... connect is async |
| 89 | } |
| 90 | List<OvsdbClientService> clientServices = ovsController.getNodeIds().stream() |
| 91 | .filter(nodeId -> nodeId.getIpAddress().equals(targetIp)) |
| 92 | .map(ovsController::getOvsdbClient) |
| 93 | .filter(cs -> cs.getBridges().stream().anyMatch(b -> dpidMatches(b, ofDeviceId))) |
| 94 | .collect(Collectors.toList()); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 95 | checkState(!clientServices.isEmpty(), "No clientServices found"); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 96 | //FIXME add connection to management address if null --> done ? |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 97 | return !clientServices.isEmpty() ? clientServices.get(0) : null; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private static boolean dpidMatches(OvsdbBridge bridge, DeviceId deviceId) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 101 | checkArgument(bridge.datapathId().isPresent()); |
| 102 | |
| 103 | String bridgeDpid = "of:" + bridge.datapathId().get(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 104 | String ofDpid = deviceId.toString(); |
| 105 | return bridgeDpid.equals(ofDpid); |
| 106 | } |
| 107 | } |