blob: 7f469af586ddaa55631541f796e2728c52e87433 [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;
32import org.onosproject.net.meter.MeterId;
alshabib70aaa1b2015-09-25 14:30:59 -070033import org.onosproject.net.meter.MeterKey;
alshabib10c810b2015-08-18 16:59:04 -070034import org.onosproject.net.meter.MeterListener;
35import org.onosproject.net.meter.MeterOperation;
36import org.onosproject.net.meter.MeterProvider;
37import org.onosproject.net.meter.MeterProviderRegistry;
38import org.onosproject.net.meter.MeterProviderService;
alshabibe1248b62015-08-20 17:21:55 -070039import org.onosproject.net.meter.MeterRequest;
alshabib10c810b2015-08-18 16:59:04 -070040import org.onosproject.net.meter.MeterService;
41import org.onosproject.net.meter.MeterState;
42import org.onosproject.net.meter.MeterStore;
43import org.onosproject.net.meter.MeterStoreDelegate;
44import org.onosproject.net.meter.MeterStoreResult;
alshabib1d2bc402015-07-31 17:04:11 -070045import org.onosproject.net.provider.AbstractListenerProviderRegistry;
46import org.onosproject.net.provider.AbstractProviderService;
47import org.onosproject.store.service.AtomicCounter;
48import org.onosproject.store.service.StorageService;
49import org.slf4j.Logger;
50
51import java.util.Collection;
alshabib5eb79392015-08-19 18:09:55 -070052import java.util.Map;
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -070053import java.util.function.Function;
alshabib5eb79392015-08-19 18:09:55 -070054import java.util.stream.Collectors;
alshabib1d2bc402015-07-31 17:04:11 -070055
56import static org.slf4j.LoggerFactory.getLogger;
57
alshabib1d2bc402015-07-31 17:04:11 -070058/**
59 * Provides implementation of the meter service APIs.
60 */
alshabib58fe6dc2015-08-19 17:16:13 -070061@Component(immediate = true, enabled = true)
alshabib1d2bc402015-07-31 17:04:11 -070062@Service
63public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, MeterListener,
64 MeterProvider, MeterProviderService>
65 implements MeterService, MeterProviderRegistry {
66
alshabib70aaa1b2015-09-25 14:30:59 -070067 private static final String METERCOUNTERIDENTIFIER = "meter-id-counter-%s";
alshabib1d2bc402015-07-31 17:04:11 -070068 private final Logger log = getLogger(getClass());
69 private final MeterStoreDelegate delegate = new InternalMeterStoreDelegate();
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected StorageService storageService;
73
alshabib7bb05012015-08-05 10:15:09 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib58fe6dc2015-08-19 17:16:13 -070075 protected MeterStore store;
alshabib7bb05012015-08-05 10:15:09 -070076
alshabib70aaa1b2015-09-25 14:30:59 -070077 private Map<DeviceId, AtomicCounter> meterIdCounters
78 = Maps.newConcurrentMap();
alshabib1d2bc402015-07-31 17:04:11 -070079
alshabibe1248b62015-08-20 17:21:55 -070080 private TriConsumer<MeterRequest, MeterStoreResult, Throwable> onComplete;
alshabibeadfc8e2015-08-18 15:40:46 -070081
alshabib1d2bc402015-07-31 17:04:11 -070082 @Activate
83 public void activate() {
alshabibeadfc8e2015-08-18 15:40:46 -070084
alshabib58fe6dc2015-08-19 17:16:13 -070085 store.setDelegate(delegate);
86
alshabibe1248b62015-08-20 17:21:55 -070087 onComplete = (request, result, error) ->
alshabibeadfc8e2015-08-18 15:40:46 -070088 {
alshabibe1248b62015-08-20 17:21:55 -070089 request.context().ifPresent(c -> {
alshabibeadfc8e2015-08-18 15:40:46 -070090 if (error != null) {
alshabibe1248b62015-08-20 17:21:55 -070091 c.onError(request, MeterFailReason.UNKNOWN);
alshabibeadfc8e2015-08-18 15:40:46 -070092 } else {
93 if (result.reason().isPresent()) {
alshabibe1248b62015-08-20 17:21:55 -070094 c.onError(request, result.reason().get());
alshabibeadfc8e2015-08-18 15:40:46 -070095 } else {
alshabibe1248b62015-08-20 17:21:55 -070096 c.onSuccess(request);
alshabibeadfc8e2015-08-18 15:40:46 -070097 }
98 }
99 });
100
101 };
alshabib1d2bc402015-07-31 17:04:11 -0700102 log.info("Started");
103 }
104
105 @Deactivate
106 public void deactivate() {
alshabib58fe6dc2015-08-19 17:16:13 -0700107 store.unsetDelegate(delegate);
alshabib1d2bc402015-07-31 17:04:11 -0700108 log.info("Stopped");
109 }
110
111 @Override
112 protected MeterProviderService createProviderService(MeterProvider provider) {
113 return new InternalMeterProviderService(provider);
114 }
115
116 @Override
alshabibe1248b62015-08-20 17:21:55 -0700117 public Meter submit(MeterRequest request) {
118
alshabib70aaa1b2015-09-25 14:30:59 -0700119 MeterId id = allocateMeterId(request.deviceId());
120
alshabibe1248b62015-08-20 17:21:55 -0700121 Meter.Builder mBuilder = DefaultMeter.builder()
122 .forDevice(request.deviceId())
123 .fromApp(request.appId())
124 .withBands(request.bands())
alshabib70aaa1b2015-09-25 14:30:59 -0700125 .withId(id)
alshabibe1248b62015-08-20 17:21:55 -0700126 .withUnit(request.unit());
127
128 if (request.isBurst()) {
129 mBuilder.burst();
130 }
131 DefaultMeter m = (DefaultMeter) mBuilder.build();
alshabib7bb05012015-08-05 10:15:09 -0700132 m.setState(MeterState.PENDING_ADD);
alshabibeadfc8e2015-08-18 15:40:46 -0700133 store.storeMeter(m).whenComplete((result, error) ->
alshabibe1248b62015-08-20 17:21:55 -0700134 onComplete.accept(request, result, error));
135 return m;
alshabib1d2bc402015-07-31 17:04:11 -0700136 }
137
138 @Override
alshabibe1248b62015-08-20 17:21:55 -0700139 public void withdraw(MeterRequest request, MeterId meterId) {
140 Meter.Builder mBuilder = DefaultMeter.builder()
141 .forDevice(request.deviceId())
142 .fromApp(request.appId())
143 .withBands(request.bands())
144 .withId(meterId)
145 .withUnit(request.unit());
alshabib1d2bc402015-07-31 17:04:11 -0700146
alshabibe1248b62015-08-20 17:21:55 -0700147 if (request.isBurst()) {
148 mBuilder.burst();
149 }
150
151 DefaultMeter m = (DefaultMeter) mBuilder.build();
alshabib7bb05012015-08-05 10:15:09 -0700152 m.setState(MeterState.PENDING_REMOVE);
alshabibeadfc8e2015-08-18 15:40:46 -0700153 store.deleteMeter(m).whenComplete((result, error) ->
alshabibe1248b62015-08-20 17:21:55 -0700154 onComplete.accept(request, result, error));
alshabib1d2bc402015-07-31 17:04:11 -0700155 }
156
157 @Override
alshabib70aaa1b2015-09-25 14:30:59 -0700158 public Meter getMeter(DeviceId deviceId, MeterId id) {
159 MeterKey key = MeterKey.key(deviceId, id);
160 return store.getMeter(key);
alshabib1d2bc402015-07-31 17:04:11 -0700161 }
162
163 @Override
Jian Li1932b932016-01-03 00:35:40 -0800164 public Collection<Meter> getMeters(DeviceId deviceId) {
165 return store.getAllMeters().stream().filter(m ->
166 m.deviceId().equals(deviceId)).collect(Collectors.toList());
167 }
168
169 @Override
alshabib58fe6dc2015-08-19 17:16:13 -0700170 public Collection<Meter> getAllMeters() {
171 return store.getAllMeters();
172 }
173
alshabib70aaa1b2015-09-25 14:30:59 -0700174 private MeterId allocateMeterId(DeviceId deviceId) {
175 long id = meterIdCounters.compute(deviceId, (k, v) -> {
176 if (v == null) {
177 return allocateCounter(k);
178 }
179 return v;
180 }).incrementAndGet();
181
182 return MeterId.meterId((int) id);
183 }
184
185 private AtomicCounter allocateCounter(DeviceId deviceId) {
Madan Jampanid5714e02016-04-19 14:15:20 -0700186 return storageService.getAtomicCounter(String.format(METERCOUNTERIDENTIFIER, deviceId));
alshabib1d2bc402015-07-31 17:04:11 -0700187 }
188
189 private class InternalMeterProviderService
190 extends AbstractProviderService<MeterProvider>
191 implements MeterProviderService {
192
193 /**
194 * Creates a provider service on behalf of the specified provider.
195 *
196 * @param provider provider to which this service is being issued
197 */
198 protected InternalMeterProviderService(MeterProvider provider) {
199 super(provider);
200 }
201
202 @Override
alshabib7bb05012015-08-05 10:15:09 -0700203 public void meterOperationFailed(MeterOperation operation,
204 MeterFailReason reason) {
205 store.failedMeter(operation, reason);
alshabib1d2bc402015-07-31 17:04:11 -0700206 }
207
208 @Override
209 public void pushMeterMetrics(DeviceId deviceId, Collection<Meter> meterEntries) {
alshabib5eb79392015-08-19 18:09:55 -0700210 //FIXME: FOLLOWING CODE CANNOT BE TESTED UNTIL SOMETHING THAT
211 //FIXME: IMPLEMENTS METERS EXISTS
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500212 Map<Pair<DeviceId, MeterId>, Meter> storedMeterMap = store.getAllMeters().stream()
Sho SHIMIZU0e03f59b2016-06-08 17:03:48 -0700213 .collect(Collectors.toMap(m -> Pair.of(m.deviceId(), m.id()), Function.identity()));
alshabib5eb79392015-08-19 18:09:55 -0700214
215 meterEntries.stream()
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500216 .filter(m -> storedMeterMap.remove(Pair.of(m.deviceId(), m.id())) != null)
alshabib5eb79392015-08-19 18:09:55 -0700217 .forEach(m -> store.updateMeterState(m));
218
219 storedMeterMap.values().stream().forEach(m -> {
220 if (m.state() == MeterState.PENDING_ADD) {
221 provider().performMeterOperation(m.deviceId(),
222 new MeterOperation(m,
Konstantinos Kanonakisa45755d2016-03-14 11:31:23 -0500223 MeterOperation.Type.MODIFY));
224 } else if (m.state() == MeterState.PENDING_REMOVE) {
alshabib5eb79392015-08-19 18:09:55 -0700225 store.deleteMeterNow(m);
226 }
227 });
alshabib1d2bc402015-07-31 17:04:11 -0700228 }
229 }
230
231 private class InternalMeterStoreDelegate implements MeterStoreDelegate {
232
233 @Override
234 public void notify(MeterEvent event) {
alshabibeadfc8e2015-08-18 15:40:46 -0700235 DeviceId deviceId = event.subject().deviceId();
236 MeterProvider p = getProvider(event.subject().deviceId());
alshabib7bb05012015-08-05 10:15:09 -0700237 switch (event.type()) {
alshabibeadfc8e2015-08-18 15:40:46 -0700238 case METER_ADD_REQ:
239 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700240 MeterOperation.Type.ADD));
alshabib7bb05012015-08-05 10:15:09 -0700241 break;
alshabibeadfc8e2015-08-18 15:40:46 -0700242 case METER_REM_REQ:
243 p.performMeterOperation(deviceId, new MeterOperation(event.subject(),
alshabibe1248b62015-08-20 17:21:55 -0700244 MeterOperation.Type.REMOVE));
alshabib7bb05012015-08-05 10:15:09 -0700245 break;
246 default:
247 log.warn("Unknown meter event {}", event.type());
248 }
alshabib1d2bc402015-07-31 17:04:11 -0700249
250 }
251 }
252
253}