blob: 20bcb25bb41d051e3053c6d5a7dc3a7d57c70e49 [file] [log] [blame]
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001/*
2 * Copyright 2016 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.io.IOException;
21import java.util.Optional;
22
23import org.onosproject.net.Annotations;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.DefaultAnnotations.Builder;
26import org.onosproject.net.OchSignal;
27import org.onosproject.net.OduSignalType;
28import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.SparseAnnotations;
31import org.onosproject.net.device.DefaultPortDescription;
32import org.onosproject.net.device.PortDescription;
33import org.onosproject.net.optical.OchPort;
34import org.onosproject.net.optical.impl.DefaultOchPort;
35import org.onosproject.net.optical.json.OchSignalCodec;
36import org.slf4j.Logger;
37
38import com.fasterxml.jackson.databind.ObjectMapper;
39import com.fasterxml.jackson.databind.node.ObjectNode;
40import com.google.common.annotations.Beta;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070041import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042
43/**
44 * OCh port related helpers.
45 */
46@Beta
47public final class OchPortHelper {
48
49 private static final Logger log = getLogger(OchPortHelper.class);
50
51 private static final ObjectMapper MAPPER = new ObjectMapper();
52
53 // Annotation keys
54 private static final String SIGNAL_TYPE = "signalType";
55 private static final String TUNABLE = "tunable";
56 private static final String LAMBDA = "lambda";
57
58 /**
59 * Creates OCh port DefaultPortDescription based on the supplied information.
60 *
61 * @param number port number
62 * @param isEnabled port enabled state
63 * @param signalType ODU signal type
64 * @param isTunable tunable wavelength capability
65 * @param lambda OCh signal
66 * @return OCh port DefaultPortDescription with OCh annotations
67 */
68 public static PortDescription ochPortDescription(PortNumber number,
69 boolean isEnabled,
70 OduSignalType signalType,
71 boolean isTunable,
72 OchSignal lambda) {
73 return ochPortDescription(number, isEnabled, signalType, isTunable, lambda, DefaultAnnotations.EMPTY);
74 }
75
76 /**
77 * Creates OCh port DefaultPortDescription based on the supplied information.
78 *
79 * @param number port number
80 * @param isEnabled port enabled state
81 * @param signalType ODU signal type
82 * @param isTunable tunable wavelength capability
83 * @param lambda OCh signal
84 * @param annotationsIn key/value annotations map
85 * @return OCh port DefaultPortDescription with OCh annotations
86 */
87 public static PortDescription ochPortDescription(PortNumber number,
88 boolean isEnabled,
89 OduSignalType signalType,
90 boolean isTunable,
91 OchSignal lambda,
92 SparseAnnotations annotationsIn) {
93
94 Builder builder = DefaultAnnotations.builder();
95 builder.putAll(annotationsIn);
96
97 builder.set(TUNABLE, String.valueOf(isTunable));
98 builder.set(LAMBDA, OchSignalCodec.encode(lambda).toString());
99 builder.set(SIGNAL_TYPE, signalType.toString());
100
101 DefaultAnnotations annotations = builder.build();
102 long portSpeed = 0; // FIXME assign appropriate value
103 return new DefaultPortDescription(number, isEnabled, Port.Type.OCH, portSpeed, annotations);
104 }
105
106 /**
107 * Creates OCh port DefaultPortDescription based on the supplied information.
108 *
109 * @param base PortDescription to get basic information from
110 * @param signalType ODU signal type
111 * @param isTunable tunable wavelength capability
112 * @param lambda OCh signal
113 * @param annotations key/value annotations map
114 * @return OCh port DefaultPortDescription with OCh annotations
115 */
116 public static PortDescription ochPortDescription(PortDescription base,
117 OduSignalType signalType,
118 boolean isTunable,
119 OchSignal lambda,
120 SparseAnnotations annotations) {
121 return ochPortDescription(base.portNumber(), base.isEnabled(), signalType, isTunable, lambda, annotations);
122 }
123
124
125 public static Optional<OchPort> asOchPort(Port port) {
126 if (port instanceof OchPort) {
127 return Optional.of((OchPort) port);
128 }
129
130 try {
131 Annotations an = port.annotations();
132
133 OduSignalType signalType = Enum.valueOf(OduSignalType.class,
134 an.value(SIGNAL_TYPE));
135
136 boolean isTunable = Boolean.valueOf(an.value(TUNABLE));
137
138 ObjectNode obj = (ObjectNode) MAPPER.readTree(an.value(LAMBDA));
139 OchSignal lambda = OchSignalCodec.decode(obj);
140
141 // Note: OCh specific annotations is not filtered-out here.
142 // DefaultOchPort should filter them, if necessary.
143 return Optional.of(new DefaultOchPort(port, signalType, isTunable, lambda));
144
145 // TODO: it'll be better to verify each inputs properly
146 // instead of catching all these Exceptions.
147 } catch (IOException | NullPointerException
148 | IllegalArgumentException | ClassCastException e) {
149
150 log.warn("{} was not well-formed OCh port.", port, e);
151 return Optional.empty();
152 }
153 }
154
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700155 /**
156 * Returns {@link Annotations} not used by the port type projection.
157 *
158 * @param input {@link Annotations}
159 * @return filtered view of given {@link Annotations}
160 */
161 public static Annotations stripHandledAnnotations(Annotations input) {
162 return new FilteredAnnotation(input, ImmutableSet.of(SIGNAL_TYPE, TUNABLE, LAMBDA));
163 }
164
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800165 // not meant to be instantiated
166 private OchPortHelper() {}
167}