2f0831c64933c360ce769f19dadd0fbdec600ac6
[onosfw.git] /
1 /*
2  * Copyright 2015 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.provider.of.flow.impl;
17
18 import com.google.common.collect.BiMap;
19 import com.google.common.collect.EnumHashBiMap;
20 import org.onosproject.net.ChannelSpacing;
21 import org.onosproject.net.GridType;
22 import org.onosproject.net.OchSignalType;
23
24 /**
25  * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
26  */
27 final class OpenFlowValueMapper {
28
29     // prohibit instantiation
30     private OpenFlowValueMapper() {}
31
32     private static final BiMap<GridType, Byte> GRID_TYPES = EnumHashBiMap.create(GridType.class);
33     static {
34         // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
35         GRID_TYPES.put(GridType.DWDM, (byte) 1); // OFPGRIDT_DWDM of enum ofp_grid_type
36         GRID_TYPES.put(GridType.CWDM, (byte) 2); // OFPGRIDT_CWDM of enum ofp_grid_type
37         GRID_TYPES.put(GridType.FLEX, (byte) 3); // OFPGRIDT_FLEX of enum ofp_grid_type
38     }
39
40     private static final BiMap<ChannelSpacing, Byte> CHANNEL_SPACING = EnumHashBiMap.create(ChannelSpacing.class);
41     static {
42         // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
43         CHANNEL_SPACING.put(ChannelSpacing.CHL_100GHZ, (byte) 1);   // OFPCS_100GHZ of enum ofp_chl_spacing
44         CHANNEL_SPACING.put(ChannelSpacing.CHL_50GHZ, (byte) 2);    // OFPCS_50GHZ of enum ofp_chl_spacing
45         CHANNEL_SPACING.put(ChannelSpacing.CHL_25GHZ, (byte) 3);    // OFPCS_25GHZ of enum ofp_chl_spacing
46         CHANNEL_SPACING.put(ChannelSpacing.CHL_12P5GHZ, (byte) 4);  // OFPCS_12P5GHZ of enum ofp_chl_spacing
47         CHANNEL_SPACING.put(ChannelSpacing.CHL_6P25GHZ, (byte) 5);  // OFPCS_6P25GHZ of enum ofp_chl_spacing
48     }
49
50     private static final BiMap<OchSignalType, Byte> OCH_SIGNAL_TYPES = EnumHashBiMap.create(OchSignalType.class);
51     static {
52         // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
53         OCH_SIGNAL_TYPES.put(OchSignalType.FIXED_GRID, (byte) 1); // OFPOCHT_FIX_GRID of enum ofp_och_signal_type
54         OCH_SIGNAL_TYPES.put(OchSignalType.FLEX_GRID, (byte) 2);  // OFPOCHT_FLEX_GRID of enum ofp_och_signal_type
55     }
56
57     /**
58      * Looks up the specified input value to the corresponding value with the specified map.
59      *
60      * @param map bidirectional mapping
61      * @param input input value
62      * @param cls class of output value
63      * @param <I> type of input value
64      * @param <O> type of output value
65      * @return the corresponding value stored in the specified map
66      * @throws NoMappingFoundException if no corresponding value is found
67      */
68     private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
69         if (!map.containsKey(input)) {
70             throw new NoMappingFoundException(input, cls);
71         }
72
73         return map.get(input);
74     }
75
76     /**
77      * Looks up the corresponding byte value defined in
78      * ONF "Optical Transport Protocol Extensions Version 1.0"
79      * from the specified {@link GridType} instance.
80      *
81      * @param type grid type
82      * @return the byte value corresponding to the specified grid type
83      * @throws NoMappingFoundException if the specified grid type is not found
84      */
85     static byte lookupGridType(GridType type) {
86         return lookup(GRID_TYPES, type, Byte.class);
87     }
88
89     /**
90      * Looks up the corresponding {@link GridType} instance
91      * from the specified byte value for grid type
92      * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
93      *
94      * @param type byte value as grid type defined the spec
95      * @return the corresponding GridType instance
96      */
97     static GridType lookupGridType(byte type) {
98         return lookup(GRID_TYPES.inverse(), type, GridType.class);
99     }
100
101     /**
102      * Looks up the corresponding byte value for channel spacing defined in
103      * ONF "Optical Transport Protocol Extensions Version 1.0"
104      * from the specified {@link ChannelSpacing} instance.
105      *
106      * @param spacing channel spacing
107      * @return byte value corresponding to the specified channel spacing
108      * @throws NoMappingFoundException if the specified channel spacing is not found
109      */
110     static byte lookupChannelSpacing(ChannelSpacing spacing) {
111         return lookup(CHANNEL_SPACING, spacing, Byte.class);
112     }
113
114     /**
115      * Looks up the corresponding {@link ChannelSpacing} instance
116      * from the specified byte value for channel spacing
117      * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
118      *
119      * @param spacing byte value as channel spacing defined the spec
120      * @return the corresponding ChannelSpacing instance
121      * @throws NoMappingFoundException if the specified channel spacing is not found
122      */
123     static ChannelSpacing lookupChannelSpacing(byte spacing) {
124         return lookup(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class);
125     }
126
127     /**
128      * Looks up the corresponding byte value for Och signal type defined in
129      * ONF "Optical Transport Protocol Extensions Version 1.0"
130      * from the specified {@link OchSignalType} instance.
131      *
132      * @param signalType optical signal type
133      * @return byte value corresponding to the specified OCh signal type
134      * @throws NoMappingFoundException if the specified Och signal type is not found
135      */
136     static byte lookupOchSignalType(OchSignalType signalType) {
137         return lookup(OCH_SIGNAL_TYPES, signalType, Byte.class);
138     }
139
140     /**
141      * Looks up the the corresponding {@link OchSignalType} instance
142      * from the specified byte value for Och signal type defined in
143      * ONF "Optical Transport Protocol Extensions Version 1.0".
144      *
145      * @param signalType byte value as Och singal type defined the spec
146      * @return the corresponding OchSignalType instance
147      * @throws NoMappingFoundException if the specified Och signal type is not found
148      */
149     static OchSignalType lookupOchSignalType(byte signalType) {
150         return lookup(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class);
151     }
152 }