blob: c1326027f1cde528da9a27cc6756b7b4b0e9630c [file] [log] [blame]
senthil46003d12024-03-21 12:23:42 +05301/*
2 * Copyright 2021-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.bgpmonitoring;
18
19
20import org.onlab.packet.DeserializationException;
21import java.util.Arrays;
22import java.util.Map;
23import java.util.Optional;
24import java.util.concurrent.ConcurrentHashMap;
25
26/**
27 * Enum to Provide the Different types of BMP peer down reasons.
28 */
29public enum PeerDownReason {
30
31 LOCAL_SYSTEM_CLOSED_SESSION_WITH_NOTIFICATION(1),
32
33 LOCAL_SYSTEM_CLOSED_SESSION_WITHOUT_NOTIFICATION(2),
34
35 REMOTE_SYSTEM_CLOSED_SESSION_WITH_NOTIFICATION(3),
36
37 REMOTE_SYSTEM_CLOSED_SESSION_WITHOUT_NOTIFICATION(4);
38
39
40 private final int value;
41
42 /**
43 * Assign value with the value val as the types of BMP peer down reasons.
44 *
45 * @param val type of BMP peer down reasons
46 */
47 PeerDownReason(int val) {
48 value = val;
49 }
50
51 /**
52 * Returns value as type of BMP peer down reasons.
53 *
54 * @return value type of BMP peer down reasons
55 */
56 public int getReason() {
57 return value;
58 }
59
60
61 private static Map<Integer, PeerDownReason> parser = new ConcurrentHashMap<>();
62
63 static {
64 Arrays.stream(PeerDownReason.values()).forEach(v -> parser.put(v.value, v));
65 }
66
67 public static PeerDownReason getType(int type) throws DeserializationException {
68 if (type > 4) {
69 throw new DeserializationException("Invalid bmp peer down reason type");
70 }
71 return Optional.of(type)
72 .filter(id -> parser.containsKey(id))
73 .map(id -> parser.get(id))
74 .orElse(LOCAL_SYSTEM_CLOSED_SESSION_WITH_NOTIFICATION);
75 }
76}