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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.store.service.ConsistentMap; |
| 29 | import org.onosproject.store.service.Serializer; |
| 30 | import org.onosproject.store.service.StorageService; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | import org.onosproject.store.serializers.KryoNamespaces; |
| 34 | |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Map; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | import java.util.concurrent.ConcurrentHashMap; |
| 40 | import org.onosproject.store.service.Versioned; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | |
| 44 | @Component(immediate = true) |
| 45 | @Service |
| 46 | public 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 | } |