blob: 2c99c535107127ffc823f8d973bbc8a3feb2619e [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;
36
37/**
38 * OMS port related helpers.
39 */
40@Beta
41public final class OmsPortHelper {
42
43 private static final Logger log = getLogger(OmsPortHelper.class);
44
45 // Annotation keys
46 /**
47 * minFrequency in Hz.
48 */
49 private static final String MIN_FREQ_HZ = "minFrequency";
50 /**
51 * maxFrequency in Hz.
52 */
53 private static final String MAX_FREQ_HZ = "maxFrequency";
54 /**
55 * grid in Hz.
56 */
57 private static final String GRID_HZ = "grid";
58
59 /**
60 * Creates OMS port description based on the supplied information.
61 *
62 * @param number port number
63 * @param isEnabled port enabled state
64 * @param minFrequency minimum frequency
65 * @param maxFrequency maximum frequency
66 * @param grid grid spacing frequency
67 * @param annotations key/value annotations map
68 */
69 public static PortDescription omsPortDescription(PortNumber number,
70 boolean isEnabled,
71 Frequency minFrequency,
72 Frequency maxFrequency,
73 Frequency grid,
74 SparseAnnotations annotations) {
75
76 Builder builder = DefaultAnnotations.builder();
77 builder.putAll(annotations);
78
79 builder.set(MIN_FREQ_HZ, String.valueOf(minFrequency.asHz()));
80 builder.set(MAX_FREQ_HZ, String.valueOf(maxFrequency.asHz()));
81 builder.set(GRID_HZ, String.valueOf(grid.asHz()));
82
83 long portSpeed = 0;
84 return new DefaultPortDescription(number, isEnabled, Port.Type.OMS, portSpeed, builder.build());
85 }
86
87 /**
88 * Creates OMS port description based on the supplied information.
89 *
90 * @param number port number
91 * @param isEnabled port enabled state
92 * @param minFrequency minimum frequency
93 * @param maxFrequency maximum frequency
94 * @param grid grid spacing frequency
95 */
96 public static PortDescription omsPortDescription(PortNumber number,
97 boolean isEnabled,
98 Frequency minFrequency,
99 Frequency maxFrequency,
100 Frequency grid) {
101 return omsPortDescription(number, isEnabled, minFrequency, maxFrequency, grid, DefaultAnnotations.EMPTY);
102 }
103
104 /**
105 * Creates OMS port description based on the supplied information.
106 *
107 * @param base PortDescription to get basic information from
108 * @param minFrequency minimum frequency
109 * @param maxFrequency maximum frequency
110 * @param grid grid spacing frequency
111 * @param annotations key/value annotations map
112 */
113 public static PortDescription omsPortDescription(PortDescription base,
114 Frequency minFrequency,
115 Frequency maxFrequency,
116 Frequency grid,
117 SparseAnnotations annotations) {
118
119 return omsPortDescription(base.portNumber(), base.isEnabled(),
120 minFrequency, maxFrequency, grid,
121 annotations);
122 }
123
124 public static Optional<OmsPort> asOmsPort(Port port) {
125 if (port instanceof OmsPort) {
126 return Optional.of((OmsPort) port);
127 }
128
129 try {
130 Annotations an = port.annotations();
131
132 Frequency minFrequency = Frequency.ofHz(Long.parseLong(an.value(MIN_FREQ_HZ)));
133 Frequency maxFrequency = Frequency.ofHz(Long.parseLong(an.value(MAX_FREQ_HZ)));
134 Frequency grid = Frequency.ofHz(Long.parseLong(an.value(GRID_HZ)));
135
136 return Optional.of(new DefaultOmsPort(port, minFrequency, maxFrequency, grid));
137
138 } catch (NumberFormatException e) {
139
140 log.warn("{} was not well-formed OMS port.", port, e);
141 return Optional.empty();
142 }
143 }
144
145 // not meant to be instantiated
146 private OmsPortHelper() {}
147}