blob: aeac4f9e150e1a1bacba5fdfb451904141f1abbb [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;
36
37/**
38 * OTU port related helpers.
39 */
40@Beta
41public final class OtuPortHelper {
42
43 private static final Logger log = getLogger(OtuPortHelper.class);
44
45 // Annotation keys
46 /**
47 * {@link OtuSignalType} as String.
48 */
49 private static final String SIGNAL_TYPE = "signalType";
50
51 /**
52 * Creates OTU port description based on the supplied information.
53 *
54 * @param number port number
55 * @param isEnabled port enabled state
56 * @param signalType OTU client signal type
57 */
58 public static PortDescription otuPortDescription(PortNumber number,
59 boolean isEnabled,
60 OtuSignalType signalType) {
61 return otuPortDescription(number, isEnabled, signalType, DefaultAnnotations.EMPTY);
62 }
63
64 /**
65 * Creates OTU port description based on the supplied information.
66 *
67 * @param number port number
68 * @param isEnabled port enabled state
69 * @param signalType OTU client signal type
70 * @param annotations key/value annotations map
71 */
72 public static PortDescription otuPortDescription(PortNumber number,
73 boolean isEnabled,
74 OtuSignalType signalType,
75 SparseAnnotations annotations) {
76 Builder builder = DefaultAnnotations.builder();
77 builder.putAll(annotations);
78
79 builder.set(SIGNAL_TYPE, signalType.toString());
80
81 long portSpeed = 0; // TODO specify appropriate value?
82 return new DefaultPortDescription(number,
83 isEnabled,
84 Port.Type.OTU,
85 portSpeed,
86 builder.build());
87 }
88
89 /**
90 * Creates OTU port description based on the supplied information.
91 *
92 * @param base PortDescription to get basic information from
93 * @param signalType OTU client signal type
94 * @param annotations key/value annotations map
95 */
96 public static PortDescription otuPortDescription(PortDescription base,
97 OtuSignalType signalType,
98 SparseAnnotations annotations) {
99 return otuPortDescription(base.portNumber(), base.isEnabled(), signalType, annotations);
100 }
101
102 public static Optional<OtuPort> asOtuPort(Port port) {
103 if (port instanceof OtuPort) {
104 return Optional.of((OtuPort) port);
105 }
106
107 try {
108 Annotations an = port.annotations();
109
110 OtuSignalType signalType = Enum.valueOf(OtuSignalType.class,
111 an.value(SIGNAL_TYPE));
112
113
114 // Note: OTU specific annotations is not filtered-out here.
115 // DefaultOtuPort should filter them, if necessary.
116 return Optional.of(new DefaultOtuPort(port, signalType));
117
118 } catch (NullPointerException | IllegalArgumentException e) {
119
120 log.warn("{} was not well-formed Otu port.", port, e);
121 return Optional.empty();
122 }
123 }
124
125
126 // not meant to be instantiated
127 private OtuPortHelper() {}
128
129}