Jian Li | 01a6167 | 2016-02-11 14:59:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 01a6167 | 2016-02-11 14:59:43 -0800 | [diff] [blame] | 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.onosproject.cpman.impl; |
| 17 | |
| 18 | import com.google.common.collect.BiMap; |
| 19 | import com.google.common.collect.EnumHashBiMap; |
| 20 | import org.onosproject.cpman.ControlMessage; |
| 21 | import org.onosproject.cpman.ControlMetricType; |
| 22 | |
| 23 | import static org.onosproject.cpman.ControlMessage.Type; |
| 24 | |
| 25 | /** |
| 26 | * Collection of helper methods to convert protocol agnostic control message |
| 27 | * type to control metric type. |
| 28 | */ |
| 29 | public final class ControlMessageMetricMapper { |
| 30 | |
| 31 | // prohibit instantiation |
| 32 | private ControlMessageMetricMapper() { |
| 33 | } |
| 34 | |
| 35 | private static final BiMap<ControlMessage.Type, ControlMetricType> MESSAGE_TYPE = |
| 36 | EnumHashBiMap.create(ControlMessage.Type.class); |
| 37 | |
| 38 | static { |
| 39 | // key is protocol agnostic ControlMessage.Type |
| 40 | // value is ControlMetricType |
| 41 | MESSAGE_TYPE.put(Type.INBOUND_PACKET, ControlMetricType.INBOUND_PACKET); |
| 42 | MESSAGE_TYPE.put(Type.OUTBOUND_PACKET, ControlMetricType.OUTBOUND_PACKET); |
| 43 | MESSAGE_TYPE.put(Type.FLOW_MOD_PACKET, ControlMetricType.FLOW_MOD_PACKET); |
| 44 | MESSAGE_TYPE.put(Type.FLOW_REMOVED_PACKET, ControlMetricType.FLOW_REMOVED_PACKET); |
| 45 | MESSAGE_TYPE.put(Type.REQUEST_PACKET, ControlMetricType.REQUEST_PACKET); |
| 46 | MESSAGE_TYPE.put(Type.REPLY_PACKET, ControlMetricType.REPLY_PACKET); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Looks up the specified input value to the corresponding value with the specified map. |
| 51 | * |
| 52 | * @param map bidirectional mapping |
| 53 | * @param input input type |
| 54 | * @param cls class of output value |
| 55 | * @param <I> type of input value |
| 56 | * @param <O> type of output value |
| 57 | * @return the corresponding value stored in the specified map |
| 58 | */ |
| 59 | private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) { |
| 60 | if (!map.containsKey(input)) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 61 | throw new IllegalArgumentException( |
Jian Li | 01a6167 | 2016-02-11 14:59:43 -0800 | [diff] [blame] | 62 | String.format("No mapping found for %s when converting to %s", |
| 63 | input, cls.getName())); |
| 64 | } |
| 65 | return map.get(input); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Looks up the corresponding {@link ControlMetricType} instance |
| 70 | * from the ControlMessage.Type for control message type. |
| 71 | * |
| 72 | * @param type control message type |
| 73 | * @return control metric type |
| 74 | */ |
| 75 | public static ControlMetricType lookupControlMetricType(ControlMessage.Type type) { |
| 76 | return lookup(MESSAGE_TYPE, type, ControlMetricType.class); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Looks up the corresponding {@link ControlMessage.Type} instance from |
| 81 | * the specified control metric type. |
| 82 | * |
| 83 | * @param type control metric type |
| 84 | * @return control message type |
| 85 | */ |
| 86 | public static ControlMessage.Type lookupControlMessageType(ControlMetricType type) { |
| 87 | return lookup(MESSAGE_TYPE.inverse(), type, ControlMessage.Type.class); |
| 88 | } |
| 89 | } |