blob: 7b9dcd8873481e35da4c79a6530dcae2bbc9ecbe [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;
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080073 return DefaultPortDescription.builder()
74 .withPortNumber(number)
75 .isEnabled(isEnabled)
76 .type(Port.Type.OMS)
77 .portSpeed(portSpeed)
78 .annotations(builder.build())
79 .build();
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070080 }
81
82 /**
83 * Creates OMS port description based on the supplied information.
84 *
85 * @param number port number
86 * @param isEnabled port enabled state
87 * @param minFrequency minimum frequency
88 * @param maxFrequency maximum frequency
89 * @param grid grid spacing frequency
Ray Milkeybb23e0b2016-08-02 17:00:21 -070090 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070091 */
92 public static PortDescription omsPortDescription(PortNumber number,
93 boolean isEnabled,
94 Frequency minFrequency,
95 Frequency maxFrequency,
96 Frequency grid) {
97 return omsPortDescription(number, isEnabled, minFrequency, maxFrequency, grid, DefaultAnnotations.EMPTY);
98 }
99
100 /**
101 * Creates OMS port description based on the supplied information.
102 *
103 * @param base PortDescription to get basic information from
104 * @param minFrequency minimum frequency
105 * @param maxFrequency maximum frequency
106 * @param grid grid spacing frequency
107 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700108 * @return port description
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700109 */
110 public static PortDescription omsPortDescription(PortDescription base,
111 Frequency minFrequency,
112 Frequency maxFrequency,
113 Frequency grid,
114 SparseAnnotations annotations) {
115
116 return omsPortDescription(base.portNumber(), base.isEnabled(),
117 minFrequency, maxFrequency, grid,
118 annotations);
119 }
120
121 public static Optional<OmsPort> asOmsPort(Port port) {
122 if (port instanceof OmsPort) {
123 return Optional.of((OmsPort) port);
124 }
125
126 try {
127 Annotations an = port.annotations();
128
Jimmy Yand20e44f2016-09-09 13:32:46 -0700129 Frequency minFrequency = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.MIN_FREQ_HZ)));
130 Frequency maxFrequency = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.MAX_FREQ_HZ)));
131 Frequency grid = Frequency.ofHz(Long.parseLong(an.value(OpticalAnnotations.GRID_HZ)));
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700132
133 return Optional.of(new DefaultOmsPort(port, minFrequency, maxFrequency, grid));
134
135 } catch (NumberFormatException e) {
136
137 log.warn("{} was not well-formed OMS port.", port, e);
138 return Optional.empty();
139 }
140 }
141
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700142 /**
143 * Returns {@link Annotations} not used by the port type projection.
144 *
145 * @param input {@link Annotations}
146 * @return filtered view of given {@link Annotations}
147 */
148 public static Annotations stripHandledAnnotations(Annotations input) {
Jimmy Yand20e44f2016-09-09 13:32:46 -0700149 return new FilteredAnnotation(input, ImmutableSet.of(
150 OpticalAnnotations.MIN_FREQ_HZ,
151 OpticalAnnotations.MAX_FREQ_HZ,
152 OpticalAnnotations.GRID_HZ));
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700153 }
154
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700155 // not meant to be instantiated
156 private OmsPortHelper() {}
157}