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