Jian Li
Committed by Gerrit Code Review

Define a new control message to control metric type mapper

Change-Id: Id5b365753a3a5abc3a864c4350e427336ee69448
...@@ -21,7 +21,7 @@ package org.onosproject.cpman; ...@@ -21,7 +21,7 @@ package org.onosproject.cpman;
21 public interface ControlMessage { 21 public interface ControlMessage {
22 22
23 enum Type { 23 enum Type {
24 - INCOMING_PACKET, OUTGOING_PACKET, FLOW_MOD_PACKET, 24 + INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET,
25 FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET 25 FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET
26 } 26 }
27 27
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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)) {
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 +}
...@@ -39,8 +39,8 @@ public final class OpenFlowControlMessageMapper { ...@@ -39,8 +39,8 @@ public final class OpenFlowControlMessageMapper {
39 static { 39 static {
40 // key is OpenFlow specific OFType 40 // key is OpenFlow specific OFType
41 // value is protocol agnostic ControlMessage.Type 41 // value is protocol agnostic ControlMessage.Type
42 - MESSAGE_TYPE.put(PACKET_IN, INCOMING_PACKET); 42 + MESSAGE_TYPE.put(PACKET_IN, INBOUND_PACKET);
43 - MESSAGE_TYPE.put(PACKET_OUT, OUTGOING_PACKET); 43 + MESSAGE_TYPE.put(PACKET_OUT, OUTBOUND_PACKET);
44 MESSAGE_TYPE.put(FLOW_MOD, FLOW_MOD_PACKET); 44 MESSAGE_TYPE.put(FLOW_MOD, FLOW_MOD_PACKET);
45 MESSAGE_TYPE.put(FLOW_REMOVED, FLOW_REMOVED_PACKET); 45 MESSAGE_TYPE.put(FLOW_REMOVED, FLOW_REMOVED_PACKET);
46 MESSAGE_TYPE.put(STATS_REQUEST, REQUEST_PACKET); 46 MESSAGE_TYPE.put(STATS_REQUEST, REQUEST_PACKET);
......