blob: 15f087919c7536e86589f6bb76140eee262a6079 [file] [log] [blame]
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07001/*
2 * Copyright 2014-2015 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.device.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070019import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070020
Ray Milkeya4122362015-08-18 15:19:08 -070021import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070022import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070023import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DefaultAnnotations;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070025import org.onosproject.net.OchPort;
26import org.onosproject.net.OduCltPort;
27import org.onosproject.net.OmsPort;
28import org.onosproject.net.Port;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070029import org.onosproject.net.PortNumber;
30import org.onosproject.net.SparseAnnotations;
31import org.onosproject.net.device.DefaultPortDescription;
32import org.onosproject.net.device.OchPortDescription;
33import org.onosproject.net.device.OduCltPortDescription;
34import org.onosproject.net.device.OmsPortDescription;
35import org.onosproject.net.device.PortDescription;
36import org.slf4j.Logger;
37
38/**
39 * Implementations of merge policies for various sources of optical port
40 * configuration information. This includes applications, provides, and network
41 * configurations.
42 */
43public final class OpticalPortOperator implements ConfigOperator {
44
45 private static final Logger log = getLogger(OpticalPortOperator.class);
46
47 private OpticalPortOperator() {
48 }
49
50 /**
51 * Generates a PortDescription containing fields from a PortDescription and
52 * an OpticalPortConfig.
53 *
54 * @param opc the port config entity from network config
55 * @param descr a PortDescription
56 * @return PortDescription based on both sources
57 */
58 public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
59 if (opc == null) {
60 return descr;
61 }
62
63 PortNumber port = descr.portNumber();
64 final String name = opc.name();
65 final String numName = opc.numberName();
66 // if the description is null, or the current description port name != config name,
67 // create a new PortNumber.
68 PortNumber newPort = null;
69 if (port == null) {
70 // try to get the portNumber from the numName.
71 if (!numName.isEmpty()) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070072 final long pn = Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070073 newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
74 } else {
75 // we don't have defining info (a port number value)
76 throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
77 }
78 } else if ((!name.isEmpty()) && !name.equals(port.name())) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070079 final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070080 newPort = PortNumber.portNumber(pn, name);
81 }
82
83 // Port type won't change unless we're overwriting a port completely.
84 // Watch out for overwrites to avoid class cast craziness.
85 boolean noOwrite = (opc.type() == descr.type()) ? true : false;
86
87 SparseAnnotations sa = combine(opc, descr.annotations());
88 if (noOwrite) {
89 return updateDescription((newPort == null) ? port : newPort, sa, descr);
90 } else {
91 // TODO: must reconstruct a different type of PortDescription.
92 log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
93 }
94 return descr;
95 }
96
97 // updates a port description whose port type has not changed.
98 private static PortDescription updateDescription(
99 PortNumber port, SparseAnnotations sa, PortDescription descr) {
100 switch (descr.type()) {
101 case OMS:
102 OmsPortDescription oms = (OmsPortDescription) descr;
103 return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
104 oms.maxFrequency(), oms.grid(), sa);
105 case OCH:
106 // We might need to update lambda below with STATIC_LAMBDA.
107 OchPortDescription och = (OchPortDescription) descr;
108 return new OchPortDescription(port, och.isEnabled(), och.signalType(),
109 och.isTunable(), och.lambda(), sa);
110 case ODUCLT:
111 OduCltPortDescription odu = (OduCltPortDescription) descr;
112 return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
113 case PACKET:
114 case FIBER:
115 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
116 descr.portSpeed(), sa);
117 default:
118 // this includes copper ports.
119 log.warn("Unsupported optical port type {} - can't update", descr.type());
120 return descr;
121 }
122 }
123
124 /**
125 * Generates an annotation from an existing annotation and OptcalPortConfig.
126 *
127 * @param opc the port config entity from network config
128 * @param an the annotation
129 * @return annotation combining both sources
130 */
131 public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
132 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
133 if (!opc.staticPort().isEmpty()) {
134 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
135 }
136 if (opc.staticLambda().isPresent()) {
137 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
138 }
139 // The following may not need to be carried.
140 if (!opc.name().isEmpty()) {
141 b.set(AnnotationKeys.PORT_NAME, opc.name());
142 }
143 return DefaultAnnotations.union(an, b.build());
144 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700145
146 /**
147 * Returns a description built from an existing port.
148 *
149 * @param port the device port
150 * @return a PortDescription based on the port
151 */
152 public static PortDescription descriptionOf(Port port) {
153 checkNotNull(port, "Must supply non-null Port");
154 final PortNumber ptn = port.number();
155 final boolean isup = port.isEnabled();
156 final SparseAnnotations an = (SparseAnnotations) port.annotations();
157 switch (port.type()) {
158 case OMS:
159 OmsPort oms = (OmsPort) port;
160 return new OmsPortDescription(ptn, isup, oms.minFrequency(),
161 oms.maxFrequency(), oms.grid(), an);
162 case OCH:
163 OchPort och = (OchPort) port;
164 return new OchPortDescription(ptn, isup, och.signalType(),
165 och.isTunable(), och.lambda(), an);
166 case ODUCLT:
167 OduCltPort odu = (OduCltPort) port;
168 return new OduCltPortDescription(ptn, isup, odu.signalType(), an);
169 default:
170 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
171 }
172 }
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700173}