Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -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.provider.nil; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 19 | import io.netty.util.Timeout; |
| 20 | import io.netty.util.TimerTask; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 21 | import org.onlab.util.Timer; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 24 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 25 | import org.onosproject.net.flow.FlowEntry; |
| 26 | import org.onosproject.net.flow.FlowRule; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRuleProvider; |
| 28 | import org.onosproject.net.flow.FlowRuleProviderService; |
Ray Milkey | 17801b4 | 2019-02-22 14:18:00 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 30 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.util.Collections; |
| 34 | import java.util.Set; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | import java.util.concurrent.ConcurrentMap; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Null provider to accept any flow and report them. |
| 43 | */ |
| 44 | class 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 Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 53 | 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 HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 62 | timeout = Timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 63 | } |
| 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 Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 74 | for (FlowRule rule : flowRules) { |
| 75 | flowTable.getOrDefault(rule.deviceId(), Sets.newConcurrentHashSet()) |
| 76 | .add(new DefaultFlowEntry(rule)); |
| 77 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void removeFlowRule(FlowRule... flowRules) { |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 82 | for (FlowRule rule : flowRules) { |
| 83 | flowTable.getOrDefault(rule.deviceId(), Sets.newConcurrentHashSet()) |
| 84 | .remove(new DefaultFlowEntry(rule)); |
| 85 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 89 | 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 HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 127 | timeout = to.timer().newTimeout(to.task(), 5, TimeUnit.SECONDS); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |