blob: 055a4befbbe0d50bbf80b023af616a55287a5e92 [file] [log] [blame]
Daniele Moro8d630f12021-06-15 20:53:22 +02001/*
2 * Copyright 2021-present Open Networking Foundation
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.pipelines.fabric.impl.behaviour.upf;
18
19import com.google.common.collect.Sets;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.flow.DefaultFlowEntry;
23import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleOperations;
26import org.onosproject.net.flow.FlowRuleServiceAdapter;
27
28import java.util.Set;
29import java.util.concurrent.atomic.AtomicBoolean;
30import java.util.stream.Collectors;
31
32public class MockFlowRuleService extends FlowRuleServiceAdapter {
33
34 final Set<FlowRule> flows = Sets.newHashSet();
35 boolean success;
36
37 int errorFlow = -1;
38
39 public void setErrorFlow(int errorFlow) {
40 this.errorFlow = errorFlow;
41 }
42
43 public void setFuture(boolean success) {
44 this.success = success;
45 }
46
47 @Override
48 public void apply(FlowRuleOperations ops) {
49 AtomicBoolean thisSuccess = new AtomicBoolean(success);
50 ops.stages().forEach(stage -> stage.forEach(flow -> {
51 if (errorFlow == flow.rule().id().value()) {
52 thisSuccess.set(false);
53 } else {
54 switch (flow.type()) {
55 case ADD:
56 case MODIFY: //TODO is this the right behavior for modify?
57 flows.add(flow.rule());
58 break;
59 case REMOVE:
60 flows.remove(flow.rule());
61 break;
62 default:
63 break;
64 }
65 }
66 }));
67 if (thisSuccess.get()) {
68 ops.callback().onSuccess(ops);
69 } else {
70 ops.callback().onError(ops);
71 }
72 }
73
74 @Override
75 public int getFlowRuleCount() {
76 return flows.size();
77 }
78
79 @Override
80 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
81 return flows.stream()
82 .filter(flow -> flow.deviceId().equals(deviceId))
83 .map(DefaultFlowEntry::new)
84 .collect(Collectors.toList());
85 }
86
87 @Override
88 public void applyFlowRules(FlowRule... flowRules) {
89 for (FlowRule flow : flowRules) {
90 flows.add(flow);
91 }
92 }
93
94 @Override
95 public void removeFlowRules(FlowRule... flowRules) {
96 for (FlowRule flow : flowRules) {
97 flows.remove(flow);
98 }
99 }
100
101 @Override
102 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
103 return flows.stream()
104 .filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
105 .collect(Collectors.toList());
106 }
107
108 @Override
109 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
110 return flows.stream()
111 .filter(flow -> flow.appId() == id.id())
112 .map(DefaultFlowEntry::new)
113 .collect(Collectors.toList());
114 }
115}
116
117