blob: dbe7bae42b2ab14a0ad05376a80254e6a215cdaa [file] [log] [blame]
YuanyouZhangb5e05302015-07-21 20:14:35 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
YuanyouZhangb5e05302015-07-21 20:14:35 +08003 *
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 */
16package org.onosproject.ovsdb.providers.device;
17
YuanyouZhangb5e05302015-07-21 20:14:35 +080018import org.onlab.packet.ChassisId;
jiangrui83563612015-11-16 11:59:47 +080019import org.onlab.packet.IpAddress;
Michal Mach958adf62017-06-05 16:29:13 +020020import org.onosproject.mastership.MastershipService;
YuanyouZhangb5e05302015-07-21 20:14:35 +080021import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070025import org.onosproject.net.PortNumber;
YuanyouZhangb5e05302015-07-21 20:14:35 +080026import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.device.DefaultDeviceDescription;
28import org.onosproject.net.device.DeviceDescription;
Michal Mach958adf62017-06-05 16:29:13 +020029import org.onosproject.net.device.DeviceDescriptionDiscovery;
30import org.onosproject.net.device.DeviceEvent;
31import org.onosproject.net.device.DeviceListener;
YuanyouZhangb5e05302015-07-21 20:14:35 +080032import org.onosproject.net.device.DeviceProvider;
33import org.onosproject.net.device.DeviceProviderRegistry;
34import org.onosproject.net.device.DeviceProviderService;
Michal Mach958adf62017-06-05 16:29:13 +020035import org.onosproject.net.device.DeviceService;
YuanyouZhangb5e05302015-07-21 20:14:35 +080036import org.onosproject.net.provider.AbstractProvider;
37import org.onosproject.net.provider.ProviderId;
jiangrui83563612015-11-16 11:59:47 +080038import org.onosproject.ovsdb.controller.OvsdbClientService;
YuanyouZhangb5e05302015-07-21 20:14:35 +080039import org.onosproject.ovsdb.controller.OvsdbController;
40import org.onosproject.ovsdb.controller.OvsdbNodeId;
41import org.onosproject.ovsdb.controller.OvsdbNodeListener;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import org.osgi.service.component.annotations.Activate;
43import org.osgi.service.component.annotations.Component;
44import org.osgi.service.component.annotations.Deactivate;
45import org.osgi.service.component.annotations.Reference;
46import org.osgi.service.component.annotations.ReferenceCardinality;
YuanyouZhangb5e05302015-07-21 20:14:35 +080047import org.slf4j.Logger;
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049import java.net.URI;
50import java.util.concurrent.ExecutorService;
51import java.util.concurrent.Executors;
52import java.util.concurrent.TimeUnit;
53
54import static com.google.common.base.Preconditions.checkNotNull;
55import static org.onlab.util.Tools.groupedThreads;
Jian Li7e8040d2022-01-12 03:35:38 +090056import static org.onosproject.net.MastershipRole.NONE;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057import static org.slf4j.LoggerFactory.getLogger;
58
YuanyouZhangb5e05302015-07-21 20:14:35 +080059/**
60 * Provider which uses an ovsdb controller to detect device.
61 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062@Component(immediate = true, service = DeviceProvider.class)
YuanyouZhangb5e05302015-07-21 20:14:35 +080063public class OvsdbDeviceProvider extends AbstractProvider
64 implements DeviceProvider {
65 private final Logger log = getLogger(getClass());
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
YuanyouZhangb5e05302015-07-21 20:14:35 +080068 protected DeviceProviderRegistry providerRegistry;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
YuanyouZhangb5e05302015-07-21 20:14:35 +080071 protected OvsdbController controller;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Michal Mach958adf62017-06-05 16:29:13 +020074 protected DeviceService deviceService;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Michal Mach958adf62017-06-05 16:29:13 +020077 protected MastershipService mastershipService;
78
YuanyouZhangb5e05302015-07-21 20:14:35 +080079 private DeviceProviderService providerService;
80 private OvsdbNodeListener innerNodeListener = new InnerOvsdbNodeListener();
Michal Mach958adf62017-06-05 16:29:13 +020081 private InternalDeviceListener deviceListener = new InternalDeviceListener();
YuanyouZhangb5e05302015-07-21 20:14:35 +080082 protected static final String ISNOTNULL = "OvsdbNodeId is not null";
Michal Mach958adf62017-06-05 16:29:13 +020083 protected static final String SCHEME_NAME = "ovsdb";
YuanyouZhangb5e05302015-07-21 20:14:35 +080084 private static final String UNKNOWN = "unknown";
85
Michal Mach958adf62017-06-05 16:29:13 +020086 protected ExecutorService executor =
87 Executors.newFixedThreadPool(5, groupedThreads("onos/ovsdbdeviceprovider",
88 "device-installer-%d", log));
89
YuanyouZhangb5e05302015-07-21 20:14:35 +080090 @Activate
91 public void activate() {
92 providerService = providerRegistry.register(this);
93 controller.addNodeListener(innerNodeListener);
Michal Mach958adf62017-06-05 16:29:13 +020094 deviceService.addListener(deviceListener);
YuanyouZhangb5e05302015-07-21 20:14:35 +080095 log.info("Started");
96 }
97
98 @Deactivate
99 public void deactivate() {
Frank Wang14629822017-05-26 09:22:59 +0800100 controller.removeNodeListener(innerNodeListener);
YuanyouZhangb5e05302015-07-21 20:14:35 +0800101 providerRegistry.unregister(this);
Michal Mach958adf62017-06-05 16:29:13 +0200102 deviceService.removeListener(deviceListener);
103 waitForTasksToEnd();
YuanyouZhangb5e05302015-07-21 20:14:35 +0800104 providerService = null;
105 log.info("Stopped");
106 }
107
108 public OvsdbDeviceProvider() {
109 super(new ProviderId("ovsdb", "org.onosproject.ovsdb.provider.device"));
110 }
111
112 @Override
113 public void triggerProbe(DeviceId deviceId) {
YuanyouZhangb5e05302015-07-21 20:14:35 +0800114 log.info("Triggering probe on device {}", deviceId);
jiangrui5f4d2e62015-11-16 12:52:35 +0800115 if (!isReachable(deviceId)) {
116 log.error("Failed to probe device {}", deviceId);
117 providerService.deviceDisconnected(deviceId);
jiangrui5f4d2e62015-11-16 12:52:35 +0800118 } else {
119 log.trace("Confirmed device {} connection", deviceId);
120 }
YuanyouZhangb5e05302015-07-21 20:14:35 +0800121 }
122
123 @Override
124 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
Jian Li7e8040d2022-01-12 03:35:38 +0900125 // for OVSDB there is no Mastership concept, simulating here a fake
126 // Mastership handshake to be compliant with the ONOS core
127 if (newRole != null && newRole != NONE) {
128 final MastershipRole role = mastershipService.getLocalRole(deviceId);
129 providerService.receivedRoleReply(deviceId, role, role);
130 }
YuanyouZhangb5e05302015-07-21 20:14:35 +0800131 }
132
133 @Override
134 public boolean isReachable(DeviceId deviceId) {
jiangrui83563612015-11-16 11:59:47 +0800135 OvsdbClientService ovsdbClient = controller.getOvsdbClient(changeDeviceIdToNodeId(deviceId));
136 return !(ovsdbClient == null || !ovsdbClient.isConnected());
YuanyouZhangb5e05302015-07-21 20:14:35 +0800137 }
138
139 private class InnerOvsdbNodeListener implements OvsdbNodeListener {
140
141 @Override
142 public void nodeAdded(OvsdbNodeId nodeId) {
143 checkNotNull(nodeId, ISNOTNULL);
144 DeviceId deviceId = DeviceId.deviceId(nodeId.toString());
145 URI uri = URI.create(nodeId.toString());
146 ChassisId cid = new ChassisId();
147 String ipAddress = nodeId.getIpAddress();
148 SparseAnnotations annotations = DefaultAnnotations.builder()
149 .set("ipaddress", ipAddress).build();
150 DeviceDescription deviceDescription = new DefaultDeviceDescription(
151 uri,
152 Device.Type.CONTROLLER,
153 UNKNOWN, UNKNOWN,
154 UNKNOWN, UNKNOWN,
155 cid,
156 annotations);
157 providerService.deviceConnected(deviceId, deviceDescription);
YuanyouZhangb5e05302015-07-21 20:14:35 +0800158 }
159
160 @Override
161 public void nodeRemoved(OvsdbNodeId nodeId) {
162 checkNotNull(nodeId, ISNOTNULL);
163 DeviceId deviceId = DeviceId.deviceId(nodeId.toString());
164 providerService.deviceDisconnected(deviceId);
165
166 }
167 }
jiangrui83563612015-11-16 11:59:47 +0800168
169 private OvsdbNodeId changeDeviceIdToNodeId(DeviceId deviceId) {
170 String[] strings = deviceId.toString().split(":");
171 if (strings.length < 1) {
172 return null;
173 }
174 return new OvsdbNodeId(IpAddress.valueOf(strings[1]), 0);
175 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700176
177 @Override
178 public void changePortState(DeviceId deviceId, PortNumber portNumber,
179 boolean enable) {
180 // TODO if required
181 }
Michal Mach958adf62017-06-05 16:29:13 +0200182
183 private void discoverPorts(DeviceId deviceId) {
184 Device device = deviceService.getDevice(deviceId);
185 if (device.is(DeviceDescriptionDiscovery.class)) {
186 DeviceDescriptionDiscovery deviceDescriptionDiscovery = device.as(DeviceDescriptionDiscovery.class);
187 providerService.updatePorts(deviceId, deviceDescriptionDiscovery.discoverPortDetails());
188 } else {
189 log.warn("Device " + deviceId + " does not support behaviour DeviceDescriptionDiscovery");
190 }
191 }
192
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700193 @Override
194 public void triggerDisconnect(DeviceId deviceId) {
195 log.debug("Forcing disconnect for device {}", deviceId);
196 OvsdbNodeId ovsdbNodeId = changeDeviceIdToNodeId(deviceId);
197 OvsdbClientService client = controller.getOvsdbClient(ovsdbNodeId);
198 if (client != null) {
199 client.disconnect();
200 }
201 }
202
Michal Mach958adf62017-06-05 16:29:13 +0200203 private class InternalDeviceListener implements DeviceListener {
204 @Override
205 public void event(DeviceEvent event) {
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700206 executor.execute(() -> discoverPorts(event.subject().id()));
Michal Mach958adf62017-06-05 16:29:13 +0200207 }
208
209 @Override
210 public boolean isRelevant(DeviceEvent event) {
211 DeviceId deviceId = event.subject().id();
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700212 return event.type() == DeviceEvent.Type.DEVICE_ADDED &&
213 isRelevant(deviceId) && mastershipService.isLocalMaster(deviceId);
Michal Mach958adf62017-06-05 16:29:13 +0200214 }
215
216 private boolean isRelevant(DeviceId deviceId) {
217 return deviceId.uri().getScheme().equals(SCHEME_NAME);
218 }
219 }
220
221 private void waitForTasksToEnd() {
222 executor.shutdown();
223 try {
224 executor.awaitTermination(5, TimeUnit.SECONDS);
225 } catch (InterruptedException e) {
226 log.error("Timeout while waiting for child threads to finish because: " + e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800227 Thread.currentThread().interrupt();
Michal Mach958adf62017-06-05 16:29:13 +0200228 }
229 }
YuanyouZhangb5e05302015-07-21 20:14:35 +0800230}