blob: aee89dd076c797183a4c333cd39f32c7d8d78f73 [file] [log] [blame]
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -07003 *
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;
Jimmy Yand20e44f2016-09-09 13:32:46 -070032import org.onosproject.net.optical.OpticalAnnotations;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070033import org.onosproject.net.optical.impl.DefaultOmsPort;
34import org.slf4j.Logger;
35
36import com.google.common.annotations.Beta;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070037import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070038
39/**
40 * OMS port related helpers.
41 */
42@Beta
43public final class OmsPortHelper {
44
45 private static final Logger log = getLogger(OmsPortHelper.class);
46
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070047 /**
48 * Creates OMS port description based on the supplied information.
49 *
50 * @param number port number
51 * @param isEnabled port enabled state
52 * @param minFrequency minimum frequency
53 * @param maxFrequency maximum frequency
54 * @param grid grid spacing frequency
55 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -070056 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070057 */
58 public static PortDescription omsPortDescription(PortNumber number,
59 boolean isEnabled,
60 Frequency minFrequency,
61 Frequency maxFrequency,
62 Frequency grid,
63 SparseAnnotations annotations) {
64
65 Builder builder = DefaultAnnotations.builder();
66 builder.putAll(annotations);
67
Jimmy Yand20e44f2016-09-09 13:32:46 -070068 builder.set(OpticalAnnotations.MIN_FREQ_HZ, String.valueOf(minFrequency.asHz()));
69 builder.set(OpticalAnnotations.MAX_FREQ_HZ, String.valueOf(maxFrequency.asHz()));
70 builder.set(OpticalAnnotations.GRID_HZ, String.valueOf(grid.asHz()));
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070071
72 long portSpeed = 0;
73 return new DefaultPortDescription(number, isEnabled, Port.Type.OMS, portSpeed, builder.build());
74 }
75
76 /**
77 * Creates OMS port description based on the supplied information.
78 *
79 * @param number port number
80 * @param isEnabled port enabled state
81 * @param minFrequency minimum frequency
82 * @param maxFrequency maximum frequency
83 * @param grid grid spacing frequency
Ray Milkeybb23e0b2016-08-02 17:00:21 -070084 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070085 */
86 public static PortDescription omsPortDescription(PortNumber number,
87 boolean isEnabled,
88 Frequency minFrequency,
89 Frequency maxFrequency,
90 Frequency grid) {
91 return omsPortDescription(number, isEnabled, minFrequency, maxFrequency, grid, DefaultAnnotations.EMPTY);
92 }
93
94 /**
95 * Creates OMS port description based on the supplied information.
96 *
97 * @param base PortDescription to get basic information from
98 * @param minFrequency minimum frequency
99 * @param maxFrequency maximum frequency
100 * @param grid grid spacing frequency
101 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700102 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700103 */
104 public static PortDescription omsPortDescription(PortDescription base,
105 Frequency minFrequency,
106 Frequency maxFrequency,
107 Frequency grid,
108 SparseAnnotations annotations) {
109
110 return omsPortDescription(base.portNumber(), base.isEnabled(),
111 minFrequency, maxFrequency, grid,
112 annotations);
113 }
114
115 public static Optional<OmsPort> asOmsPort(Port port) {
116 if (port instanceof OmsPort) {
117 return Optional.of((OmsPort) port);
118 }
119
120 try {
121 Annotations an = port.annotations();
122
Jimmy Yand20e44f2016-09-09 13:32:46 -0700123 Frequency minFrequency = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.MIN_FREQ_HZ)));
124 Frequency maxFrequency = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.MAX_FREQ_HZ)));
125 Frequency grid = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.GRID_HZ)));
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700126
127 return Optional.of(new DefaultOmsPort(port, minFrequency, maxFrequency, grid));
128
129 } catch (NumberFormatException e) {
130
131 log.warn("{} was not well-formed OMS port.", port, e);
132 return Optional.empty();
133 }
134 }
135
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700136 /**
137 * Returns {@link Annotations} not used by the port type projection.
138 *
139 * @param input {@link Annotations}
140 * @return filtered view of given {@link Annotations}
141 */
142 public static Annotations stripHandledAnnotations(Annotations input) {
Jimmy Yand20e44f2016-09-09 13:32:46 -0700143 return new FilteredAnnotation(input, ImmutableSet.of(
144 OpticalAnnotations.MIN_FREQ_HZ,
145 OpticalAnnotations.MAX_FREQ_HZ,
146 OpticalAnnotations.GRID_HZ));
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700147 }
148
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700149 // not meant to be instantiated
150 private OmsPortHelper() {}
151}