HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -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 | 4c0ef6b | 2016-05-02 19:45:41 -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.onosproject.net.Annotations; |
| 23 | import org.onosproject.net.CltSignalType; |
| 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.OduCltPort; |
| 32 | import org.onosproject.net.optical.impl.DefaultOduCltPort; |
| 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 | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * ODU client port related helpers. |
| 40 | */ |
| 41 | @Beta |
| 42 | public final class OduCltPortHelper { |
| 43 | |
| 44 | private static final Logger log = getLogger(OduCltPortHelper.class); |
| 45 | |
| 46 | // Annotation keys |
| 47 | /** |
| 48 | * {@link CltSignalType} as String. |
| 49 | */ |
| 50 | private static final String SIGNAL_TYPE = "signalType"; |
| 51 | |
| 52 | /** |
| 53 | * Creates ODU client port description based on the supplied information. |
| 54 | * |
| 55 | * @param number port number |
| 56 | * @param isEnabled port enabled state |
| 57 | * @param signalType ODU client signal type |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 58 | * @return port description |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 59 | */ |
| 60 | public static PortDescription oduCltPortDescription(PortNumber number, |
| 61 | boolean isEnabled, |
| 62 | CltSignalType signalType) { |
| 63 | return oduCltPortDescription(number, isEnabled, signalType, DefaultAnnotations.EMPTY); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates ODU client port description based on the supplied information. |
| 68 | * |
| 69 | * @param number port number |
| 70 | * @param isEnabled port enabled state |
| 71 | * @param signalType ODU client signal type |
| 72 | * @param annotations key/value annotations map |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 73 | * @return port description |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 74 | */ |
| 75 | public static PortDescription oduCltPortDescription(PortNumber number, |
| 76 | boolean isEnabled, |
| 77 | CltSignalType signalType, |
| 78 | SparseAnnotations annotations) { |
| 79 | Builder builder = DefaultAnnotations.builder(); |
| 80 | builder.putAll(annotations); |
| 81 | |
| 82 | builder.set(SIGNAL_TYPE, signalType.toString()); |
| 83 | |
| 84 | long portSpeed = signalType.bitRate(); |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 85 | return DefaultPortDescription.builder() |
| 86 | .withPortNumber(number) |
| 87 | .isEnabled(isEnabled) |
| 88 | .type(Port.Type.ODUCLT) |
| 89 | .portSpeed(portSpeed) |
| 90 | .annotations(builder.build()) |
| 91 | .build(); |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates ODU client port description based on the supplied information. |
| 96 | * |
| 97 | * @param base PortDescription to get basic information from |
| 98 | * @param signalType ODU client signal type |
| 99 | * @param annotations key/value annotations map |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 100 | * @return port description |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 101 | */ |
| 102 | public static PortDescription oduCltPortDescription(PortDescription base, |
| 103 | CltSignalType signalType, |
| 104 | SparseAnnotations annotations) { |
| 105 | return oduCltPortDescription(base.portNumber(), base.isEnabled(), signalType, annotations); |
| 106 | } |
| 107 | |
| 108 | public static Optional<OduCltPort> asOduCltPort(Port port) { |
| 109 | if (port instanceof OduCltPort) { |
| 110 | return Optional.of((OduCltPort) port); |
| 111 | } |
| 112 | |
| 113 | try { |
| 114 | Annotations an = port.annotations(); |
| 115 | |
| 116 | CltSignalType signalType = Enum.valueOf(CltSignalType.class, |
| 117 | an.value(SIGNAL_TYPE)); |
| 118 | |
| 119 | |
| 120 | // Note: ODU specific annotations is not filtered-out here. |
| 121 | // DefaultOduCltPort should filter them, if necessary. |
| 122 | return Optional.of(new DefaultOduCltPort(port, signalType)); |
| 123 | |
| 124 | } catch (NullPointerException | IllegalArgumentException e) { |
| 125 | |
| 126 | log.warn("{} was not well-formed OduClt port.", port, e); |
| 127 | return Optional.empty(); |
| 128 | } |
| 129 | } |
| 130 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Returns {@link Annotations} not used by the port type projection. |
| 133 | * |
| 134 | * @param input {@link Annotations} |
| 135 | * @return filtered view of given {@link Annotations} |
| 136 | */ |
| 137 | public static Annotations stripHandledAnnotations(Annotations input) { |
| 138 | return new FilteredAnnotation(input, ImmutableSet.of(SIGNAL_TYPE)); |
| 139 | } |
| 140 | |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 141 | // not meant to be instantiated |
| 142 | private OduCltPortHelper() {} |
| 143 | } |