blob: db5cff049393326d0dbabc0adfba93d894073027 [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabib1d2bc402015-07-31 17:04:11 -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 */
16package org.onosproject.incubator.net.meter.impl;
17
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -050018import org.apache.commons.lang3.tuple.Pair;
alshabib70aaa1b2015-09-25 14:30:59 -070019import com.google.common.collect.Maps;
alshabib1d2bc402015-07-31 17:04:11 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
alshabibeadfc8e2015-08-18 15:40:46 -070026import org.onlab.util.TriConsumer;
Jian Li1932b932016-01-03 00:35:40 -080027import org.onosproject.net.DeviceId;
alshabib10c810b2015-08-18 16:59:04 -070028import org.onosproject.net.meter.DefaultMeter;
29import org.onosproject.net.meter.Meter;
30import org.onosproject.net.meter.MeterEvent;
31import org.onosproject.net.meter.MeterFailReason;
Jordi Ortizaa8de492016-12-01 00:21:36 +010032import org.onosproject.net.meter.MeterFeatures;
Jordi Ortiz21daebf2016-12-27 18:50:14 +010033import org.onosproject.net.meter.MeterFeaturesKey;
alshabib10c810b2015-08-18 16:59:04 -070034import org.onosproject.net.meter.MeterId;
alshabib70aaa1b2015-09-25 14:30:59 -070035import org.onosproject.net.meter.MeterKey;
alshabib10c810b2015-08-18 16:59:04 -070036import org.onosproject.net.meter.MeterListener;
37import org.onosproject.net.meter.MeterOperation;
38import org.onosproject.net.meter.MeterProvider;
39import org.onosproject.net.meter.MeterProviderRegistry;
40import org.onosproject.net.meter.MeterProviderService;
alshabibe1248b62015-08-20 17:21:55 -070041import org.onosproject.net.meter.MeterRequest;
alshabib10c810b2015-08-18 16:59:04 -070042import org.onosproject.net.meter.MeterService;
43import org.onosproject.net.meter.MeterState;
44import org.onosproject.net.meter.MeterStore;
45import org.onosproject.net.meter.MeterStoreDelegate;
46import org.onosproject.net.meter.MeterStoreResult;
alshabib1d2bc402015-07-31 17:04:11 -070047import org.onosproject.net.provider.AbstractListenerProviderRegistry;
48import org.onosproject.net.provider.AbstractProviderService;
49import org.onosproject.store.service.AtomicCounter;
50import org.onosproject.store.service.StorageService;
51import org.slf4j.Logger;
52
53import java.util.Collection;
alshabib5eb79392015-08-19 18:09:55 -070054import java.util.Map;
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070055import java.util.function.Function;
alshabib5eb79392015-08-19 18:09:55 -070056import java.util.stream.Collectors;
alshabib1d2bc402015-07-31 17:04:11 -070057
58import static org.slf4j.LoggerFactory.getLogger;
59
alshabib1d2bc402015-07-31 17:04:11 -070060/**
61 * Provides implementation of the meter service APIs.
62 */
alshabib58fe6dc2015-08-19 17:16:13 -070063@Component(immediate = true, enabled = true)
alshabib1d2bc402015-07-31 17:04:11 -070064@Service
65public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, MeterListener,
66 MeterProvider, MeterProviderService>
67 implements MeterService, MeterProviderRegistry {
68
alshabib70aaa1b2015-09-25 14:30:59 -070069 private static final String METERCOUNTERIDENTIFIER = "meter-id-counter-%s";
alshabib1d2bc402015-07-31 17:04:11 -070070 private final Logger log = getLogger(getClass());
71 private final MeterStoreDelegate delegate = new InternalMeterStoreDelegate();
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected StorageService storageService;
75
alshabib7bb05012015-08-05 10:15:09 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib58fe6dc2015-08-19 17:16:13 -070077 protected MeterStore store;
alshabib7bb05012015-08-05 10:15:09 -070078
alshabib70aaa1b2015-09-25 14:30:59 -070079 private Map<DeviceId, AtomicCounter> meterIdCounters
80 = Maps.newConcurrentMap();
alshabib1d2bc402015-07-31 17:04:11 -070081
alshabibe1248b62015-08-20 17:21:55 -070082 private TriConsumer<MeterRequest, MeterStoreResult, Throwable> onComplete;
alshabibeadfc8e2015-08-18 15:40:46 -070083
alshabib1d2bc402015-07-31 17:04:11 -070084 @Activate
85 public void activate() {
alshabibeadfc8e2015-08-18 15:40:46 -070086
alshabib58fe6dc2015-08-19 17:16:13 -070087 store.setDelegate(delegate);
Jordi Ortiz3a6f3072016-11-07 10:23:48 +010088 eventDispatcher.addSink(MeterEvent.class, listenerRegistry);
alshabib58fe6dc2015-08-19 17:16:13 -070089
Jordi Ortiz3a6f3072016-11-07 10:23:48 +010090 onComplete = (request, result, error) -> {
alshabibe1248b62015-08-20 17:21:55 -070091 request.context().ifPresent(c -> {
alshabibeadfc8e2015-08-18 15:40:46 -070092 if (error != null) {
alshabibe1248b62015-08-20 17:21:55 -070093 c.onError(request, MeterFailReason.UNKNOWN);
alshabibeadfc8e2015-08-18 15:40:46 -070094 } else {
95 if (result.reason().isPresent()) {
alshabibe1248b62015-08-20 17:21:55 -070096 c.onError(request, result.reason().get());
alshabibeadfc8e2015-08-18 15:40:46 -070097 } else {
alshabibe1248b62015-08-20 17:21:55 -070098 c.onSuccess(request);
alshabibeadfc8e2015-08-18 15:40:46 -070099 }
100 }
101 });
102
103 };
alshabib1d2bc402015-07-31 17:04:11 -0700104 log.info("Started");
105 }
106
107 @Deactivate
108 public void deactivate() {
alshabib58fe6dc2015-08-19 17:16:13 -0700109 store.unsetDelegate(delegate);
alshabib1d2bc402015-07-31 17:04:11 -0700110 log.info("Stopped");
111 }
112
113 @Override
114 protected MeterProviderService createProviderService(MeterProvider provider) {
115 return new InternalMeterProviderService(provider);
116 }
117
118 @Override
alshabibe1248b62015-08-20 17:21:55 -0700119 public Meter submit(MeterRequest request) {
120
alshabib70aaa1b2015-09-25 14:30:59 -0700121 MeterId id = allocateMeterId(request.deviceId());
122
alshabibe1248b62015-08-20 17:21:55 -0700123 Meter.Builder mBuilder = DefaultMeter.builder()
124 .forDevice(request.deviceId())
125 .fromApp(request.appId())
126 .withBands(request.bands())
alshabib70aaa1b2015-09-25 14:30:59 -0700127 .withId(id)
alshabibe1248b62015-08-20 17:21:55 -0700128 .withUnit(request.unit());
129
130 if (request.isBurst()) {
131 mBuilder.burst();
132 }
133 DefaultMeter m = (DefaultMeter) mBuilder.build();
alshabib7bb05012015-08-05 10:15:09 -0700134 m.setState(MeterState.PENDING_ADD);
alshabibeadfc8e2015-08-18 15:40:46 -0700135 store.storeMeter(m).whenComplete((result, error) ->
alshabibe1248b62015-08-20 17:21:55 -0700136 onComplete.accept(request, result, error));
137 return m;
alshabib1d2bc402015-07-31 17:04:11 -0700138 }
139
140 @Override
alshabibe1248b62015-08-20 17:21:55 -0700141 public void withdraw(MeterRequest request, MeterId meterId) {
142 Meter.Builder mBuilder = DefaultMeter.builder()
143 .forDevice(request.deviceId())
144 .fromApp(request.appId())
145 .withBands(request.bands())
146 .withId(meterId)
147 .withUnit(request.unit());
alshabib1d2bc402015-07-31 17:04:11 -0700148
alshabibe1248b62015-08-20 17:21:55 -0700149 if (request.isBurst()) {
150 mBuilder.burst();
151 }
152
153 DefaultMeter m = (DefaultMeter) mBuilder.build();
alshabib7bb05012015-08-05 10:15:09 -0700154 m.setState(MeterState.PENDING_REMOVE);
alshabibeadfc8e2015-08-18 15:40:46 -0700155 store.deleteMeter(m).whenComplete((result, error) ->
alshabibe1248b62015-08-20 17:21:55 -0700156 onComplete.accept(request, result, error));
alshabib1d2bc402015-07-31 17:04:11 -0700157 }
158
159 @Override
alshabib70aaa1b2015-09-25 14:30:59 -0700160 public Meter getMeter(DeviceId deviceId, MeterId id) {
161 MeterKey key = MeterKey.key(deviceId, id);
162 return store.getMeter(key);
alshabib1d2bc402015-07-31 17:04:11 -0700163 }
164
165 @Override
Jian Li1932b932016-01-03 00:35:40 -0800166 public Collection<Meter> getMeters(DeviceId deviceId) {
167 return store.getAllMeters().stream().filter(m ->
168 m.deviceId().equals(deviceId)).collect(Collectors.toList());
169 }
170
171 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700172 public Collection<Meter> getAllMeters() {
173 return store.getAllMeters();
174 }
175
alshabib70aaa1b2015-09-25 14:30:59 -0700176 private MeterId allocateMeterId(DeviceId deviceId) {
Jordi Ortiz21daebf2016-12-27 18:50:14 +0100177 long maxMeters = store.getMaxMeters(MeterFeaturesKey.key(deviceId));
178
179 if (maxMeters == 0L) {
180 throw new IllegalStateException("Meters not supported by device " + deviceId);
181 }
182
183 final long mmeters = maxMeters;
alshabib70aaa1b2015-09-25 14:30:59 -0700184 long id = meterIdCounters.compute(deviceId, (k, v) -> {
185 if (v == null) {
186 return allocateCounter(k);
187 }
Jordi Ortiz21daebf2016-12-27 18:50:14 +0100188 if (v.get() >= mmeters) {
189 throw new IllegalStateException("Maximum number of meters " +
190 meterIdCounters.get(deviceId).get() +
191 " reached for device " + deviceId);
192 }
alshabib70aaa1b2015-09-25 14:30:59 -0700193 return v;
194 }).incrementAndGet();
195
Jordi Ortiz21daebf2016-12-27 18:50:14 +0100196 return MeterId.meterId(id);
alshabib70aaa1b2015-09-25 14:30:59 -0700197 }
198
199 private AtomicCounter allocateCounter(DeviceId deviceId) {
Madan Jampanid5714e02016-04-19 14:15:20 -0700200 return storageService.getAtomicCounter(String.format(METERCOUNTERIDENTIFIER, deviceId));
alshabib1d2bc402015-07-31 17:04:11 -0700201 }
202
203 private class InternalMeterProviderService
204 extends AbstractProviderService<MeterProvider>
205 implements MeterProviderService {
206
207 /**
208 * Creates a provider service on behalf of the specified provider.
209 *
210 * @param provider provider to which this service is being issued
211 */
212 protected InternalMeterProviderService(MeterProvider provider) {
213 super(provider);
214 }
215
216 @Override
alshabib7bb05012015-08-05 10:15:09 -0700217 public void meterOperationFailed(MeterOperation operation,
218 MeterFailReason reason) {
219 store.failedMeter(operation, reason);
alshabib1d2bc402015-07-31 17:04:11 -0700220 }
221
222 @Override
223 public void pushMeterMetrics(DeviceId deviceId, Collection<Meter> meterEntries) {
alshabib5eb79392015-08-19 18:09:55 -0700224 //FIXME: FOLLOWING CODE CANNOT BE TESTED UNTIL SOMETHING THAT
225 //FIXME: IMPLEMENTS METERS EXISTS
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500226 Map<Pair<DeviceId, MeterId>, Meter> storedMeterMap = store.getAllMeters().stream()
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -0700227 .collect(Collectors.toMap(m -> Pair.of(m.deviceId(), m.id()), Function.identity()));
alshabib5eb79392015-08-19 18:09:55 -0700228
229 meterEntries.stream()
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500230 .filter(m -> storedMeterMap.remove(Pair.of(m.deviceId(), m.id())) != null)
alshabib5eb79392015-08-19 18:09:55 -0700231 .forEach(m -> store.updateMeterState(m));
232
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700233 storedMeterMap.values().forEach(m -> {
alshabib5eb79392015-08-19 18:09:55 -0700234 if (m.state() == MeterState.PENDING_ADD) {
235 provider().performMeterOperation(m.deviceId(),
236 new MeterOperation(m,
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500237 MeterOperation.Type.MODIFY));
238 } else if (m.state() == MeterState.PENDING_REMOVE) {
alshabib5eb79392015-08-19 18:09:55 -0700239 store.deleteMeterNow(m);
240 }
241 });
alshabib1d2bc402015-07-31 17:04:11 -0700242 }
Jordi Ortizaa8de492016-12-01 00:21:36 +0100243
244 @Override
245 public void pushMeterFeatures(DeviceId deviceId, MeterFeatures meterfeatures) {
246 store.storeMeterFeatures(meterfeatures);
247 }
248
249 @Override
250 public void deleteMeterFeatures(DeviceId deviceId) {
251 store.deleteMeterFeatures(deviceId);
252 }
alshabib1d2bc402015-07-31 17:04:11 -0700253 }
254
255 private class InternalMeterStoreDelegate implements MeterStoreDelegate {
256
257 @Override
258 public void notify(MeterEvent event) {
alshabibeadfc8e2015-08-18 15:40:46 -0700259 DeviceId deviceId = event.subject().deviceId();
260 MeterProvider p = getProvider(event.subject().deviceId());
alshabib7bb05012015-08-05 10:15:09 -0700261 switch (event.type()) {
alshabibeadfc8e2015-08-18 15:40:46 -0700262 case METER_ADD_REQ:
263 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700264 MeterOperation.Type.ADD));
alshabib7bb05012015-08-05 10:15:09 -0700265 break;
alshabibeadfc8e2015-08-18 15:40:46 -0700266 case METER_REM_REQ:
267 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700268 MeterOperation.Type.REMOVE));
alshabib7bb05012015-08-05 10:15:09 -0700269 break;
270 default:
271 log.warn("Unknown meter event {}", event.type());
272 }
alshabib1d2bc402015-07-31 17:04:11 -0700273
274 }
275 }
276
277}