blob: 096bd300decfbf7098cd4e614780d8c3efd89ac1 [file] [log] [blame]
Jian Li1d13c262016-02-09 14:58:28 -08001/*
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 */
16package org.onosproject.provider.of.message.impl;
17
18import com.google.common.collect.BiMap;
19import com.google.common.collect.EnumHashBiMap;
20import org.onosproject.cpman.ControlMessage;
21import org.projectfloodlight.openflow.protocol.OFType;
22
23import static org.projectfloodlight.openflow.protocol.OFType.*;
24import static org.onosproject.cpman.ControlMessage.Type.*;
25
26/**
27 * Collection of helper methods to convert protocol agnostic control message to
28 * messages used in OpenFlow specification.
29 */
30public final class OpenFlowControlMessageMapper {
31
32 // prohibit instantiation
33 private OpenFlowControlMessageMapper() {
34 }
35
36 private static final BiMap<OFType, ControlMessage.Type> MESSAGE_TYPE =
37 EnumHashBiMap.create(OFType.class);
38
39 static {
40 // key is OpenFlow specific OFType
41 // value is protocol agnostic ControlMessage.Type
Jian Li01a61672016-02-11 14:59:43 -080042 MESSAGE_TYPE.put(PACKET_IN, INBOUND_PACKET);
43 MESSAGE_TYPE.put(PACKET_OUT, OUTBOUND_PACKET);
Jian Li1d13c262016-02-09 14:58:28 -080044 MESSAGE_TYPE.put(FLOW_MOD, FLOW_MOD_PACKET);
45 MESSAGE_TYPE.put(FLOW_REMOVED, FLOW_REMOVED_PACKET);
46 MESSAGE_TYPE.put(STATS_REQUEST, REQUEST_PACKET);
47 MESSAGE_TYPE.put(STATS_REPLY, REPLY_PACKET);
48 }
49
50 /**
51 * Looks up the specified input value to the corresponding value with the specified map.
52 *
53 * @param map bidirectional mapping
54 * @param input input type
55 * @param cls class of output value
56 * @param <I> type of input value
57 * @param <O> type of output value
58 * @return the corresponding value stored in the specified map
59 */
60 private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
61 if (!map.containsKey(input)) {
62 throw new RuntimeException(
63 String.format("No mapping found for %s when converting to %s",
64 input, cls.getName()));
65 }
66 return map.get(input);
67 }
68
69 /**
70 * Looks up the corresponding {@link ControlMessage.Type} instance
71 * from the specified OFType value for OpenFlow message type.
72 *
73 * @param type OpenFlow message type
74 * @return protocol agnostic control message type
75 */
76 public static ControlMessage.Type lookupControlMessageType(OFType type) {
77 return lookup(MESSAGE_TYPE, type, ControlMessage.Type.class);
78 }
79
80 /**
81 * Looks up the corresponding {@link OFType} instance from the specified
82 * ControlMetricType value.
83 *
84 * @param type control message type
85 * @return OpenFlow specific message type
86 */
87 public static OFType lookupOFType(ControlMessage.Type type) {
88 return lookup(MESSAGE_TYPE.inverse(), type, OFType.class);
89 }
90}