blob: f6bdcdca5d03c009db6c40efb967e2222b77ed3e [file] [log] [blame]
Jian Li01a61672016-02-11 14:59:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li01a61672016-02-11 14:59:43 -08003 *
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 */
16package org.onosproject.cpman.impl;
17
18import com.google.common.collect.BiMap;
19import com.google.common.collect.EnumHashBiMap;
20import org.onosproject.cpman.ControlMessage;
21import org.onosproject.cpman.ControlMetricType;
22
23import 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 */
29public 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)) {
61 throw new RuntimeException(
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}