blob: 968dd6b3556c64ca67c43e28706dbc6484e031df [file] [log] [blame]
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -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.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.Port;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.DefaultAnnotations.Builder;
28import org.onosproject.net.OtuSignalType;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.device.PortDescription;
31import org.onosproject.net.optical.OtuPort;
32import org.onosproject.net.optical.impl.DefaultOtuPort;
33import org.slf4j.Logger;
34
35import com.google.common.annotations.Beta;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070036import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070037
38/**
39 * OTU port related helpers.
40 */
41@Beta
42public 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
58 */
59 public static PortDescription otuPortDescription(PortNumber number,
60 boolean isEnabled,
61 OtuSignalType signalType) {
62 return otuPortDescription(number, isEnabled, signalType, DefaultAnnotations.EMPTY);
63 }
64
65 /**
66 * Creates OTU port description based on the supplied information.
67 *
68 * @param number port number
69 * @param isEnabled port enabled state
70 * @param signalType OTU client signal type
71 * @param annotations key/value annotations map
72 */
73 public static PortDescription otuPortDescription(PortNumber number,
74 boolean isEnabled,
75 OtuSignalType signalType,
76 SparseAnnotations annotations) {
77 Builder builder = DefaultAnnotations.builder();
78 builder.putAll(annotations);
79
80 builder.set(SIGNAL_TYPE, signalType.toString());
81
82 long portSpeed = 0; // TODO specify appropriate value?
83 return new DefaultPortDescription(number,
84 isEnabled,
85 Port.Type.OTU,
86 portSpeed,
87 builder.build());
88 }
89
90 /**
91 * Creates OTU port description based on the supplied information.
92 *
93 * @param base PortDescription to get basic information from
94 * @param signalType OTU client signal type
95 * @param annotations key/value annotations map
96 */
97 public static PortDescription otuPortDescription(PortDescription base,
98 OtuSignalType signalType,
99 SparseAnnotations annotations) {
100 return otuPortDescription(base.portNumber(), base.isEnabled(), signalType, annotations);
101 }
102
103 public static Optional<OtuPort> asOtuPort(Port port) {
104 if (port instanceof OtuPort) {
105 return Optional.of((OtuPort) port);
106 }
107
108 try {
109 Annotations an = port.annotations();
110
111 OtuSignalType signalType = Enum.valueOf(OtuSignalType.class,
112 an.value(SIGNAL_TYPE));
113
114
115 // Note: OTU specific annotations is not filtered-out here.
116 // DefaultOtuPort should filter them, if necessary.
117 return Optional.of(new DefaultOtuPort(port, signalType));
118
119 } catch (NullPointerException | IllegalArgumentException e) {
120
121 log.warn("{} was not well-formed Otu port.", port, e);
122 return Optional.empty();
123 }
124 }
125
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700126 /**
127 * Returns {@link Annotations} not used by the port type projection.
128 *
129 * @param input {@link Annotations}
130 * @return filtered view of given {@link Annotations}
131 */
132 public static Annotations stripHandledAnnotations(Annotations input) {
133 return new FilteredAnnotation(input, ImmutableSet.of(SIGNAL_TYPE));
134 }
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700135
136 // not meant to be instantiated
137 private OtuPortHelper() {}
138
139}