blob: 01573870b53344b1a12fbc459296898f71db6e8a [file] [log] [blame]
Jimmy Jin10852392017-01-24 13:45:13 -08001/*
2 * Copyright 2017-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 */
16
17package org.onosproject.driver.optical.protection;
18
MaoLu937cf422017-03-03 23:31:46 -080019import org.onosproject.core.CoreService;
Jimmy Jin10852392017-01-24 13:45:13 -080020import org.onosproject.net.DeviceId;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.FilteredConnectPoint;
23import org.onosproject.net.Link;
24import org.onosproject.net.LinkKey;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription;
28import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState;
29import org.onosproject.net.behaviour.protection.ProtectionConfigBehaviour;
30import org.onosproject.net.behaviour.protection.TransportEndpointDescription;
31import org.onosproject.net.behaviour.protection.TransportEndpointId;
32import org.onosproject.net.behaviour.protection.TransportEndpointState;
33import org.onosproject.net.config.NetworkConfigService;
34import org.onosproject.net.config.basics.BasicLinkConfig;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.driver.AbstractHandlerBehaviour;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.DefaultTrafficSelector;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.FlowRule;
41import org.onosproject.net.flow.FlowRuleService;
42import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
44import org.onosproject.net.link.LinkService;
45import org.onosproject.net.optical.OpticalAnnotations;
46import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.ArrayList;
50import java.util.HashMap;
51import java.util.List;
52import java.util.Map;
53import java.util.Set;
54import java.util.concurrent.CompletableFuture;
55
56import static org.onosproject.net.LinkKey.linkKey;
57
58/**
59 * Implementations of the protection behaviours for Oplink Optical Protection Switch (OPS).
60 * - Oplink OPS has 3 logical bi-directional ports (6 uni-directional physical ports):
61 * - port 1 is primary port for network side
62 * - port 2 is secondary port for network side.
63 * - port 3 is connected to the client side port;
64 * - Traffic protection
65 * - Traffic(Optical light) from client port is broadcasted (50/50 split) to
66 * both primary and secondary ports all the time.
67 * - In fault free condition, traffic from primary port is bridged to client port and
68 * in the case of primary port fails (LOS), traffic is bridged from secondary port to client port.
69 * - User initiated switch (to primary or secondary) is also supported.
70 */
71public class OplinkSwitchProtection extends AbstractHandlerBehaviour implements ProtectionConfigBehaviour {
72
73 private static final int VIRTUAL_PORT = 0;
74 private static final int PRIMARY_PORT = 1;
75 private static final int SECONDARY_PORT = 2;
76 private static final int CLIENT_PORT = 3;
MaoLu937cf422017-03-03 23:31:46 -080077 private static final int FLOWRULE_PRIORITY = 88;
Jimmy Jin10852392017-01-24 13:45:13 -080078 private static final String PRIMARY_ID = "primary_port";
79 private static final String SECONDARY_ID = "secondary_port";
80 private static final String OPLINK_FINGERPRINT = "OplinkOPS";
MaoLu937cf422017-03-03 23:31:46 -080081 private static final String APP_ID = "org.onosproject.drivers.optical";
Jimmy Jin10852392017-01-24 13:45:13 -080082
83 protected final Logger log = LoggerFactory.getLogger(getClass());
84
Jimmy Jin10852392017-01-24 13:45:13 -080085 @Override
86 public CompletableFuture<ConnectPoint> createProtectionEndpoint(
87 ProtectedTransportEndpointDescription configuration) {
88 //This OPS device only support one protection group of port 2 and port 3
89
90 CompletableFuture result = new CompletableFuture<ConnectPoint>();
91
92 //add flow from client port to virtual port. This set device in auto switch mode
93 addFlow(PortNumber.portNumber(VIRTUAL_PORT));
94
95 //add a virtual link bewteen two virtual ports of this device and peer
96 addLinkToPeer(configuration.peer());
97
MaoLu937cf422017-03-03 23:31:46 -080098 result.complete(new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)));
Jimmy Jin10852392017-01-24 13:45:13 -080099
100 return result;
101 }
102
103 @Override
104 public CompletableFuture<ConnectPoint> updateProtectionEndpoint(
105 ConnectPoint identifier, ProtectedTransportEndpointDescription configuration) {
106
107 log.warn("Update protection configuration is not supported by this device");
108
109 CompletableFuture result = new CompletableFuture<ConnectPoint>();
MaoLu937cf422017-03-03 23:31:46 -0800110 result.complete(new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)));
Jimmy Jin10852392017-01-24 13:45:13 -0800111
112 return result;
113 }
114
115 @Override
116 public CompletableFuture<Boolean> deleteProtectionEndpoint(ConnectPoint identifier) {
117 //OPS has only one protection group
118 CompletableFuture result = new CompletableFuture<Boolean>();
119
120 if (identifier.port().toLong() == VIRTUAL_PORT) {
121 //add a link bewteen two virtual ports of this device and peer
122 removeLinkToPeer(getPeerId());
123 deleteFlow();
124 result.complete(true);
125 } else {
126 result.complete(false);
127 }
128
129 return result;
130 }
131
132 @Override
133 public CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointDescription>> getProtectionEndpointConfigs() {
MaoLu937cf422017-03-03 23:31:46 -0800134 ConnectPoint cp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT));
Jimmy Jin10852392017-01-24 13:45:13 -0800135
136 Map<ConnectPoint, ProtectedTransportEndpointDescription> protectedGroups = new HashMap<>();
137 CompletableFuture result = new CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointDescription>>();
138
139 protectedGroups.put(cp, getProtectedTransportEndpointDescription());
140 result.complete(protectedGroups);
141
142 return result;
143 }
144
145 @Override
146 public CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointState>> getProtectionEndpointStates() {
MaoLu937cf422017-03-03 23:31:46 -0800147 ConnectPoint cp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT));
Jimmy Jin10852392017-01-24 13:45:13 -0800148
149 Map<ConnectPoint, ProtectedTransportEndpointState> protectedGroups = new HashMap<>();
150 CompletableFuture result = new CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointState>>();
151
152 protectedGroups.put(cp, getProtectedTransportEndpointState());
153 result.complete(protectedGroups);
154
155 return result;
156 }
157
158 /*
159 ** index: PRIMARY_PORT - manual switch to primary port
160 * SECONDARY_PORT - manual switch to Secondary port
161 * VIRTUAL_PORT - automatic switch mode
162 */
163 @Override
164 public CompletableFuture<Void> switchWorkingPath(ConnectPoint identifier, int index) {
165 CompletableFuture result = new CompletableFuture<Boolean>();
166
167 //send switch command to switch to device by sending a flow-mod message.
168 if (identifier.port().toLong() == VIRTUAL_PORT) {
169 deleteFlow();
170 addFlow(PortNumber.portNumber(index));
171 result.complete(true);
172 } else {
173 result.complete(false);
174 }
175
176 return result;
177 }
178
179 /*
180 * return the protected endpoint description of this devices
181 */
182 private ProtectedTransportEndpointDescription getProtectedTransportEndpointDescription() {
183 List<TransportEndpointDescription> teds = new ArrayList<>();
184 FilteredConnectPoint fcpPrimary = new FilteredConnectPoint(
185 new ConnectPoint(data().deviceId(), PortNumber.portNumber(PRIMARY_PORT)));
186 FilteredConnectPoint fcpSecondary = new FilteredConnectPoint(
187 new ConnectPoint(data().deviceId(), PortNumber.portNumber(SECONDARY_PORT)));
188 TransportEndpointDescription tedPrimary = TransportEndpointDescription.builder()
189 .withOutput(fcpPrimary).build();
190 TransportEndpointDescription tedSecondary = TransportEndpointDescription.builder()
191 .withOutput(fcpSecondary).build();
192
193 teds.add(tedPrimary);
194 teds.add(tedSecondary);
MaoLu937cf422017-03-03 23:31:46 -0800195 return ProtectedTransportEndpointDescription.of(teds, getPeerId(), OPLINK_FINGERPRINT);
Jimmy Jin10852392017-01-24 13:45:13 -0800196 }
197
198 /*
199 * get endpoint state attributes
200 */
201 private Map<String, String> getProtectionStateAttributes(PortNumber portNumber) {
202 Map<String, String> attributes = new HashMap<>();
203
204 //get status form port annotations, the status is update by hand shaker driver periodically
MaoLu937cf422017-03-03 23:31:46 -0800205 Port port = handler().get(DeviceService.class).getPort(data().deviceId(), portNumber);
Jimmy Jin10852392017-01-24 13:45:13 -0800206 if (port != null) {
207 String portStatus = port.annotations().value(OpticalAnnotations.INPUT_PORT_STATUS);
208 attributes.put(OpticalAnnotations.INPUT_PORT_STATUS, portStatus);
209 }
210 return attributes;
211 }
212
MaoLua8998df2017-03-15 14:20:38 -0700213 /*
214 * get activer port number
215 */
216 private int getActivePort() {
MaoLu937cf422017-03-03 23:31:46 -0800217 Port port = handler().get(DeviceService.class)
218 .getPort(data().deviceId(), PortNumber.portNumber(PRIMARY_PORT));
Jimmy Jin10852392017-01-24 13:45:13 -0800219 if (port != null) {
220 if (port.annotations().value(OpticalAnnotations.INPUT_PORT_STATUS)
221 .equals(OpticalAnnotations.STATUS_IN_SERVICE)) {
222 return PRIMARY_PORT;
223 }
224 }
225 return SECONDARY_PORT;
226 }
227
228 /*
MaoLua8998df2017-03-15 14:20:38 -0700229 * get active path index
230 */
231 private int getActiveIndex(List<TransportEndpointState> pathStates) {
232 long activePort = (long) getActivePort();
233 int activeIndex = 0;
234 for (TransportEndpointState state : pathStates) {
235 if (state.description().output().connectPoint().port().toLong() == activePort) {
236 return activeIndex;
237 }
238 ++activeIndex;
239 }
240 return ProtectedTransportEndpointState.ACTIVE_UNKNOWN;
241 }
242
243 /*
Jimmy Jin10852392017-01-24 13:45:13 -0800244 * get protected endpoint state
245 */
246 private ProtectedTransportEndpointState getProtectedTransportEndpointState() {
247 List<TransportEndpointState> tess = new ArrayList<>();
MaoLu937cf422017-03-03 23:31:46 -0800248 PortNumber portPrimary = PortNumber.portNumber(PRIMARY_PORT);
249 PortNumber portSecondary = PortNumber.portNumber(SECONDARY_PORT);
Jimmy Jin10852392017-01-24 13:45:13 -0800250 FilteredConnectPoint fcpPrimary = new FilteredConnectPoint(
MaoLu937cf422017-03-03 23:31:46 -0800251 new ConnectPoint(data().deviceId(), portPrimary));
Jimmy Jin10852392017-01-24 13:45:13 -0800252 FilteredConnectPoint fcpSecondary = new FilteredConnectPoint(
MaoLu937cf422017-03-03 23:31:46 -0800253 new ConnectPoint(data().deviceId(), portSecondary));
Jimmy Jin10852392017-01-24 13:45:13 -0800254 TransportEndpointDescription tedPrimary = TransportEndpointDescription.builder()
255 .withOutput(fcpPrimary).build();
256 TransportEndpointDescription tedSecondary = TransportEndpointDescription.builder()
257 .withOutput(fcpSecondary).build();
258
259 TransportEndpointState tesPrimary = TransportEndpointState.builder()
260 .withDescription(tedPrimary)
261 .withId(TransportEndpointId.of(PRIMARY_ID))
MaoLu937cf422017-03-03 23:31:46 -0800262 .addAttributes(getProtectionStateAttributes(portPrimary))
Jimmy Jin10852392017-01-24 13:45:13 -0800263 .build();
264 TransportEndpointState tesSecondary = TransportEndpointState.builder()
265 .withDescription(tedSecondary)
266 .withId(TransportEndpointId.of(SECONDARY_ID))
MaoLu937cf422017-03-03 23:31:46 -0800267 .addAttributes(getProtectionStateAttributes((portSecondary)))
Jimmy Jin10852392017-01-24 13:45:13 -0800268 .build();
269
270 tess.add(tesPrimary);
271 tess.add(tesSecondary);
272 return ProtectedTransportEndpointState.builder()
273 .withDescription(getProtectedTransportEndpointDescription())
274 .withPathStates(tess)
MaoLua8998df2017-03-15 14:20:38 -0700275 .withActivePathIndex(getActiveIndex(tess))
Jimmy Jin10852392017-01-24 13:45:13 -0800276 .build();
277 }
278
279 /*
280 * - Protection switch is controlled by setting up flow on the device
281 * - There is only one flow on the device at any point
282 * - A flow from virtual port to client port indicates the device is in auto switch mode
283 * - A flow from primary port to client port indicates the device is manually switched to primary
284 * - A flow from secondary port to client port indicates the device is manually switched to secondary
285 */
286 private void addFlow(PortNumber workingPort) {
MaoLu937cf422017-03-03 23:31:46 -0800287 // set working port as flow's input port
288 TrafficSelector selector = DefaultTrafficSelector.builder()
289 .matchInPort(workingPort)
290 .build();
291 // the flow's output port is always the clinet port
292 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
293 .setOutput(PortNumber.portNumber(CLIENT_PORT))
294 .build();
295 FlowRule flowRule = DefaultFlowRule.builder()
296 .forDevice(data().deviceId())
297 .fromApp(handler().get(CoreService.class).getAppId(APP_ID))
298 .withPriority(FLOWRULE_PRIORITY)
299 .withSelector(selector)
300 .withTreatment(treatment)
301 .makePermanent()
302 .build();
Jimmy Jin10852392017-01-24 13:45:13 -0800303
304 // install flow rule
MaoLu937cf422017-03-03 23:31:46 -0800305 handler().get(FlowRuleService.class).applyFlowRules(flowRule);
Jimmy Jin10852392017-01-24 13:45:13 -0800306 }
307 /*
308 Delete all the flows to put device in default mode.
309 */
310 private void deleteFlow() {
311 // remove all the flows.
MaoLu937cf422017-03-03 23:31:46 -0800312 handler().get(FlowRuleService.class).purgeFlowRules(data().deviceId());
Jimmy Jin10852392017-01-24 13:45:13 -0800313 }
314
315 private void addLinkToPeer(DeviceId peerId) {
316
317 if (peerId == null) {
318 log.warn("PeerID is null for device {}", data().deviceId());
319 return;
320 }
321 ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT));
322 ConnectPoint srcCp = new ConnectPoint(peerId, PortNumber.portNumber(VIRTUAL_PORT));
323 LinkKey link = linkKey(srcCp, dstCp);
MaoLu937cf422017-03-03 23:31:46 -0800324 BasicLinkConfig cfg = handler().get(NetworkConfigService.class)
325 .addConfig(link, BasicLinkConfig.class);
Jimmy Jin10852392017-01-24 13:45:13 -0800326 cfg.type(Link.Type.VIRTUAL);
327 cfg.apply();
328 }
329
330 private void removeLinkToPeer(DeviceId peerId) {
331 if (peerId == null) {
332 log.warn("PeerID is null for device {}", data().deviceId());
333 return;
334 }
335 ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT));
336 ConnectPoint srcCp = new ConnectPoint(peerId, PortNumber.portNumber(VIRTUAL_PORT));
337 LinkKey link = linkKey(srcCp, dstCp);
MaoLu937cf422017-03-03 23:31:46 -0800338 handler().get(NetworkConfigService.class).removeConfig(link, BasicLinkConfig.class);
Jimmy Jin10852392017-01-24 13:45:13 -0800339 }
340
341 private DeviceId getPeerId() {
342 ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT));
MaoLu937cf422017-03-03 23:31:46 -0800343 Set<Link> links = handler().get(LinkService.class).getIngressLinks(dstCp);
Jimmy Jin10852392017-01-24 13:45:13 -0800344
345 for (Link l : links) {
346 if (l.type() == Link.Type.VIRTUAL) {
347 // this devide is the destination and peer is the source.
348 return l.src().deviceId();
349 }
350 }
351
MaoLu937cf422017-03-03 23:31:46 -0800352 return data().deviceId();
Jimmy Jin10852392017-01-24 13:45:13 -0800353 }
354}