Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.provider.nil; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.jboss.netty.util.HashedWheelTimer; |
| 20 | import org.jboss.netty.util.Timeout; |
| 21 | import org.jboss.netty.util.TimerTask; |
| 22 | import org.onlab.util.Timer; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 26 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 27 | import org.onosproject.net.flow.FlowEntry; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 30 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 31 | import org.onosproject.net.flow.FlowRuleProvider; |
| 32 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Collections; |
| 36 | import java.util.Set; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | import java.util.concurrent.ConcurrentMap; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Null provider to accept any flow and report them. |
| 45 | */ |
| 46 | class NullFlowRuleProvider extends NullProviders.AbstractNullProvider |
| 47 | implements FlowRuleProvider { |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
| 51 | private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>(); |
| 52 | |
| 53 | private FlowRuleProviderService providerService; |
| 54 | |
| 55 | private HashedWheelTimer timer = Timer.getTimer(); |
| 56 | 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; |
| 65 | timeout = timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS); |
| 66 | } |
| 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 | } |
| 129 | timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS); |
| 130 | } |
| 131 | } |
| 132 | } |