alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
| 18 | import org.onlab.osgi.ServiceDirectory; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DefaultAnnotations; |
| 23 | import org.onosproject.net.Device; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 25 | import org.onosproject.net.MastershipRole; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.behaviour.Pipeliner; |
| 28 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 29 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 30 | import org.onosproject.net.device.DeviceDescription; |
| 31 | import org.onosproject.net.device.DeviceProvider; |
| 32 | import org.onosproject.net.device.DeviceProviderRegistry; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceService; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 35 | import org.onosproject.net.flow.DefaultFlowRule; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.FlowRuleOperations; |
| 40 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.flow.instructions.Instructions; |
| 45 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 46 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.NextObjective; |
| 48 | import org.onosproject.net.flowobjective.ObjectiveError; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 49 | import org.onosproject.net.packet.PacketPriority; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 50 | import org.onosproject.net.provider.AbstractProvider; |
| 51 | import org.onosproject.net.provider.ProviderId; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 54 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
Jonathan Hart | 64da69d | 2015-07-15 15:10:28 -0700 | [diff] [blame] | 58 | * Pipeliner for OLT device. |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 59 | */ |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame^] | 60 | public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 64 | static final ProviderId PID = new ProviderId("olt", "org.onosproject.olt", true); |
| 65 | |
| 66 | static final String DEVICE = "isAccess"; |
| 67 | static final String OLT = "true"; |
| 68 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 69 | private ServiceDirectory serviceDirectory; |
| 70 | private FlowRuleService flowRuleService; |
| 71 | private DeviceId deviceId; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 72 | private CoreService coreService; |
| 73 | |
| 74 | private ApplicationId appId; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 75 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 76 | private DeviceProvider provider = new AnnotationProvider(); |
| 77 | |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 78 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 81 | this.serviceDirectory = context.directory(); |
| 82 | this.deviceId = deviceId; |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 83 | DeviceProviderRegistry registry = |
| 84 | serviceDirectory.get(DeviceProviderRegistry.class); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 85 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 86 | coreService = serviceDirectory.get(CoreService.class); |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 87 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 88 | /*try { |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 89 | DeviceProviderService providerService = registry.register(provider); |
| 90 | providerService.deviceConnected(deviceId, |
| 91 | description(deviceId, DEVICE, OLT)); |
| 92 | } finally { |
| 93 | registry.unregister(provider); |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 94 | }*/ |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 95 | |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 96 | appId = coreService.registerApplication( |
| 97 | "org.onosproject.driver.OLTPipeline"); |
| 98 | |
| 99 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 100 | .matchEthType(EthType.EtherType.EAPOL.ethType().toShort()) |
| 101 | .build(); |
| 102 | |
| 103 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 104 | .punt() |
| 105 | .build(); |
| 106 | |
| 107 | FlowRule flowRule = new DefaultFlowRule(deviceId, selector, treatment, |
| 108 | PacketPriority.CONTROL.priorityValue(), |
| 109 | appId, 0, true, null); |
| 110 | |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 111 | //flowRuleService.applyFlowRules(flowRule); |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public void filter(FilteringObjective filter) { |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 116 | throw new UnsupportedOperationException("OLT does not filter."); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void forward(ForwardingObjective fwd) { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 121 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 122 | |
| 123 | if (fwd.flag() != ForwardingObjective.Flag.VERSATILE) { |
| 124 | throw new UnsupportedOperationException( |
| 125 | "Only VERSATILE is supported."); |
| 126 | } |
| 127 | |
| 128 | boolean isPunt = fwd.treatment().immediate().stream().anyMatch(i -> { |
| 129 | if (i instanceof Instructions.OutputInstruction) { |
| 130 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 131 | return out.port().equals(PortNumber.CONTROLLER); |
| 132 | } |
| 133 | return false; |
| 134 | }); |
| 135 | |
| 136 | if (isPunt) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | TrafficSelector selector = fwd.selector(); |
| 141 | TrafficTreatment treatment = fwd.treatment(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 142 | |
| 143 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 144 | .forDevice(deviceId) |
| 145 | .withSelector(selector) |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 146 | .withTreatment(treatment) |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 147 | .fromApp(fwd.appId()) |
| 148 | .withPriority(fwd.priority()); |
| 149 | |
| 150 | if (fwd.permanent()) { |
| 151 | ruleBuilder.makePermanent(); |
| 152 | } else { |
| 153 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 154 | } |
| 155 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 156 | switch (fwd.op()) { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 157 | case ADD: |
| 158 | flowBuilder.add(ruleBuilder.build()); |
| 159 | break; |
| 160 | case REMOVE: |
| 161 | flowBuilder.remove(ruleBuilder.build()); |
| 162 | break; |
| 163 | default: |
| 164 | log.warn("Unknown operation {}", fwd.op()); |
| 165 | } |
| 166 | |
| 167 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 168 | @Override |
| 169 | public void onSuccess(FlowRuleOperations ops) { |
| 170 | if (fwd.context().isPresent()) { |
| 171 | fwd.context().get().onSuccess(fwd); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onError(FlowRuleOperations ops) { |
| 177 | if (fwd.context().isPresent()) { |
| 178 | fwd.context().get().onError(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 179 | } |
| 180 | } |
| 181 | })); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void next(NextObjective nextObjective) { |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 186 | throw new UnsupportedOperationException("OLT does not next hop."); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Build a device description. |
Jonathan Hart | 3e59464 | 2015-10-20 17:31:24 -0700 | [diff] [blame] | 191 | * |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 192 | * @param deviceId a deviceId |
| 193 | * @param key the key of the annotation |
| 194 | * @param value the value for the annotation |
| 195 | * @return a device description |
| 196 | */ |
| 197 | private DeviceDescription description(DeviceId deviceId, String key, String value) { |
| 198 | DeviceService deviceService = serviceDirectory.get(DeviceService.class); |
| 199 | Device device = deviceService.getDevice(deviceId); |
| 200 | |
| 201 | checkNotNull(device, "Device not found in device service."); |
| 202 | |
| 203 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 204 | if (value != null) { |
| 205 | builder.set(key, value); |
| 206 | } else { |
| 207 | builder.remove(key); |
| 208 | } |
| 209 | return new DefaultDeviceDescription(device.id().uri(), device.type(), |
| 210 | device.manufacturer(), device.hwVersion(), |
| 211 | device.swVersion(), device.serialNumber(), |
| 212 | device.chassisId(), builder.build()); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Simple ancillary provider used to annotate device. |
| 217 | */ |
| 218 | private static final class AnnotationProvider |
| 219 | extends AbstractProvider implements DeviceProvider { |
| 220 | private AnnotationProvider() { |
| 221 | super(PID); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void triggerProbe(DeviceId deviceId) { |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean isReachable(DeviceId deviceId) { |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 238 | } |