blob: b3d29052f9df319413fbda3946a8b1eda0ca6609 [file] [log] [blame]
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -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.CltSignalType;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.SparseAnnotations;
28import org.onosproject.net.DefaultAnnotations.Builder;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.device.PortDescription;
31import org.onosproject.net.optical.OduCltPort;
32import org.onosproject.net.optical.impl.DefaultOduCltPort;
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 Yuta4c0ef6b2016-05-02 19:45:41 -070037
38/**
39 * ODU client port related helpers.
40 */
41@Beta
42public 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 Milkeybb23e0b2016-08-02 17:00:21 -070058 * @return port description
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070059 */
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 Milkeybb23e0b2016-08-02 17:00:21 -070073 * @return port description
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070074 */
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();
85 return new DefaultPortDescription(number,
86 isEnabled,
87 Port.Type.ODUCLT,
88 portSpeed,
89 builder.build());
90 }
91
92 /**
93 * Creates ODU client port description based on the supplied information.
94 *
95 * @param base PortDescription to get basic information from
96 * @param signalType ODU client signal type
97 * @param annotations key/value annotations map
Ray Milkeybb23e0b2016-08-02 17:00:21 -070098 * @return port description
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070099 */
100 public static PortDescription oduCltPortDescription(PortDescription base,
101 CltSignalType signalType,
102 SparseAnnotations annotations) {
103 return oduCltPortDescription(base.portNumber(), base.isEnabled(), signalType, annotations);
104 }
105
106 public static Optional<OduCltPort> asOduCltPort(Port port) {
107 if (port instanceof OduCltPort) {
108 return Optional.of((OduCltPort) port);
109 }
110
111 try {
112 Annotations an = port.annotations();
113
114 CltSignalType signalType = Enum.valueOf(CltSignalType.class,
115 an.value(SIGNAL_TYPE));
116
117
118 // Note: ODU specific annotations is not filtered-out here.
119 // DefaultOduCltPort should filter them, if necessary.
120 return Optional.of(new DefaultOduCltPort(port, signalType));
121
122 } catch (NullPointerException | IllegalArgumentException e) {
123
124 log.warn("{} was not well-formed OduClt port.", port, e);
125 return Optional.empty();
126 }
127 }
128
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700129 /**
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 }
138
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700139 // not meant to be instantiated
140 private OduCltPortHelper() {}
141}