alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [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.flow.impl; |
| 17 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.jboss.netty.util.HashedWheelTimer; |
| 25 | import org.jboss.netty.util.Timeout; |
| 26 | import org.jboss.netty.util.TimerTask; |
| 27 | import org.onlab.util.Timer; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.net.DeviceId; |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 31 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 32 | import org.onosproject.net.flow.FlowEntry; |
| 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowRuleProvider; |
| 37 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 38 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 39 | import org.onosproject.net.provider.AbstractProvider; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Collections; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 44 | import java.util.Set; |
| 45 | import java.util.concurrent.ConcurrentHashMap; |
| 46 | import java.util.concurrent.ConcurrentMap; |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
| 48 | |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * Null provider to accept any flow and report them. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | public class NullFlowRuleProvider extends AbstractProvider implements FlowRuleProvider { |
| 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected FlowRuleProviderRegistry providerRegistry; |
| 61 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 62 | private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>(); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 63 | |
| 64 | private FlowRuleProviderService providerService; |
| 65 | |
| 66 | private HashedWheelTimer timer = Timer.getTimer(); |
| 67 | private Timeout timeout; |
| 68 | |
| 69 | public NullFlowRuleProvider() { |
| 70 | super(new ProviderId("null", "org.onosproject.provider.nil")); |
| 71 | } |
| 72 | |
| 73 | @Activate |
| 74 | public void activate() { |
| 75 | providerService = providerRegistry.register(this); |
| 76 | timeout = timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS); |
| 77 | |
| 78 | log.info("Started"); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | public void deactivate() { |
| 83 | providerRegistry.unregister(this); |
| 84 | providerService = null; |
| 85 | timeout.cancel(); |
| 86 | |
| 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 91 | public void applyFlowRule(FlowRule... flowRules) {} |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 92 | |
| 93 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 94 | public void removeFlowRule(FlowRule... flowRules) {} |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 95 | |
| 96 | @Override |
| 97 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 98 | log.info("removal by app id not supported in null provider"); |
| 99 | } |
| 100 | |
| 101 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 102 | public void executeBatch( |
| 103 | FlowRuleBatchOperation batch) { |
| 104 | Set<FlowEntry> flowRules = flowTable.getOrDefault(batch.deviceId(), Sets.newConcurrentHashSet()); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 105 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 106 | switch (fbe.operator()) { |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 107 | case ADD: |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 108 | flowRules.add(new DefaultFlowEntry(fbe.target())); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 109 | break; |
| 110 | case REMOVE: |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 111 | flowRules.remove(new DefaultFlowEntry(fbe.target())); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 112 | break; |
| 113 | case MODIFY: |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 114 | FlowEntry entry = new DefaultFlowEntry(fbe.target()); |
| 115 | flowRules.remove(entry); |
| 116 | flowRules.add(entry); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | log.error("Unknown flow operation: {}", fbe); |
| 120 | } |
| 121 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 122 | flowTable.put(batch.deviceId(), flowRules); |
| 123 | providerService.batchOperationCompleted(batch.id(), |
| 124 | new CompletedBatchOperation( |
| 125 | true, |
| 126 | Collections.emptySet(), |
| 127 | batch.deviceId())); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private class StatisticTask implements TimerTask { |
| 131 | |
| 132 | @Override |
| 133 | public void run(Timeout to) throws Exception { |
| 134 | for (DeviceId devId : flowTable.keySet()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 135 | providerService.pushFlowMetrics(devId, |
| 136 | flowTable.getOrDefault(devId, Collections.emptySet())); |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 137 | } |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 138 | timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 139 | |
alshabib | 1423337 | 2015-01-21 13:45:25 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |