Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 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 | package org.onosproject.protocol.restconf.ctl; |
| 17 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.glassfish.jersey.client.ChunkedInput; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.protocol.http.ctl.HttpSBControllerImpl; |
| 26 | import org.onosproject.protocol.rest.RestSBDevice; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 27 | import org.onosproject.protocol.restconf.RestConfSBController; |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 28 | import org.onosproject.protocol.restconf.RestconfNotificationEventListener; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 32 | import javax.ws.rs.client.WebTarget; |
| 33 | import javax.ws.rs.core.GenericType; |
| 34 | import javax.ws.rs.core.Response; |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 35 | import java.util.HashSet; |
| 36 | import java.util.Map; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
| 39 | import java.util.concurrent.ExecutorService; |
| 40 | import java.util.concurrent.Executors; |
| 41 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 42 | /** |
| 43 | * The implementation of RestConfSBController. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class RestConfSBControllerImpl extends HttpSBControllerImpl |
| 48 | implements RestConfSBController { |
| 49 | |
| 50 | private static final Logger log = LoggerFactory |
| 51 | .getLogger(RestConfSBControllerImpl.class); |
| 52 | |
| 53 | // TODO: for the Ibis release when both RESTCONF server and RESTCONF client |
| 54 | // fully support root resource discovery, ROOT_RESOURCE constant will be |
| 55 | // removed and rather the value would get discovered dynamically. |
| 56 | private static final String ROOT_RESOURCE = "/onos/restconf"; |
| 57 | |
| 58 | private static final String RESOURCE_PATH_PREFIX = "/data/"; |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame] | 59 | private static final String NOTIFICATION_PATH_PREFIX = "/streams/"; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 60 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 61 | private Map<DeviceId, Set<RestconfNotificationEventListener>> |
| 62 | restconfNotificationListenerMap = new ConcurrentHashMap<>(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 63 | private Map<DeviceId, GetChunksRunnable> runnableTable = new ConcurrentHashMap<>(); |
| 64 | |
| 65 | ExecutorService executor = Executors.newCachedThreadPool(); |
| 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | log.info("RESTCONF SBI Started"); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | log.info("RESTCONF SBI Stopped"); |
| 75 | executor.shutdown(); |
| 76 | this.getClientMap().clear(); |
| 77 | this.getDeviceMap().clear(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Map<DeviceId, RestSBDevice> getDevices() { |
| 82 | log.trace("RESTCONF SBI::getDevices"); |
| 83 | return super.getDevices(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public RestSBDevice getDevice(DeviceId deviceInfo) { |
| 88 | log.trace("RESTCONF SBI::getDevice with deviceId"); |
| 89 | return super.getDevice(deviceInfo); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public RestSBDevice getDevice(IpAddress ip, int port) { |
| 94 | log.trace("RESTCONF SBI::getDevice with ip and port"); |
| 95 | return super.getDevice(ip, port); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void addDevice(RestSBDevice device) { |
| 100 | log.trace("RESTCONF SBI::addDevice"); |
| 101 | super.addDevice(device); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void removeDevice(DeviceId deviceId) { |
| 106 | log.trace("RESTCONF SBI::removeDevice"); |
| 107 | super.removeDevice(deviceId); |
| 108 | } |
| 109 | |
| 110 | @Override |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 111 | public void enableNotifications(DeviceId device, String request, |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 112 | String mediaType, |
| 113 | RestconfNotificationEventListener listener) { |
| 114 | |
| 115 | if (isNotificationEnabled(device)) { |
| 116 | log.warn("enableNotifications: already enabled on device: {}", device); |
| 117 | return; |
| 118 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 119 | |
| 120 | request = discoverRootResource(device) + NOTIFICATION_PATH_PREFIX |
| 121 | + request; |
| 122 | |
| 123 | addNotificationListener(device, listener); |
| 124 | |
| 125 | GetChunksRunnable runnable = new GetChunksRunnable(request, mediaType, |
| 126 | device); |
| 127 | runnableTable.put(device, runnable); |
| 128 | executor.execute(runnable); |
| 129 | } |
| 130 | |
Henry Yu | 05c2c76 | 2017-01-30 12:11:08 -0500 | [diff] [blame] | 131 | public void stopNotifications(DeviceId device) { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 132 | runnableTable.get(device).terminate(); |
| 133 | runnableTable.remove(device); |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 134 | restconfNotificationListenerMap.remove(device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 135 | log.debug("Stop sending notifications for device URI: " + device.uri().toString()); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | public class GetChunksRunnable implements Runnable { |
| 140 | private String request; |
| 141 | private String mediaType; |
| 142 | private DeviceId device; |
| 143 | |
| 144 | private volatile boolean running = true; |
| 145 | |
| 146 | public void terminate() { |
| 147 | running = false; |
| 148 | } |
| 149 | |
| 150 | /** |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 151 | * @param request request |
Ray Milkey | 0bb1e10 | 2016-11-10 14:51:27 -0800 | [diff] [blame] | 152 | * @param mediaType media type |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 153 | * @param device device identifier |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 154 | */ |
| 155 | public GetChunksRunnable(String request, String mediaType, |
| 156 | DeviceId device) { |
| 157 | this.request = request; |
| 158 | this.mediaType = mediaType; |
| 159 | this.device = device; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void run() { |
| 164 | WebTarget wt = getWebTarget(device, request); |
| 165 | Response clientResp = wt.request(mediaType).get(); |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 166 | Set<RestconfNotificationEventListener> listeners = |
| 167 | restconfNotificationListenerMap.get(device); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 168 | final ChunkedInput<String> chunkedInput = (ChunkedInput<String>) clientResp |
| 169 | .readEntity(new GenericType<ChunkedInput<String>>() { |
| 170 | }); |
| 171 | |
| 172 | String chunk; |
| 173 | // Note that the read() is a blocking operation and the invoking |
| 174 | // thread is blocked until a new chunk comes. Jersey implementation |
| 175 | // of this IO operation is in a way that it does not respond to |
| 176 | // interrupts. |
| 177 | while (running) { |
| 178 | chunk = chunkedInput.read(); |
| 179 | if (chunk != null) { |
| 180 | if (running) { |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 181 | for (RestconfNotificationEventListener listener : listeners) { |
| 182 | listener.handleNotificationEvent(device, chunk); |
| 183 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 184 | } else { |
| 185 | log.trace("the requesting client is no more interested " |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 186 | + "to receive such notifications."); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 187 | } |
| 188 | } else { |
| 189 | log.trace("The received notification chunk is null. do not continue any more."); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | log.trace("out of while loop -- end of run"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public String discoverRootResource(DeviceId device) { |
| 198 | // FIXME: send a GET command to the device to discover the root resource. |
| 199 | // The plan to fix this is for the Ibis release when the RESTCONF server and |
| 200 | // the RESTCONF client both support root resource discovery. |
| 201 | return ROOT_RESOURCE; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void addNotificationListener(DeviceId deviceId, |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 206 | RestconfNotificationEventListener listener) { |
| 207 | Set<RestconfNotificationEventListener> listeners = |
| 208 | restconfNotificationListenerMap.get(deviceId); |
| 209 | if (listeners == null) { |
| 210 | listeners = new HashSet<>(); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 211 | } |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 212 | |
| 213 | listeners.add(listener); |
| 214 | |
| 215 | this.restconfNotificationListenerMap.put(deviceId, listeners); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 219 | public void removeNotificationListener(DeviceId deviceId, |
| 220 | RestconfNotificationEventListener listener) { |
| 221 | Set<RestconfNotificationEventListener> listeners = |
| 222 | restconfNotificationListenerMap.get(deviceId); |
| 223 | if (listeners != null) { |
| 224 | listeners.remove(listener); |
| 225 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 228 | public boolean isNotificationEnabled(DeviceId deviceId) { |
| 229 | return runnableTable.containsKey(deviceId); |
| 230 | } |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 231 | } |