Define a new control message to control metric type mapper

Change-Id: Id5b365753a3a5abc3a864c4350e427336ee69448
diff --git a/apps/cpman/api/src/main/java/org/onosproject/cpman/ControlMessage.java b/apps/cpman/api/src/main/java/org/onosproject/cpman/ControlMessage.java
index ffcb9dd..8b6112e 100644
--- a/apps/cpman/api/src/main/java/org/onosproject/cpman/ControlMessage.java
+++ b/apps/cpman/api/src/main/java/org/onosproject/cpman/ControlMessage.java
@@ -21,7 +21,7 @@
 public interface ControlMessage {
 
     enum Type {
-        INCOMING_PACKET, OUTGOING_PACKET, FLOW_MOD_PACKET,
+        INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET,
         FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET
     }
 
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlMessageMetricMapper.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlMessageMetricMapper.java
new file mode 100644
index 0000000..4a7dbd6
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/impl/ControlMessageMetricMapper.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.cpman.impl;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.EnumHashBiMap;
+import org.onosproject.cpman.ControlMessage;
+import org.onosproject.cpman.ControlMetricType;
+
+import static org.onosproject.cpman.ControlMessage.Type;
+
+/**
+ * Collection of helper methods to convert protocol agnostic control message
+ * type to control metric type.
+ */
+public final class ControlMessageMetricMapper {
+
+    // prohibit instantiation
+    private ControlMessageMetricMapper() {
+    }
+
+    private static final BiMap<ControlMessage.Type, ControlMetricType> MESSAGE_TYPE =
+            EnumHashBiMap.create(ControlMessage.Type.class);
+
+    static {
+        // key is protocol agnostic ControlMessage.Type
+        // value is ControlMetricType
+        MESSAGE_TYPE.put(Type.INBOUND_PACKET, ControlMetricType.INBOUND_PACKET);
+        MESSAGE_TYPE.put(Type.OUTBOUND_PACKET, ControlMetricType.OUTBOUND_PACKET);
+        MESSAGE_TYPE.put(Type.FLOW_MOD_PACKET, ControlMetricType.FLOW_MOD_PACKET);
+        MESSAGE_TYPE.put(Type.FLOW_REMOVED_PACKET, ControlMetricType.FLOW_REMOVED_PACKET);
+        MESSAGE_TYPE.put(Type.REQUEST_PACKET, ControlMetricType.REQUEST_PACKET);
+        MESSAGE_TYPE.put(Type.REPLY_PACKET, ControlMetricType.REPLY_PACKET);
+    }
+
+    /**
+     * Looks up the specified input value to the corresponding value with the specified map.
+     *
+     * @param map   bidirectional mapping
+     * @param input input type
+     * @param cls   class of output value
+     * @param <I>   type of input value
+     * @param <O>   type of output value
+     * @return the corresponding value stored in the specified map
+     */
+    private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
+        if (!map.containsKey(input)) {
+            throw new RuntimeException(
+                    String.format("No mapping found for %s when converting to %s",
+                            input, cls.getName()));
+        }
+        return map.get(input);
+    }
+
+    /**
+     * Looks up the corresponding {@link ControlMetricType} instance
+     * from the ControlMessage.Type for control message type.
+     *
+     * @param type control message type
+     * @return control metric type
+     */
+    public static ControlMetricType lookupControlMetricType(ControlMessage.Type type) {
+        return lookup(MESSAGE_TYPE, type, ControlMetricType.class);
+    }
+
+    /**
+     * Looks up the corresponding {@link ControlMessage.Type} instance from
+     * the specified control metric type.
+     *
+     * @param type control metric type
+     * @return control message type
+     */
+    public static ControlMessage.Type lookupControlMessageType(ControlMetricType type) {
+        return lookup(MESSAGE_TYPE.inverse(), type, ControlMessage.Type.class);
+    }
+}
diff --git a/providers/openflow/message/src/main/java/org/onosproject/provider/of/message/impl/OpenFlowControlMessageMapper.java b/providers/openflow/message/src/main/java/org/onosproject/provider/of/message/impl/OpenFlowControlMessageMapper.java
index 7347264..096bd30 100644
--- a/providers/openflow/message/src/main/java/org/onosproject/provider/of/message/impl/OpenFlowControlMessageMapper.java
+++ b/providers/openflow/message/src/main/java/org/onosproject/provider/of/message/impl/OpenFlowControlMessageMapper.java
@@ -39,8 +39,8 @@
     static {
         // key is OpenFlow specific OFType
         // value is protocol agnostic ControlMessage.Type
-        MESSAGE_TYPE.put(PACKET_IN, INCOMING_PACKET);
-        MESSAGE_TYPE.put(PACKET_OUT, OUTGOING_PACKET);
+        MESSAGE_TYPE.put(PACKET_IN, INBOUND_PACKET);
+        MESSAGE_TYPE.put(PACKET_OUT, OUTBOUND_PACKET);
         MESSAGE_TYPE.put(FLOW_MOD, FLOW_MOD_PACKET);
         MESSAGE_TYPE.put(FLOW_REMOVED, FLOW_REMOVED_PACKET);
         MESSAGE_TYPE.put(STATS_REQUEST, REQUEST_PACKET);