blob: 91fcac55fc015c53f07822b022f1a589edd99145 [file] [log] [blame]
Yafit Hadara9a73de2015-09-06 13:52:52 +03001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Yafit Hadara9a73de2015-09-06 13:52:52 +03003 *
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.device.impl;
17
18import org.onosproject.net.OduSignalType;
19
20import com.google.common.collect.BiMap;
21import com.google.common.collect.EnumHashBiMap;
22
23/**
24 * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
25 */
26final class OpenFlowDeviceValueMapper {
27
28 // prohibit instantiation
29 private OpenFlowDeviceValueMapper() {}
30
31 private static final BiMap<OduSignalType, Byte> ODU_SIGNAL_TYPES = EnumHashBiMap.create(OduSignalType.class);
32 static {
33 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
34 ODU_SIGNAL_TYPES.put(OduSignalType.ODU1, (byte) 1); // OFPODUT_ODU1 of enum ofp_odu_signal_type
35 ODU_SIGNAL_TYPES.put(OduSignalType.ODU2, (byte) 2); // OFPODUT_ODU2 of enum ofp_odu_signal_type
36 ODU_SIGNAL_TYPES.put(OduSignalType.ODU3, (byte) 3); // OFPODUT_ODU3 of enum ofp_odu_signal_type
37 ODU_SIGNAL_TYPES.put(OduSignalType.ODU4, (byte) 4); // OFPODUT_ODU4 of enum ofp_odu_signal_type
38 ODU_SIGNAL_TYPES.put(OduSignalType.ODU0, (byte) 10); // OFPODUT_ODU0 of enum ofp_odu_signal_type
39 ODU_SIGNAL_TYPES.put(OduSignalType.ODU2e, (byte) 11); // OFPODUT_ODU2E of enum ofp_odu_signal_type
40 }
41
42 /**
43 * Looks up the specified input value to the corresponding value with the specified map.
44 *
45 * @param map bidirectional mapping
46 * @param input input value
47 * @param cls class of output value
48 * @param <I> type of input value
49 * @param <O> type of output value
50 * @return the corresponding value stored in the specified map
51 */
52 private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
53 if (!map.containsKey(input)) {
54 throw new RuntimeException(
55 String.format("No mapping found for %s when converting to %s", input, cls.getName()));
56 }
57
58 return map.get(input);
59 }
60
61 /**
62 * Looks up the the corresponding {@link OduSignalType} instance
63 * from the specified byte value for ODU signal type defined in
64 * ONF "Optical Transport Protocol Extensions Version 1.0".
65 *
66 * @param signalType byte value as ODU (Optical channel Data Unit) signal type defined the spec
67 * @return the corresponding OchSignalType instance
68 */
69 static OduSignalType lookupOduSignalType(byte signalType) {
70 return lookup(ODU_SIGNAL_TYPES.inverse(), signalType, OduSignalType.class);
71 }
72
73}