blob: 4b7b26c11e0a122223f372f4c4b6c16b269fff63 [file] [log] [blame]
Kalhee Kimd94ceea2017-11-29 19:03:02 +00001/*
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 */
16package org.onosproject.dhcprelay.store;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24
25import org.onlab.util.KryoNamespace;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.StorageService;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33import org.onosproject.store.serializers.KryoNamespaces;
34
35import java.util.HashSet;
36import java.util.Map;
37import java.util.Optional;
38import java.util.Set;
39import java.util.concurrent.ConcurrentHashMap;
40import org.onosproject.store.service.Versioned;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43
44@Component(immediate = true)
45@Service
46public class DistributedDhcpRelayCountersStore implements DhcpRelayCountersStore {
47 private static final KryoNamespace.Builder APP_KYRO = KryoNamespace.newBuilder()
48 .register(KryoNamespaces.API)
49 .register(DhcpRelayCounters.class);
50
51 private Logger log = LoggerFactory.getLogger(getClass());
52 private ConsistentMap<String, DhcpRelayCounters> counters;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected StorageService storageService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected CoreService coreService;
59
60
61 @Activate
62 protected void activated() {
63 ApplicationId appId = coreService.getAppId("org.onosproject.Dhcp6HandlerImpl");
64 counters = storageService.<String, DhcpRelayCounters>consistentMapBuilder()
65 .withSerializer(Serializer.using(APP_KYRO.build()))
66 .withName("Dhcp-Relay-Counters")
67 .withApplicationId(appId)
68 .withPurgeOnUninstall()
69 .build();
70 }
71
72 @Deactivate
73 protected void deactivated() {
74 counters.destroy().join();
75 }
76 @Override
77 public void incrementCounter(String coutnerClass, String counterName) {
78 DhcpRelayCounters countersRecord;
79
80 Versioned<DhcpRelayCounters> vCounters = counters.get(coutnerClass);
81 if (vCounters == null) {
82 countersRecord = new DhcpRelayCounters();
83 } else {
84 countersRecord = vCounters.value();
85 }
86 countersRecord.incrementCounter(counterName);
87 counters.put(coutnerClass, countersRecord);
88 }
89
90 @Override
91 public Set<Map.Entry<String, DhcpRelayCounters>> getAllCounters() {
92 final Set<Map.Entry<String, DhcpRelayCounters>> result =
93 new HashSet<Map.Entry<String, DhcpRelayCounters>>();
94 Set<Map.Entry<String, Versioned<DhcpRelayCounters>>> tmpCounters = counters.entrySet();
95 tmpCounters.forEach(entry -> {
96 String key = entry.getKey();
97 DhcpRelayCounters value = entry.getValue().value();
98 ConcurrentHashMap<String, DhcpRelayCounters> newMap = new ConcurrentHashMap();
99 newMap.put(key, value);
100
101 for (Map.Entry m: newMap.entrySet()) {
102 result.add(m);
103 }
104 });
105 return result;
106 }
107 @Override
108 public Optional<DhcpRelayCounters> getCounters(String counterClass) {
109 DhcpRelayCounters countersRecord;
110 checkNotNull(counterClass, "counter class can't be null");
111 Versioned<DhcpRelayCounters> vCounters = counters.get(counterClass);
112 if (vCounters == null) {
113 return Optional.empty();
114 }
115 return Optional.of(vCounters.value());
116 }
117 @Override
118 public void resetAllCounters() {
119 counters.clear();
120 }
121
122 @Override
123 public void resetCounters(String counterClass) {
124 checkNotNull(counterClass, "counter class can't be null");
125 DhcpRelayCounters countersRecord = counters.get(counterClass).value();
126 countersRecord.resetCounters();
127 counters.put(counterClass, countersRecord);
128 }
129
130}