blob: 98cb848ff64465ee756aa3e117dbc69ee3fda53f [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 -070019
20import io.netty.util.Timeout;
21import io.netty.util.TimerTask;
22
Thomas Vachuskac40d4632015-04-09 16:55:03 -070023import org.onlab.util.Timer;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.flow.CompletedBatchOperation;
27import org.onosproject.net.flow.DefaultFlowEntry;
28import org.onosproject.net.flow.FlowEntry;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.FlowRuleBatchEntry;
31import org.onosproject.net.flow.FlowRuleBatchOperation;
32import org.onosproject.net.flow.FlowRuleProvider;
33import org.onosproject.net.flow.FlowRuleProviderService;
34import org.slf4j.Logger;
35
36import java.util.Collections;
37import java.util.Set;
38import java.util.concurrent.ConcurrentHashMap;
39import java.util.concurrent.ConcurrentMap;
40import java.util.concurrent.TimeUnit;
41
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Null provider to accept any flow and report them.
46 */
47class NullFlowRuleProvider extends NullProviders.AbstractNullProvider
48 implements FlowRuleProvider {
49
50 private final Logger log = getLogger(getClass());
51
52 private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>();
53
54 private FlowRuleProviderService providerService;
55
Thomas Vachuskac40d4632015-04-09 16:55:03 -070056 private Timeout timeout;
57
58 /**
59 * Starts the flow rule provider simulation.
60 *
61 * @param providerService flow rule provider service
62 */
63 void start(FlowRuleProviderService providerService) {
64 this.providerService = providerService;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070065 timeout = Timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS);
Thomas Vachuskac40d4632015-04-09 16:55:03 -070066 }
67
68 /**
69 * Stops the flow rule provider simulation.
70 */
71 void stop() {
72 timeout.cancel();
73 }
74
75 @Override
76 public void applyFlowRule(FlowRule... flowRules) {
77 // FIXME: invoke executeBatch
78 }
79
80 @Override
81 public void removeFlowRule(FlowRule... flowRules) {
82 // FIXME: invoke executeBatch
83 }
84
85 @Override
86 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
87 throw new UnsupportedOperationException("Cannot remove by appId from null provider");
88 }
89
90 @Override
91 public void executeBatch(FlowRuleBatchOperation batch) {
92 // TODO: consider checking mastership
93 Set<FlowEntry> entries =
94 flowTable.getOrDefault(batch.deviceId(),
95 Sets.newConcurrentHashSet());
96 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
97 switch (fbe.operator()) {
98 case ADD:
99 entries.add(new DefaultFlowEntry(fbe.target()));
100 break;
101 case REMOVE:
102 entries.remove(new DefaultFlowEntry(fbe.target()));
103 break;
104 case MODIFY:
105 FlowEntry entry = new DefaultFlowEntry(fbe.target());
106 entries.remove(entry);
107 entries.add(entry);
108 break;
109 default:
110 log.error("Unknown flow operation: {}", fbe);
111 }
112 }
113 flowTable.put(batch.deviceId(), entries);
114 CompletedBatchOperation op =
115 new CompletedBatchOperation(true, Collections.emptySet(),
116 batch.deviceId());
117 providerService.batchOperationCompleted(batch.id(), op);
118 }
119
120 // Periodically reports flow rule statistics.
121 private class StatisticTask implements TimerTask {
122 @Override
123 public void run(Timeout to) throws Exception {
124 for (DeviceId devId : flowTable.keySet()) {
125 Set<FlowEntry> entries =
126 flowTable.getOrDefault(devId, Collections.emptySet());
127 providerService.pushFlowMetrics(devId, entries);
128 }
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700129 timeout = to.timer().newTimeout(to.task(), 5, TimeUnit.SECONDS);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700130 }
131 }
132}