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