blob: 2d83aa25a848813b01e19e48bde0d9fd68b2157b [file] [log] [blame]
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001/*
2 * Copyright 2016-present 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.net.optical.device;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Optional;
21
22import org.onlab.util.Frequency;
23import org.onosproject.net.Annotations;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.SparseAnnotations;
28import org.onosproject.net.DefaultAnnotations.Builder;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.device.PortDescription;
31import org.onosproject.net.optical.OmsPort;
32import org.onosproject.net.optical.impl.DefaultOmsPort;
33import org.slf4j.Logger;
34
35import com.google.common.annotations.Beta;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070036import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070037
38/**
39 * OMS port related helpers.
40 */
41@Beta
42public final class OmsPortHelper {
43
44 private static final Logger log = getLogger(OmsPortHelper.class);
45
46 // Annotation keys
47 /**
48 * minFrequency in Hz.
49 */
50 private static final String MIN_FREQ_HZ = "minFrequency";
51 /**
52 * maxFrequency in Hz.
53 */
54 private static final String MAX_FREQ_HZ = "maxFrequency";
55 /**
56 * grid in Hz.
57 */
58 private static final String GRID_HZ = "grid";
59
60 /**
61 * Creates OMS port description based on the supplied information.
62 *
63 * @param number port number
64 * @param isEnabled port enabled state
65 * @param minFrequency minimum frequency
66 * @param maxFrequency maximum frequency
67 * @param grid grid spacing frequency
68 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -070069 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070070 */
71 public static PortDescription omsPortDescription(PortNumber number,
72 boolean isEnabled,
73 Frequency minFrequency,
74 Frequency maxFrequency,
75 Frequency grid,
76 SparseAnnotations annotations) {
77
78 Builder builder = DefaultAnnotations.builder();
79 builder.putAll(annotations);
80
81 builder.set(MIN_FREQ_HZ, String.valueOf(minFrequency.asHz()));
82 builder.set(MAX_FREQ_HZ, String.valueOf(maxFrequency.asHz()));
83 builder.set(GRID_HZ, String.valueOf(grid.asHz()));
84
85 long portSpeed = 0;
86 return new DefaultPortDescription(number, isEnabled, Port.Type.OMS, portSpeed, builder.build());
87 }
88
89 /**
90 * Creates OMS port description based on the supplied information.
91 *
92 * @param number port number
93 * @param isEnabled port enabled state
94 * @param minFrequency minimum frequency
95 * @param maxFrequency maximum frequency
96 * @param grid grid spacing frequency
Ray Milkeybb23e0b2016-08-02 17:00:21 -070097 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070098 */
99 public static PortDescription omsPortDescription(PortNumber number,
100 boolean isEnabled,
101 Frequency minFrequency,
102 Frequency maxFrequency,
103 Frequency grid) {
104 return omsPortDescription(number, isEnabled, minFrequency, maxFrequency, grid, DefaultAnnotations.EMPTY);
105 }
106
107 /**
108 * Creates OMS port description based on the supplied information.
109 *
110 * @param base PortDescription to get basic information from
111 * @param minFrequency minimum frequency
112 * @param maxFrequency maximum frequency
113 * @param grid grid spacing frequency
114 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700115 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700116 */
117 public static PortDescription omsPortDescription(PortDescription base,
118 Frequency minFrequency,
119 Frequency maxFrequency,
120 Frequency grid,
121 SparseAnnotations annotations) {
122
123 return omsPortDescription(base.portNumber(), base.isEnabled(),
124 minFrequency, maxFrequency, grid,
125 annotations);
126 }
127
128 public static Optional<OmsPort> asOmsPort(Port port) {
129 if (port instanceof OmsPort) {
130 return Optional.of((OmsPort) port);
131 }
132
133 try {
134 Annotations an = port.annotations();
135
136 Frequency minFrequency = Frequency.ofHz(Long.parseLong(an.value(MIN_FREQ_HZ)));
137 Frequency maxFrequency = Frequency.ofHz(Long.parseLong(an.value(MAX_FREQ_HZ)));
138 Frequency grid = Frequency.ofHz(Long.parseLong(an.value(GRID_HZ)));
139
140 return Optional.of(new DefaultOmsPort(port, minFrequency, maxFrequency, grid));
141
142 } catch (NumberFormatException e) {
143
144 log.warn("{} was not well-formed OMS port.", port, e);
145 return Optional.empty();
146 }
147 }
148
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700149 /**
150 * Returns {@link Annotations} not used by the port type projection.
151 *
152 * @param input {@link Annotations}
153 * @return filtered view of given {@link Annotations}
154 */
155 public static Annotations stripHandledAnnotations(Annotations input) {
156 return new FilteredAnnotation(input, ImmutableSet.of(MIN_FREQ_HZ, MAX_FREQ_HZ, GRID_HZ));
157 }
158
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700159 // not meant to be instantiated
160 private OmsPortHelper() {}
161}