blob: cbfe3ef665fe8ada56c340b34a8e721434c2abe9 [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskac40d4632015-04-09 16:55:03 -07003 *
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.provider.nil;
17
18import com.google.common.collect.Sets;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070019import io.netty.util.Timeout;
20import io.netty.util.TimerTask;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070021import org.onlab.util.Timer;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070022import org.onosproject.net.DeviceId;
23import org.onosproject.net.flow.CompletedBatchOperation;
24import org.onosproject.net.flow.DefaultFlowEntry;
25import org.onosproject.net.flow.FlowEntry;
26import org.onosproject.net.flow.FlowRule;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070027import org.onosproject.net.flow.FlowRuleProvider;
28import org.onosproject.net.flow.FlowRuleProviderService;
Ray Milkey17801b42019-02-22 14:18:00 -080029import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
30import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070031import org.slf4j.Logger;
32
33import java.util.Collections;
34import java.util.Set;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.ConcurrentMap;
37import java.util.concurrent.TimeUnit;
38
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Null provider to accept any flow and report them.
43 */
44class NullFlowRuleProvider extends NullProviders.AbstractNullProvider
45 implements FlowRuleProvider {
46
47 private final Logger log = getLogger(getClass());
48
49 private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>();
50
51 private FlowRuleProviderService providerService;
52
Thomas Vachuskac40d4632015-04-09 16:55:03 -070053 private Timeout timeout;
54
55 /**
56 * Starts the flow rule provider simulation.
57 *
58 * @param providerService flow rule provider service
59 */
60 void start(FlowRuleProviderService providerService) {
61 this.providerService = providerService;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070062 timeout = Timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS);
Thomas Vachuskac40d4632015-04-09 16:55:03 -070063 }
64
65 /**
66 * Stops the flow rule provider simulation.
67 */
68 void stop() {
69 timeout.cancel();
70 }
71
72 @Override
73 public void applyFlowRule(FlowRule... flowRules) {
Thomas Vachuskafa615492018-03-19 09:11:51 -070074 for (FlowRule rule : flowRules) {
75 flowTable.getOrDefault(rule.deviceId(), Sets.newConcurrentHashSet())
76 .add(new DefaultFlowEntry(rule));
77 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -070078 }
79
80 @Override
81 public void removeFlowRule(FlowRule... flowRules) {
Thomas Vachuskafa615492018-03-19 09:11:51 -070082 for (FlowRule rule : flowRules) {
83 flowTable.getOrDefault(rule.deviceId(), Sets.newConcurrentHashSet())
84 .remove(new DefaultFlowEntry(rule));
85 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -070086 }
87
88 @Override
Thomas Vachuskac40d4632015-04-09 16:55:03 -070089 public void executeBatch(FlowRuleBatchOperation batch) {
90 // TODO: consider checking mastership
91 Set<FlowEntry> entries =
92 flowTable.getOrDefault(batch.deviceId(),
93 Sets.newConcurrentHashSet());
94 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
95 switch (fbe.operator()) {
96 case ADD:
97 entries.add(new DefaultFlowEntry(fbe.target()));
98 break;
99 case REMOVE:
100 entries.remove(new DefaultFlowEntry(fbe.target()));
101 break;
102 case MODIFY:
103 FlowEntry entry = new DefaultFlowEntry(fbe.target());
104 entries.remove(entry);
105 entries.add(entry);
106 break;
107 default:
108 log.error("Unknown flow operation: {}", fbe);
109 }
110 }
111 flowTable.put(batch.deviceId(), entries);
112 CompletedBatchOperation op =
113 new CompletedBatchOperation(true, Collections.emptySet(),
114 batch.deviceId());
115 providerService.batchOperationCompleted(batch.id(), op);
116 }
117
118 // Periodically reports flow rule statistics.
119 private class StatisticTask implements TimerTask {
120 @Override
121 public void run(Timeout to) throws Exception {
122 for (DeviceId devId : flowTable.keySet()) {
123 Set<FlowEntry> entries =
124 flowTable.getOrDefault(devId, Collections.emptySet());
125 providerService.pushFlowMetrics(devId, entries);
126 }
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700127 timeout = to.timer().newTimeout(to.task(), 5, TimeUnit.SECONDS);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700128 }
129 }
130}