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