Murat Parlakisik | 4f50e3f | 2016-04-11 03:24:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Murat Parlakisik | 4f50e3f | 2016-04-11 03:24:44 -0700 | [diff] [blame] | 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 | |
| 19 | import org.onlab.osgi.ServiceDirectory; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.behaviour.NextGroup; |
| 24 | import org.onosproject.net.behaviour.Pipeliner; |
| 25 | import org.onosproject.net.behaviour.PipelinerContext; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleOperations; |
| 30 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 31 | import org.onosproject.net.flow.FlowRuleService; |
| 32 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 34 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 35 | import org.onosproject.net.flowobjective.NextObjective; |
| 36 | import org.onosproject.net.flowobjective.Objective; |
| 37 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 38 | |
| 39 | import org.slf4j.Logger; |
| 40 | import java.util.Collection; |
| 41 | import java.util.Collections; |
| 42 | import java.util.List; |
| 43 | import java.util.Objects; |
| 44 | |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Driver for Hp. Default table starts from 200. |
| 50 | */ |
| 51 | public class HpPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
| 52 | |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | private ServiceDirectory serviceDirectory; |
| 57 | private FlowRuleService flowRuleService; |
| 58 | private CoreService coreService; |
| 59 | private DeviceId deviceId; |
| 60 | private ApplicationId appId; |
| 61 | protected FlowObjectiveStore flowObjectiveStore; |
| 62 | |
| 63 | //FIXME: hp table numbers are configurable . Set this parameter configurable |
| 64 | private static final int SOFTWARE_TABLE_START = 200; |
| 65 | private static final int TIME_OUT = 30; |
| 66 | |
| 67 | @Override |
| 68 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 69 | log.debug("Initiate HP pipeline"); |
| 70 | this.serviceDirectory = context.directory(); |
| 71 | this.deviceId = deviceId; |
| 72 | |
| 73 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 74 | coreService = serviceDirectory.get(CoreService.class); |
| 75 | flowObjectiveStore = context.store(); |
| 76 | |
| 77 | appId = coreService.registerApplication( |
| 78 | "org.onosproject.driver.HpPipeline"); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void filter(FilteringObjective filter) { |
| 83 | //Do nothing |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void forward(ForwardingObjective forwardObjective) { |
| 88 | |
| 89 | Collection<FlowRule> rules; |
| 90 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 91 | |
| 92 | rules = processForward(forwardObjective); |
| 93 | |
| 94 | switch (forwardObjective.op()) { |
| 95 | case ADD: |
| 96 | rules.stream() |
| 97 | .filter(Objects::nonNull) |
| 98 | .forEach(flowOpsBuilder::add); |
| 99 | break; |
| 100 | case REMOVE: |
| 101 | rules.stream() |
| 102 | .filter(Objects::nonNull) |
| 103 | .forEach(flowOpsBuilder::remove); |
| 104 | break; |
| 105 | default: |
| 106 | fail(forwardObjective, ObjectiveError.UNKNOWN); |
| 107 | log.warn("Unknown forwarding type {}"); |
| 108 | } |
| 109 | |
| 110 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 111 | @Override |
| 112 | public void onSuccess(FlowRuleOperations ops) { |
| 113 | pass(forwardObjective); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onError(FlowRuleOperations ops) { |
| 118 | fail(forwardObjective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 119 | } |
| 120 | })); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 125 | |
| 126 | log.debug("Processing forwarding object"); |
| 127 | |
| 128 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 129 | .forDevice(deviceId) |
| 130 | .withSelector(fwd.selector()) |
| 131 | .withTreatment(fwd.treatment()) |
| 132 | .withPriority(fwd.priority()) |
| 133 | .fromApp(fwd.appId()) |
| 134 | .forTable(SOFTWARE_TABLE_START); |
| 135 | |
| 136 | if (fwd.permanent()) { |
| 137 | ruleBuilder.makePermanent(); |
| 138 | } else { |
| 139 | ruleBuilder.makeTemporary(TIME_OUT); |
| 140 | } |
| 141 | |
| 142 | return Collections.singletonList(ruleBuilder.build()); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | @Override |
| 148 | public void next(NextObjective nextObjective) { |
| 149 | // Do nothing |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 154 | // TODO Implementation deferred to vendor |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | private void pass(Objective obj) { |
| 159 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
| 160 | } |
| 161 | |
| 162 | private void fail(Objective obj, ObjectiveError error) { |
| 163 | obj.context().ifPresent(context -> context.onError(obj, error)); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | } |