blob: 84f1e37683f648c24bb57a7c89d864a669139cd0 [file] [log] [blame]
MaoLu0cde9ce2017-01-23 19:08:35 -08001/*
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.driver.optical.handshaker;
17
Jimmy Jin10852392017-01-24 13:45:13 -080018import org.onosproject.net.behaviour.protection.ProtectionConfigBehaviour;
MaoLu0cde9ce2017-01-23 19:08:35 -080019import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
20import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsReply;
21import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsRequest;
22import org.projectfloodlight.openflow.protocol.OFCalientPortStatsEntry;
23import org.projectfloodlight.openflow.protocol.OFCalientPortStatsRequest;
24import org.projectfloodlight.openflow.protocol.OFCalientPortStatsReply;
25import org.projectfloodlight.openflow.protocol.OFCalientStatsReply;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFPortDesc;
28import org.projectfloodlight.openflow.protocol.OFStatsRequest;
29import org.projectfloodlight.openflow.protocol.OFStatsReply;
30import org.projectfloodlight.openflow.protocol.OFStatsType;
31import org.projectfloodlight.openflow.protocol.OFType;
32
33import java.io.IOException;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
37import java.util.Set;
38import java.util.concurrent.atomic.AtomicBoolean;
39import java.util.stream.Collectors;
40
41import org.onosproject.net.DefaultAnnotations;
42import org.onosproject.net.Device;
43import org.onosproject.net.device.DefaultPortDescription;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.device.PortDescription;
46import org.onosproject.net.Port;
47import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
48import org.onosproject.openflow.controller.PortDescPropertyType;
49import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
50import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted;
51import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted;
52import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted;
53import org.projectfloodlight.openflow.protocol.OFObject;
54
55import com.google.common.collect.ImmutableList;
56import com.google.common.collect.ImmutableSet;
57
Jimmy Jin10852392017-01-24 13:45:13 -080058import static org.onosproject.net.optical.OpticalAnnotations.*;
59
MaoLu0cde9ce2017-01-23 19:08:35 -080060
61/**
62 * Oplink Open Flow Protection Optical Switch handshaker - for Open Flow 1.3.
63 * In order to reduce the code changes in the short term, we reuse Calient message structure.
64 */
65public class OplinkSwitchHandshaker extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch {
66
Jimmy Jin10852392017-01-24 13:45:13 -080067 private static final String PROTECTION_FINGERPRINT = "OplinkOPS";
68 private static final int PROTECTION_VIRTUAL_PORT = 0;
69
MaoLu0cde9ce2017-01-23 19:08:35 -080070 private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false);
71 private List<OFCalientPortDescStatsEntry> opticalPorts = new ArrayList<>();
MaoLu0cde9ce2017-01-23 19:08:35 -080072
73 private enum SubType {
74 PORT_DESC_STATS, // Port description stats openflow message
75 FLOW_STATS, // Flow stats openflow message
76 PORT_STATS // Port stats openflow message
77 }
78
79 @Override
80 public Boolean supportNxRole() {
81 return false;
82 }
83
84 @Override
85 public void startDriverHandshake() {
86 log.info("OPLK Switch: Starting driver handshake for sw {}", getStringId());
87 if (startDriverHandshakeCalled) {
88 throw new SwitchDriverSubHandshakeAlreadyStarted();
89 }
90 startDriverHandshakeCalled = true;
91
92 log.debug("OPLK Switch: sendHandshakeOFExperimenterPortDescRequest for sw {}", getStringId());
93
94 try {
95 sendHandshakeOFExperimenterPortDescRequest();
96 } catch (IOException e) {
97 log.error("OPLK Switch: exception while sending experimenter port desc:", e);
98 }
99 }
100
101 @Override
102 public void processDriverHandshakeMessage(OFMessage m) {
103 if (!startDriverHandshakeCalled) {
104 throw new SwitchDriverSubHandshakeNotStarted();
105 }
106 if (driverHandshakeComplete.get()) {
107 throw new SwitchDriverSubHandshakeCompleted(m);
108 }
109
110 log.info("OPLK Switch: processDriverHandshakeMessage for sw {}", getStringId());
111
112 switch (m.getType()) {
113 case STATS_REPLY: // multipart message is reported as STAT
114 processOFMultipartReply((OFStatsReply) m);
115 driverHandshakeComplete.set(true);
116 break;
117 default:
118 log.warn("OPLK Switch: Received message {} during switch-driver " +
119 "subhandshake from switch {} ... " +
120 "Ignoring message", m, getStringId());
121 }
122 }
123
124 @Override
125 public final void sendMsg(OFMessage m) {
126 List<OFMessage> messages = new ArrayList<>();
127 messages.add(m);
128 if (m.getType() == OFType.STATS_REQUEST) {
129 OFStatsRequest sr = (OFStatsRequest) m;
130 log.debug("OPLK Switch: Rebuilding stats request type {}", sr.getStatsType());
131 switch (sr.getStatsType()) {
132 case PORT:
133 //Send experiment status request for Optical Fiber switch to device
134 //Note: We just re-use calient message for a short term.
135 OFCalientPortStatsRequest portRequest = this.factory().buildCalientPortStatsRequest()
136 .setXid(sr.getXid())
137 .setFlags(sr.getFlags())
138 .build();
139 messages.add(portRequest);
140 break;
141 default:
142 break;
143 }
144 } else {
145 log.debug("OPLK Switch: sends msg:{}, as is", m.getType());
146 }
147 super.sendMsg(messages);
148 }
149
150 @Override
151 public boolean isDriverHandshakeComplete() {
152 return driverHandshakeComplete.get();
153 }
154
155 private void sendHandshakeOFExperimenterPortDescRequest() throws
156 IOException {
Jimmy Jin10852392017-01-24 13:45:13 -0800157 /*
158 *Note:
MaoLu0cde9ce2017-01-23 19:08:35 -0800159 * Oplink protection switch and Calient switch are both optical fiber switch,
160 * so Calient port description matches well for Oplink switch.
161 * OFCalientPortDescStatsRequest is generated by loxi.
162 * If change the OF message name, we need to change onos-loxi.
163 * To reduce code change for a short term, we reuse calient message and message name.
164 * These will be re-processed in the future.
165 */
166 OFCalientPortDescStatsRequest preq = factory()
167 .buildCalientPortDescStatsRequest()
168 .setXid(getNextTransactionId())
169 .build();
170
171 log.info("OPLK Switch: Sending experimented port description message {}", preq);
172
173 this.sendHandshakeMessage(preq);
174 }
175
176 @Override
177 public Device.Type deviceType() {
178 return Device.Type.FIBER_SWITCH;
179 }
180
181 /*
182 * OduClt ports are reported as regular ETH ports.
183 */
184 @Override
185 public List<OFPortDesc> getPorts() {
186 return ImmutableList.copyOf(
187 ports.stream().flatMap(p -> p.getEntries().stream())
188 .collect(Collectors.toList()));
189 }
190
191 @Override
192 public List<? extends OFObject> getPortsOf(PortDescPropertyType type) {
193 return ImmutableList.copyOf(opticalPorts);
194 }
195
196 @Override
197 public Set<PortDescPropertyType> getPortTypes() {
198 return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT);
199 }
200
201 @Override
202 public List<PortDescription> processExpPortStats(OFMessage msg) {
203 OFCalientStatsReply statsReply = (OFCalientStatsReply) msg;
204 //Sub type from openflowj is start from index 1
205 SubType type = SubType.values()[(int) statsReply.getSubtype() - 1];
206 switch (type) {
207 case PORT_STATS:
208 //Note: We just re-use calient message for a short term.
209 OFCalientPortStatsReply portStats = (OFCalientPortStatsReply) msg;
210 return buildPortDescriptions(portStats.getEntries());
211 default:
212 //Ignore other messages
213 log.warn("OPLK Switch: Received message {} from switch {} ... " +
214 "Ignoring message", msg, getStringId());
215 return null;
216 }
217 }
218
219 private List<PortDescription> buildPortDescriptions(List<OFCalientPortStatsEntry> entries) {
220 DeviceService deviceService = this.handler().get(DeviceService.class);
221 List<Port> ports = deviceService.getPorts(this.data().deviceId());
222 HashMap<Long, OFCalientPortStatsEntry> statsMap = new HashMap<>(entries.size());
223 entries.forEach(entry -> statsMap.put((long) entry.getPortNo().getPortNumber(), entry));
224 final List<PortDescription> portDescs = new ArrayList<>();
225 for (Port port : ports) {
226 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
227 builder.putAll(port.annotations());
Jimmy Jin10852392017-01-24 13:45:13 -0800228
229 //set fingerprint for the virtual port (port 0)
230 if (port.number().toLong() == PROTECTION_VIRTUAL_PORT) {
231 builder.set(ProtectionConfigBehaviour.FINGERPRINT, PROTECTION_FINGERPRINT);
232 }
233
MaoLu0cde9ce2017-01-23 19:08:35 -0800234 OFCalientPortStatsEntry entry = statsMap.get(port.number().toLong());
235 if (entry == null) {
236 continue;
237 }
Jimmy Jin10852392017-01-24 13:45:13 -0800238 builder.set(CURRENT_POWER, entry.getInportPower());
239 builder.set(OUTPUT_POWER, entry.getOutportPower());
MaoLu0cde9ce2017-01-23 19:08:35 -0800240 //Note: There are some mistakes about bitmask encoding and decoding in openflowj.
241 //We just use this code for a short term, and will modify in the future.
242 if (entry.getInOperStatus().isEmpty()) {
Jimmy Jin10852392017-01-24 13:45:13 -0800243 builder.set(INPUT_PORT_STATUS, STATUS_IN_SERVICE);
MaoLu0cde9ce2017-01-23 19:08:35 -0800244 } else {
Jimmy Jin10852392017-01-24 13:45:13 -0800245 builder.set(INPUT_PORT_STATUS, STATUS_OUT_SERVICE);
MaoLu0cde9ce2017-01-23 19:08:35 -0800246 }
247 if (entry.getOutOperStatus().isEmpty()) {
Jimmy Jin10852392017-01-24 13:45:13 -0800248 builder.set(OUTPUT_PORT_STATUS, STATUS_IN_SERVICE);
MaoLu0cde9ce2017-01-23 19:08:35 -0800249 } else {
Jimmy Jin10852392017-01-24 13:45:13 -0800250 builder.set(OUTPUT_PORT_STATUS, STATUS_OUT_SERVICE);
MaoLu0cde9ce2017-01-23 19:08:35 -0800251 }
252 portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(),
253 port.type(), port.portSpeed(), builder.build()));
254 }
255 return portDescs;
256 }
257
258 private void processOFMultipartReply(OFStatsReply stats) {
259 log.debug("OPLK Switch: Received message {} during switch-driver " +
260 "subhandshake from switch {} ... ", stats, getStringId());
261 //Process experimenter messages
262 if (stats.getStatsType() == OFStatsType.EXPERIMENTER) {
263 try {
264 //Note: We just re-use calient message for a short term.
265 OFCalientPortDescStatsReply descReply = (OFCalientPortDescStatsReply) stats;
266 opticalPorts.addAll(descReply.getPortDesc());
267 driverHandshakeComplete.set(true);
268 } catch (ClassCastException e) {
269 log.error("OPLK Switch: Unexspected Experimenter Multipart message type {} ",
270 stats.getClass().getName());
271 }
272 }
273 }
274}