blob: 8c86e01e7e249883f8293abb0b896571057197a8 [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -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 */
16package org.onosproject.incubator.net.meter.impl;
17
alshabib70aaa1b2015-09-25 14:30:59 -070018import com.google.common.collect.Maps;
alshabib1d2bc402015-07-31 17:04:11 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
alshabibeadfc8e2015-08-18 15:40:46 -070025import org.onlab.util.TriConsumer;
alshabib10c810b2015-08-18 16:59:04 -070026import org.onosproject.net.meter.DefaultMeter;
27import org.onosproject.net.meter.Meter;
28import org.onosproject.net.meter.MeterEvent;
29import org.onosproject.net.meter.MeterFailReason;
30import org.onosproject.net.meter.MeterId;
alshabib70aaa1b2015-09-25 14:30:59 -070031import org.onosproject.net.meter.MeterKey;
alshabib10c810b2015-08-18 16:59:04 -070032import org.onosproject.net.meter.MeterListener;
33import org.onosproject.net.meter.MeterOperation;
34import org.onosproject.net.meter.MeterProvider;
35import org.onosproject.net.meter.MeterProviderRegistry;
36import org.onosproject.net.meter.MeterProviderService;
alshabibe1248b62015-08-20 17:21:55 -070037import org.onosproject.net.meter.MeterRequest;
alshabib10c810b2015-08-18 16:59:04 -070038import org.onosproject.net.meter.MeterService;
39import org.onosproject.net.meter.MeterState;
40import org.onosproject.net.meter.MeterStore;
41import org.onosproject.net.meter.MeterStoreDelegate;
42import org.onosproject.net.meter.MeterStoreResult;
alshabib1d2bc402015-07-31 17:04:11 -070043import org.onosproject.net.DeviceId;
44import org.onosproject.net.provider.AbstractListenerProviderRegistry;
45import org.onosproject.net.provider.AbstractProviderService;
46import org.onosproject.store.service.AtomicCounter;
47import org.onosproject.store.service.StorageService;
48import org.slf4j.Logger;
49
50import java.util.Collection;
alshabib5eb79392015-08-19 18:09:55 -070051import java.util.Map;
52import java.util.stream.Collectors;
alshabib1d2bc402015-07-31 17:04:11 -070053
54import static org.slf4j.LoggerFactory.getLogger;
55
56
57/**
58 * Provides implementation of the meter service APIs.
59 */
alshabib58fe6dc2015-08-19 17:16:13 -070060@Component(immediate = true, enabled = true)
alshabib1d2bc402015-07-31 17:04:11 -070061@Service
62public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, MeterListener,
63 MeterProvider, MeterProviderService>
64 implements MeterService, MeterProviderRegistry {
65
alshabib70aaa1b2015-09-25 14:30:59 -070066 private static final String METERCOUNTERIDENTIFIER = "meter-id-counter-%s";
alshabib1d2bc402015-07-31 17:04:11 -070067 private final Logger log = getLogger(getClass());
68 private final MeterStoreDelegate delegate = new InternalMeterStoreDelegate();
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected StorageService storageService;
72
alshabib7bb05012015-08-05 10:15:09 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib58fe6dc2015-08-19 17:16:13 -070074 protected MeterStore store;
alshabib7bb05012015-08-05 10:15:09 -070075
alshabib70aaa1b2015-09-25 14:30:59 -070076 private Map<DeviceId, AtomicCounter> meterIdCounters
77 = Maps.newConcurrentMap();
alshabib1d2bc402015-07-31 17:04:11 -070078
alshabibe1248b62015-08-20 17:21:55 -070079 private TriConsumer<MeterRequest, MeterStoreResult, Throwable> onComplete;
alshabibeadfc8e2015-08-18 15:40:46 -070080
alshabib1d2bc402015-07-31 17:04:11 -070081 @Activate
82 public void activate() {
alshabib70aaa1b2015-09-25 14:30:59 -070083 /*meterIdCounter = storageService.atomicCounterBuilder()
84 .withName(METERCOUNTERIDENTIFIER)
85 .build();*/
alshabibeadfc8e2015-08-18 15:40:46 -070086
alshabib58fe6dc2015-08-19 17:16:13 -070087 store.setDelegate(delegate);
88
alshabibe1248b62015-08-20 17:21:55 -070089 onComplete = (request, result, error) ->
alshabibeadfc8e2015-08-18 15:40:46 -070090 {
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
alshabib58fe6dc2015-08-19 17:16:13 -0700166 public Collection<Meter> getAllMeters() {
167 return store.getAllMeters();
168 }
169
alshabib70aaa1b2015-09-25 14:30:59 -0700170 private MeterId allocateMeterId(DeviceId deviceId) {
171 long id = meterIdCounters.compute(deviceId, (k, v) -> {
172 if (v == null) {
173 return allocateCounter(k);
174 }
175 return v;
176 }).incrementAndGet();
177
178 return MeterId.meterId((int) id);
179 }
180
181 private AtomicCounter allocateCounter(DeviceId deviceId) {
182 return storageService.atomicCounterBuilder()
183 .withName(String.format(METERCOUNTERIDENTIFIER, deviceId))
184 .build();
alshabib1d2bc402015-07-31 17:04:11 -0700185 }
186
187 private class InternalMeterProviderService
188 extends AbstractProviderService<MeterProvider>
189 implements MeterProviderService {
190
191 /**
192 * Creates a provider service on behalf of the specified provider.
193 *
194 * @param provider provider to which this service is being issued
195 */
196 protected InternalMeterProviderService(MeterProvider provider) {
197 super(provider);
198 }
199
200 @Override
alshabib7bb05012015-08-05 10:15:09 -0700201 public void meterOperationFailed(MeterOperation operation,
202 MeterFailReason reason) {
203 store.failedMeter(operation, reason);
alshabib1d2bc402015-07-31 17:04:11 -0700204 }
205
206 @Override
207 public void pushMeterMetrics(DeviceId deviceId, Collection<Meter> meterEntries) {
alshabib5eb79392015-08-19 18:09:55 -0700208 //FIXME: FOLLOWING CODE CANNOT BE TESTED UNTIL SOMETHING THAT
209 //FIXME: IMPLEMENTS METERS EXISTS
210 Map<MeterId, Meter> storedMeterMap = store.getAllMeters().stream()
211 .collect(Collectors.toMap(Meter::id, m -> m));
212
213 meterEntries.stream()
214 .filter(m -> storedMeterMap.remove(m.id()) != null)
215 .forEach(m -> store.updateMeterState(m));
216
217 storedMeterMap.values().stream().forEach(m -> {
218 if (m.state() == MeterState.PENDING_ADD) {
219 provider().performMeterOperation(m.deviceId(),
220 new MeterOperation(m,
alshabibe1248b62015-08-20 17:21:55 -0700221 MeterOperation.Type.ADD));
alshabib5eb79392015-08-19 18:09:55 -0700222 } else {
223 store.deleteMeterNow(m);
224 }
225 });
alshabib1d2bc402015-07-31 17:04:11 -0700226 }
227 }
228
229 private class InternalMeterStoreDelegate implements MeterStoreDelegate {
230
231 @Override
232 public void notify(MeterEvent event) {
alshabibeadfc8e2015-08-18 15:40:46 -0700233 DeviceId deviceId = event.subject().deviceId();
234 MeterProvider p = getProvider(event.subject().deviceId());
alshabib7bb05012015-08-05 10:15:09 -0700235 switch (event.type()) {
alshabibeadfc8e2015-08-18 15:40:46 -0700236 case METER_ADD_REQ:
237 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700238 MeterOperation.Type.ADD));
alshabib7bb05012015-08-05 10:15:09 -0700239 break;
alshabibeadfc8e2015-08-18 15:40:46 -0700240 case METER_REM_REQ:
241 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700242 MeterOperation.Type.REMOVE));
alshabib7bb05012015-08-05 10:15:09 -0700243 break;
244 default:
245 log.warn("Unknown meter event {}", event.type());
246 }
alshabib1d2bc402015-07-31 17:04:11 -0700247
248 }
249 }
250
251}