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