blob: 0e127f72c8e59282562801579d3473605a719886 [file] [log] [blame]
Murat Parlakisik4f50e3f2016-04-11 03:24:44 -07001/*
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.pipeline;
17
18
19import org.onlab.osgi.ServiceDirectory;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.behaviour.NextGroup;
24import org.onosproject.net.behaviour.Pipeliner;
25import org.onosproject.net.behaviour.PipelinerContext;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.flow.DefaultFlowRule;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.FlowRuleOperations;
30import org.onosproject.net.flow.FlowRuleOperationsContext;
31import org.onosproject.net.flow.FlowRuleService;
32import org.onosproject.net.flowobjective.FilteringObjective;
33import org.onosproject.net.flowobjective.FlowObjectiveStore;
34import org.onosproject.net.flowobjective.ForwardingObjective;
35import org.onosproject.net.flowobjective.NextObjective;
36import org.onosproject.net.flowobjective.Objective;
37import org.onosproject.net.flowobjective.ObjectiveError;
38
39import org.slf4j.Logger;
40import java.util.Collection;
41import java.util.Collections;
42import java.util.List;
43import java.util.Objects;
44
45
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Driver for Hp. Default table starts from 200.
50 */
51public 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}