blob: 4f934844aa89dc93b48d1b5327a03e969bbd7897 [file] [log] [blame]
andreaed976a42015-10-05 14:38:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
andreaed976a42015-10-05 14:38:25 -07003 *
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 Campanella238d96e2016-01-20 11:52:02 -080017package org.onosproject.drivers.ovsdb;
andreaed976a42015-10-05 14:38:25 -070018
Satish K2e835162015-11-22 15:15:41 +053019import com.google.common.collect.ImmutableSet;
andreaed976a42015-10-05 14:38:25 -070020import org.onlab.packet.IpAddress;
21import org.onlab.packet.TpPort;
22import org.onosproject.net.AnnotationKeys;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.ControllerConfig;
25import org.onosproject.net.behaviour.ControllerInfo;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.ovsdb.controller.OvsdbBridge;
30import org.onosproject.ovsdb.controller.OvsdbClientService;
Pier Ventref5d72362016-07-17 12:02:14 +020031import org.onosproject.ovsdb.controller.OvsdbConstant;
andreaed976a42015-10-05 14:38:25 -070032import org.onosproject.ovsdb.controller.OvsdbController;
33import org.onosproject.ovsdb.controller.OvsdbNodeId;
34
35import java.util.ArrayList;
36import java.util.List;
37import java.util.Set;
38import java.util.stream.Collectors;
39
Hyunsun Moon1251e192016-06-07 16:57:05 -070040import static com.google.common.base.Preconditions.checkArgument;
andreaed976a42015-10-05 14:38:25 -070041import static com.google.common.base.Preconditions.checkState;
42import static org.onlab.util.Tools.delay;
43
44/**
45 * Implementation of controller config which allows to get and set controllers.
46 */
47public 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 K2e835162015-11-22 15:15:41 +053062 .equals(ImmutableSet.copyOf(controllers))) {
andreaed976a42015-10-05 14:38:25 -070063 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 Hallcbd1b392017-01-18 20:15:44 -080084 if (nodeIds.isEmpty()) {
andreaed976a42015-10-05 14:38:25 -070085 //TODO decide what port?
86 ovsController.connect(IpAddress.valueOf(targetIp),
Pier Ventref5d72362016-07-17 12:02:14 +020087 targetPort == null ? TpPort.tpPort(OvsdbConstant.OVSDBPORT) : targetPort);
andreaed976a42015-10-05 14:38:25 -070088 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 Hallcbd1b392017-01-18 20:15:44 -080095 checkState(!clientServices.isEmpty(), "No clientServices found");
andreaed976a42015-10-05 14:38:25 -070096 //FIXME add connection to management address if null --> done ?
Jon Hallcbd1b392017-01-18 20:15:44 -080097 return !clientServices.isEmpty() ? clientServices.get(0) : null;
andreaed976a42015-10-05 14:38:25 -070098 }
99
100 private static boolean dpidMatches(OvsdbBridge bridge, DeviceId deviceId) {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700101 checkArgument(bridge.datapathId().isPresent());
102
103 String bridgeDpid = "of:" + bridge.datapathId().get();
andreaed976a42015-10-05 14:38:25 -0700104 String ofDpid = deviceId.toString();
105 return bridgeDpid.equals(ofDpid);
106 }
107}