blob: 2a69c2108727a5109c5fa9084e69632fa8b81d1a [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 *
16 */
17package org.onosproject.dhcprelay.store;
18
19
20import com.google.common.collect.ImmutableSet;
21import java.util.Set;
22import java.util.concurrent.ConcurrentHashMap;
23import java.util.Map;
24import java.util.Objects;
25
26public class DhcpRelayCounters {
27 // common counters
28
29 // IpV6 specific counters
30 public static final String SOLICIT = "SOLICIT";
31 public static final String ADVERTISE = "ADVERTISE";
32 public static final String REQUEST = "REQUEST";
33 public static final String CONFIRM = "CONFIRM";
34 public static final String RENEW = "RENEW";
35 public static final String REBIND = "REBIND";
36 public static final String REPLY = "REPLY";
37 public static final String RELEASE = "RELEASE";
38 public static final String DECLINE = "DECLINE";
39 public static final String RECONFIGURE = "RECONFIGURE";
40 public static final String INFORMATION_REQUEST = "INFORMATION_REQUEST";
41 public static final String RELAY_FORW = "RELAY_FORW";
42 public static final String RELAY_REPL = "RELAY_REPL";
43
44 public static final String NO_LINKLOCAL_GW = "No link-local in Gateway";
45 public static final String NO_LINKLOCAL_FAIL = "No link-local in CLIENT_ID";
46 public static final String NO_CLIENTID_FAIL = "No CLIENT_ID Found";
47 public static final String SVR_CFG_FAIL = "Server Config Error";
48 public static final String OPTION_MISSING_FAIL = "Expected Option missing";
49 public static final String NO_MATCHING_INTF = "No matching Inteface";
50 public static final String NO_CLIENT_INTF_MAC = "No client interface mac";
51 public static final String NO_SERVER_INFO = "No Server info found";
52 public static final String NO_SERVER_IP6ADDR = "No Server ip6 addr found";
53
54 public static final String INVALID_PACKET = "Invalid Packet";
55
56 public static final Set<String> SUPPORTED_COUNTERS =
57 ImmutableSet.of(SOLICIT, ADVERTISE, REQUEST, CONFIRM, RENEW,
58 REBIND, REPLY, RELEASE, DECLINE, RECONFIGURE,
59 INFORMATION_REQUEST, RELAY_FORW, RELAY_REPL,
60 NO_LINKLOCAL_GW, NO_LINKLOCAL_FAIL, NO_CLIENTID_FAIL, SVR_CFG_FAIL, OPTION_MISSING_FAIL,
61 NO_MATCHING_INTF, NO_CLIENT_INTF_MAC, NO_SERVER_INFO, NO_SERVER_IP6ADDR,
62 INVALID_PACKET);
63
64 // TODO Use AtomicInteger for the counters
65 private Map<String, Integer> countersMap = new ConcurrentHashMap<>();
66 public long lastUpdate;
67
68 public void resetCounters() {
69 countersMap.forEach((name, value) -> {
70 countersMap.put(name, 0);
71 });
72 }
73 public boolean incrementCounter(String name) {
74 boolean counterValid = false;
75 if (SUPPORTED_COUNTERS.contains(name)) {
76 Integer counter = countersMap.get(name);
77 if (counter != null) {
78 counter = counter + 1;
79 countersMap.put(name, counter);
80 } else {
81 // this is the first time
82 countersMap.put(name, 1);
83 }
84 lastUpdate = System.currentTimeMillis();
85 counterValid = true;
86 }
87 return counterValid;
88 }
89 public Map<String, Integer> getCounters() {
90 return countersMap;
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(countersMap, lastUpdate);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
103 if (!(obj instanceof DhcpRelayCounters)) {
104 return false;
105 }
106 DhcpRelayCounters that = (DhcpRelayCounters) obj;
107 return Objects.equals(countersMap, that.countersMap) &&
108 Objects.equals(lastUpdate, that.lastUpdate);
109 }
110}