senthil | 46003d1 | 2024-03-21 12:23:42 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2024-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 | |
| 17 | package org.onosproject.bgpmonitoring; |
| 18 | |
| 19 | import org.onlab.packet.DeserializationException; |
| 20 | import java.util.Arrays; |
| 21 | import java.util.Map; |
| 22 | import java.util.Optional; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
| 25 | /** |
| 26 | * Enum to Provide the Different types of BMP mirroring message. |
| 27 | */ |
| 28 | public enum MirroringType { |
| 29 | |
| 30 | BGP_MESSAGE(0), |
| 31 | |
| 32 | INFORMATION(1); |
| 33 | |
| 34 | private final int value; |
| 35 | |
| 36 | /** |
| 37 | * Assign value with the value val as the types of BMP mirroring message. |
| 38 | * |
| 39 | * @param val type of BMP mirroring message |
| 40 | */ |
| 41 | MirroringType(int val) { |
| 42 | value = val; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Returns value as type of BMP mirroring message. |
| 48 | * |
| 49 | * @return value type of BMP mirroring message |
| 50 | */ |
| 51 | public int getType() { |
| 52 | return value; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | private static Map<Integer, MirroringType> parser = new ConcurrentHashMap<>(); |
| 57 | |
| 58 | static { |
| 59 | Arrays.stream(MirroringType.values()).forEach(v -> parser.put(v.value, v)); |
| 60 | } |
| 61 | |
| 62 | public static MirroringType getType(int type) throws DeserializationException { |
| 63 | if (type > 2) { |
| 64 | throw new DeserializationException("Invalid bmp mirroring type"); |
| 65 | } |
| 66 | return Optional.of(type) |
| 67 | .filter(id -> parser.containsKey(id)) |
| 68 | .map(id -> parser.get(id)) |
| 69 | .orElse(BGP_MESSAGE); |
| 70 | } |
| 71 | |
| 72 | } |