blob: 9dd48cbf896e2379dd0cd69d24766b73b640dac4 [file] [log] [blame]
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -07003 *
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();
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080085 return DefaultPortDescription.builder()
86 .withPortNumber(number)
87 .isEnabled(isEnabled)
88 .type(Port.Type.ODUCLT)
89 .portSpeed(portSpeed)
90 .annotations(builder.build())
91 .build();
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070092 }
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 Milkeybb23e0b2016-08-02 17:00:21 -0700100 * @return port description
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700101 */
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 HIGUCHI1d547bf2016-08-02 21:44:48 -0700131 /**
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 Yuta4c0ef6b2016-05-02 19:45:41 -0700141 // not meant to be instantiated
142 private OduCltPortHelper() {}
143}