Andrea Campanella | e370878 | 2017-10-16 16:00:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.net.meter.impl; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.onosproject.mastership.MastershipService; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DeviceEvent; |
| 24 | import org.onosproject.net.device.DeviceListener; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.meter.Meter; |
| 27 | import org.onosproject.net.meter.MeterOperation; |
| 28 | import org.onosproject.net.meter.MeterOperations; |
| 29 | import org.onosproject.net.meter.MeterProgrammable; |
| 30 | import org.onosproject.net.meter.MeterProvider; |
| 31 | |
| 32 | import org.onosproject.net.meter.MeterProviderService; |
| 33 | import org.onosproject.net.provider.AbstractProvider; |
| 34 | import org.onosproject.net.provider.ProviderId; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Set; |
| 40 | import java.util.concurrent.ExecutionException; |
| 41 | import java.util.concurrent.ScheduledExecutorService; |
| 42 | import java.util.concurrent.ScheduledFuture; |
| 43 | import java.util.concurrent.TimeUnit; |
| 44 | import java.util.concurrent.TimeoutException; |
| 45 | |
| 46 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 47 | import static org.onlab.util.Tools.groupedThreads; |
| 48 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 49 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 50 | |
| 51 | /** |
| 52 | * Driver-based Meter provider. |
| 53 | */ |
| 54 | public class MeterDriverProvider extends AbstractProvider implements MeterProvider { |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
| 58 | // To be extracted for reuse as we deal with other. |
| 59 | private static final String SCHEME = "default"; |
| 60 | private static final String PROVIDER_NAME = "org.onosproject.provider.meter"; |
| 61 | |
| 62 | // potentially positive device event |
| 63 | private static final Set<DeviceEvent.Type> POSITIVE_DEVICE_EVENT = |
| 64 | Sets.immutableEnumSet(DEVICE_ADDED, |
| 65 | DEVICE_AVAILABILITY_CHANGED); |
| 66 | |
| 67 | protected DeviceService deviceService; |
| 68 | protected MastershipService mastershipService; |
| 69 | MeterProviderService meterProviderService; |
| 70 | int pollFrequency; |
| 71 | |
| 72 | private InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 73 | private ScheduledExecutorService executor |
| 74 | = newSingleThreadScheduledExecutor(groupedThreads("MeterDriverProvider", "%d", log)); |
| 75 | private ScheduledFuture<?> poller = null; |
| 76 | |
| 77 | public MeterDriverProvider() { |
| 78 | super(new ProviderId(SCHEME, PROVIDER_NAME)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Initializes the provider with the necessary device service, meter provider service, |
| 83 | * mastership service and poll frequency. |
| 84 | * |
| 85 | * @param deviceService device service |
| 86 | * @param meterProviderService meter provider service |
| 87 | * @param mastershipService mastership service |
| 88 | * @param pollFrequency meter entry poll frequency |
| 89 | */ |
| 90 | void init(DeviceService deviceService, MeterProviderService meterProviderService, |
| 91 | MastershipService mastershipService, int pollFrequency) { |
| 92 | this.deviceService = deviceService; |
| 93 | this.meterProviderService = meterProviderService; |
| 94 | this.mastershipService = mastershipService; |
| 95 | this.pollFrequency = pollFrequency; |
| 96 | |
| 97 | deviceService.addListener(deviceListener); |
| 98 | |
| 99 | if (poller != null && !poller.isCancelled()) { |
| 100 | poller.cancel(false); |
| 101 | } |
| 102 | |
| 103 | poller = executor.scheduleAtFixedRate(this::pollMeters, pollFrequency, |
| 104 | pollFrequency, TimeUnit.SECONDS); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | void terminate() { |
| 109 | deviceService.removeListener(deviceListener); |
| 110 | deviceService = null; |
| 111 | meterProviderService = null; |
| 112 | mastershipService = null; |
| 113 | poller.cancel(true); |
| 114 | executor.shutdown(); |
| 115 | } |
| 116 | |
| 117 | private void pollMeters() { |
| 118 | deviceService.getAvailableDevices().forEach(device -> { |
| 119 | if (mastershipService.isLocalMaster(device.id()) && |
| 120 | device.is(MeterProgrammable.class)) { |
| 121 | pollDeviceMeters(device.id()); |
| 122 | } |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void performMeterOperation(DeviceId deviceId, MeterOperations meterOps) { |
| 128 | meterOps.operations().forEach(meterOperation -> performMeterOperation(deviceId, meterOperation)); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void performMeterOperation(DeviceId deviceId, MeterOperation meterOp) { |
| 133 | MeterProgrammable programmable = getMeterProgrammable(deviceId); |
| 134 | if (programmable != null) { |
| 135 | programmable.performMeterOperation(meterOp); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private void pollDeviceMeters(DeviceId deviceId) { |
| 140 | Collection<Meter> meters = null; |
| 141 | try { |
| 142 | meters = getMeterProgrammable(deviceId).getMeters().get(pollFrequency, TimeUnit.SECONDS); |
| 143 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 144 | log.warn("Unable to get the Meters from {}, error: {}", deviceId, e.getMessage()); |
| 145 | log.debug("Exception: ", e); |
| 146 | } |
| 147 | meterProviderService.pushMeterMetrics(deviceId, meters); |
| 148 | } |
| 149 | |
| 150 | private MeterProgrammable getMeterProgrammable(DeviceId deviceId) { |
| 151 | Device device = deviceService.getDevice(deviceId); |
| 152 | if (device.is(MeterProgrammable.class)) { |
| 153 | return device.as(MeterProgrammable.class); |
| 154 | } else { |
| 155 | log.debug("Device {} is not meter programmable", deviceId); |
| 156 | return null; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private class InternalDeviceListener implements DeviceListener { |
| 161 | |
| 162 | @Override |
| 163 | public void event(DeviceEvent event) { |
| 164 | executor.execute(() -> handleEvent(event)); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean isRelevant(DeviceEvent event) { |
| 169 | Device device = event.subject(); |
| 170 | return POSITIVE_DEVICE_EVENT.contains(event.type()) && |
| 171 | device.is(MeterProgrammable.class); |
| 172 | } |
| 173 | |
| 174 | private void handleEvent(DeviceEvent event) { |
| 175 | Device device = event.subject(); |
| 176 | boolean isRelevant = mastershipService.isLocalMaster(device.id()) && |
| 177 | deviceService.isAvailable(device.id()); |
| 178 | |
| 179 | if (isRelevant) { |
| 180 | pollDeviceMeters(device.id()); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |