HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 32 | import org.onosproject.net.optical.impl.DefaultOmsPort; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import com.google.common.annotations.Beta; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 36 | import com.google.common.collect.ImmutableSet; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * OMS port related helpers. |
| 40 | */ |
| 41 | @Beta |
| 42 | public 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 |
| 69 | */ |
| 70 | public static PortDescription omsPortDescription(PortNumber number, |
| 71 | boolean isEnabled, |
| 72 | Frequency minFrequency, |
| 73 | Frequency maxFrequency, |
| 74 | Frequency grid, |
| 75 | SparseAnnotations annotations) { |
| 76 | |
| 77 | Builder builder = DefaultAnnotations.builder(); |
| 78 | builder.putAll(annotations); |
| 79 | |
| 80 | builder.set(MIN_FREQ_HZ, String.valueOf(minFrequency.asHz())); |
| 81 | builder.set(MAX_FREQ_HZ, String.valueOf(maxFrequency.asHz())); |
| 82 | builder.set(GRID_HZ, String.valueOf(grid.asHz())); |
| 83 | |
| 84 | long portSpeed = 0; |
| 85 | return new DefaultPortDescription(number, isEnabled, Port.Type.OMS, portSpeed, builder.build()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Creates OMS port description based on the supplied information. |
| 90 | * |
| 91 | * @param number port number |
| 92 | * @param isEnabled port enabled state |
| 93 | * @param minFrequency minimum frequency |
| 94 | * @param maxFrequency maximum frequency |
| 95 | * @param grid grid spacing frequency |
| 96 | */ |
| 97 | public static PortDescription omsPortDescription(PortNumber number, |
| 98 | boolean isEnabled, |
| 99 | Frequency minFrequency, |
| 100 | Frequency maxFrequency, |
| 101 | Frequency grid) { |
| 102 | return omsPortDescription(number, isEnabled, minFrequency, maxFrequency, grid, DefaultAnnotations.EMPTY); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates OMS port description based on the supplied information. |
| 107 | * |
| 108 | * @param base PortDescription to get basic information from |
| 109 | * @param minFrequency minimum frequency |
| 110 | * @param maxFrequency maximum frequency |
| 111 | * @param grid grid spacing frequency |
| 112 | * @param annotations key/value annotations map |
| 113 | */ |
| 114 | public static PortDescription omsPortDescription(PortDescription base, |
| 115 | Frequency minFrequency, |
| 116 | Frequency maxFrequency, |
| 117 | Frequency grid, |
| 118 | SparseAnnotations annotations) { |
| 119 | |
| 120 | return omsPortDescription(base.portNumber(), base.isEnabled(), |
| 121 | minFrequency, maxFrequency, grid, |
| 122 | annotations); |
| 123 | } |
| 124 | |
| 125 | public static Optional<OmsPort> asOmsPort(Port port) { |
| 126 | if (port instanceof OmsPort) { |
| 127 | return Optional.of((OmsPort) port); |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | Annotations an = port.annotations(); |
| 132 | |
| 133 | Frequency minFrequency = Frequency.ofHz(Long.parseLong(an.value(MIN_FREQ_HZ))); |
| 134 | Frequency maxFrequency = Frequency.ofHz(Long.parseLong(an.value(MAX_FREQ_HZ))); |
| 135 | Frequency grid = Frequency.ofHz(Long.parseLong(an.value(GRID_HZ))); |
| 136 | |
| 137 | return Optional.of(new DefaultOmsPort(port, minFrequency, maxFrequency, grid)); |
| 138 | |
| 139 | } catch (NumberFormatException e) { |
| 140 | |
| 141 | log.warn("{} was not well-formed OMS port.", port, e); |
| 142 | return Optional.empty(); |
| 143 | } |
| 144 | } |
| 145 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Returns {@link Annotations} not used by the port type projection. |
| 148 | * |
| 149 | * @param input {@link Annotations} |
| 150 | * @return filtered view of given {@link Annotations} |
| 151 | */ |
| 152 | public static Annotations stripHandledAnnotations(Annotations input) { |
| 153 | return new FilteredAnnotation(input, ImmutableSet.of(MIN_FREQ_HZ, MAX_FREQ_HZ, GRID_HZ)); |
| 154 | } |
| 155 | |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 156 | // not meant to be instantiated |
| 157 | private OmsPortHelper() {} |
| 158 | } |