HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 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.net.optical.device; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | import org.onlab.util.Frequency; |
| 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.SparseAnnotations; |
| 28 | import org.onosproject.net.DefaultAnnotations.Builder; |
| 29 | import org.onosproject.net.device.DefaultPortDescription; |
| 30 | import org.onosproject.net.device.PortDescription; |
| 31 | import org.onosproject.net.optical.OmsPort; |
Jimmy Yan | d20e44f | 2016-09-09 13:32:46 -0700 | [diff] [blame] | 32 | import org.onosproject.net.optical.OpticalAnnotations; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 33 | import org.onosproject.net.optical.impl.DefaultOmsPort; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import com.google.common.annotations.Beta; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 37 | import com.google.common.collect.ImmutableSet; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * OMS port related helpers. |
| 41 | */ |
| 42 | @Beta |
| 43 | public final class OmsPortHelper { |
| 44 | |
| 45 | private static final Logger log = getLogger(OmsPortHelper.class); |
| 46 | |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 47 | /** |
| 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 56 | * @return port description |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 57 | */ |
| 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 Yan | d20e44f | 2016-09-09 13:32:46 -0700 | [diff] [blame] | 68 | 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 Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 71 | |
| 72 | long portSpeed = 0; |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 73 | return DefaultPortDescription.builder() |
| 74 | .withPortNumber(number) |
| 75 | .isEnabled(isEnabled) |
| 76 | .type(Port.Type.OMS) |
| 77 | .portSpeed(portSpeed) |
| 78 | .annotations(builder.build()) |
| 79 | .build(); |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 80 | } |
| 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 90 | * @return port description |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 91 | */ |
| 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 108 | * @return port description |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 109 | */ |
| 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 Yan | d20e44f | 2016-09-09 13:32:46 -0700 | [diff] [blame] | 129 | 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 Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 132 | |
| 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 HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 142 | /** |
| 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 Yan | d20e44f | 2016-09-09 13:32:46 -0700 | [diff] [blame] | 149 | return new FilteredAnnotation(input, ImmutableSet.of( |
| 150 | OpticalAnnotations.MIN_FREQ_HZ, |
| 151 | OpticalAnnotations.MAX_FREQ_HZ, |
| 152 | OpticalAnnotations.GRID_HZ)); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 153 | } |
| 154 | |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 155 | // not meant to be instantiated |
| 156 | private OmsPortHelper() {} |
| 157 | } |