Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [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.opencord.dhcpl2relay.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 22 | import org.osgi.service.component.annotations.Activate; |
| 23 | import org.osgi.service.component.annotations.Component; |
| 24 | import org.osgi.service.component.annotations.Reference; |
| 25 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.Iterator; |
| 29 | import java.util.Map; |
| 30 | import java.util.concurrent.ConcurrentHashMap; |
| 31 | import java.util.concurrent.atomic.AtomicLong; |
| 32 | |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * DHCP Relay Agent Counters Manager Component. |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | public class SimpleDhcpL2RelayCountersStore implements DhcpL2RelayCountersStore { |
| 41 | private ApplicationId appId; |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | private Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap; |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 46 | protected CoreService coreService; |
| 47 | |
| 48 | @Activate |
| 49 | public void activate() { |
| 50 | log.info("Activate Dhcp L2 Counters Manager"); |
| 51 | //appId = coreService.getAppId(DhcpL2Relay.DHCP_L2RELAY_APP); |
| 52 | countersMap = new ConcurrentHashMap(); |
| 53 | // Initialize counter values for the global counters |
| 54 | initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER); |
| 55 | } |
| 56 | |
| 57 | public ImmutableMap<DhcpL2RelayCountersIdentifier, AtomicLong> getCountersMap() { |
| 58 | return ImmutableMap.copyOf(countersMap); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Initialize the supported counters map for the given counter class. |
| 63 | * @param counterClass class of counters (global, per subscriber) |
| 64 | */ |
| 65 | public void initCounters(String counterClass) { |
| 66 | checkNotNull(counterClass, "counter class can't be null"); |
| 67 | for (DhcpL2RelayCounters counterType : DhcpL2RelayCounters.SUPPORTED_COUNTERS) { |
| 68 | countersMap.put(new DhcpL2RelayCountersIdentifier(counterClass, counterType), new AtomicLong(0)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Inserts the counter entry if it is not already in the set otherwise increment the existing counter entry. |
| 74 | * @param counterClass class of counters (global, per subscriber) |
| 75 | * @param counterType name of counter |
| 76 | */ |
| 77 | public void incrementCounter(String counterClass, DhcpL2RelayCounters counterType) { |
| 78 | checkNotNull(counterClass, "counter class can't be null"); |
| 79 | if (DhcpL2RelayCounters.SUPPORTED_COUNTERS.contains(counterType)) { |
| 80 | DhcpL2RelayCountersIdentifier counterIdentifier = |
| 81 | new DhcpL2RelayCountersIdentifier(counterClass, counterType); |
| 82 | countersMap.compute(counterIdentifier, (key, counterValue) -> |
| 83 | (counterValue != null) ? new AtomicLong(counterValue.incrementAndGet()) : new AtomicLong(1) |
| 84 | ); |
| 85 | } else { |
| 86 | log.error("Failed to increment counter class {} of type {}", counterClass, counterType); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Reset the counters map for the given counter class. |
| 92 | * @param counterClass class of counters (global, per subscriber) |
| 93 | */ |
| 94 | public void resetCounters(String counterClass) { |
| 95 | checkNotNull(counterClass, "counter class can't be null"); |
| 96 | for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator(); it.hasNext();) { |
| 97 | DhcpL2RelayCounters counterType = it.next(); |
| 98 | DhcpL2RelayCountersIdentifier counterIdentifier = |
| 99 | new DhcpL2RelayCountersIdentifier(counterClass, counterType); |
| 100 | countersMap.computeIfPresent(counterIdentifier, (key, counterValue) -> |
| 101 | new AtomicLong(0) |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Inserts the counter entry if it is not already in the set otherwise update the existing counter entry. |
| 108 | * @param counterClass class of counters (global, per subscriber). |
| 109 | * @param counterType name of counter |
| 110 | * @param value conter value |
| 111 | */ |
| 112 | public void setCounter(String counterClass, DhcpL2RelayCounters counterType, Long value) { |
| 113 | checkNotNull(counterClass, "counter class can't be null"); |
| 114 | if (DhcpL2RelayCounters.SUPPORTED_COUNTERS.contains(counterType)) { |
| 115 | DhcpL2RelayCountersIdentifier counterIdentifier = |
| 116 | new DhcpL2RelayCountersIdentifier(counterClass, counterType); |
| 117 | countersMap.put(counterIdentifier, new AtomicLong(value)); |
| 118 | } else { |
| 119 | log.error("Failed to increment counter class {} of type {}", counterClass, counterType); |
| 120 | } |
| 121 | } |
| 122 | } |