blob: b04d56654f743816bfab757badcab91164af1b9e [file] [log] [blame]
andreaed976a42015-10-05 14:38:25 -07001/*
Andrea Campanella238d96e2016-01-20 11:52:02 -08002 * Copyright 2016 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
39import static com.google.common.base.Preconditions.checkState;
40import static org.onlab.util.Tools.delay;
41
42/**
43 * Implementation of controller config which allows to get and set controllers.
44 */
45public 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 K2e835162015-11-22 15:15:41 +053060 .equals(ImmutableSet.copyOf(controllers))) {
andreaed976a42015-10-05 14:38:25 -070061 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}