Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.dhcprelay.store; |
| 17 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 21 | import org.onosproject.store.serializers.KryoNamespaces; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 22 | import org.onosproject.store.service.ConsistentMap; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 25 | import org.onosproject.store.service.Versioned; |
| 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Deactivate; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 33 | |
| 34 | import java.util.HashSet; |
| 35 | import java.util.Map; |
| 36 | import java.util.Optional; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 42 | @Component(immediate = true, service = DhcpRelayCountersStore.class) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 43 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 52 | protected StorageService storageService; |
| 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 55 | 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 | } |