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