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