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 | |
| 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Multimap; |
| 20 | import com.google.common.util.concurrent.Futures; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.jboss.netty.util.HashedWheelTimer; |
| 27 | import org.jboss.netty.util.Timeout; |
| 28 | import org.jboss.netty.util.TimerTask; |
| 29 | import org.onlab.util.Timer; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.flow.BatchOperation; |
| 33 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 34 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 35 | import org.onosproject.net.flow.FlowEntry; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 38 | import org.onosproject.net.flow.FlowRuleProvider; |
| 39 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 40 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 41 | import org.onosproject.net.provider.AbstractProvider; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | import java.util.Collections; |
| 46 | import java.util.concurrent.Future; |
| 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 | |
| 62 | private Multimap<DeviceId, FlowEntry> flowTable = HashMultimap.create(); |
| 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 |
| 91 | public void applyFlowRule(FlowRule... flowRules) { |
| 92 | for (int i = 0; i < flowRules.length; i++) { |
| 93 | flowTable.put(flowRules[i].deviceId(), new DefaultFlowEntry(flowRules[i])); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void removeFlowRule(FlowRule... flowRules) { |
| 99 | for (int i = 0; i < flowRules.length; i++) { |
| 100 | flowTable.remove(flowRules[i].deviceId(), flowRules[i]); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 106 | log.info("removal by app id not supported in null provider"); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Future<CompletedBatchOperation> executeBatch( |
| 111 | BatchOperation<FlowRuleBatchEntry> batch) { |
| 112 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 113 | switch (fbe.getOperator()) { |
| 114 | case ADD: |
| 115 | applyFlowRule(fbe.getTarget()); |
| 116 | break; |
| 117 | case REMOVE: |
| 118 | removeFlowRule(fbe.getTarget()); |
| 119 | break; |
| 120 | case MODIFY: |
| 121 | removeFlowRule(fbe.getTarget()); |
| 122 | applyFlowRule(fbe.getTarget()); |
| 123 | break; |
| 124 | default: |
| 125 | log.error("Unknown flow operation: {}", fbe); |
| 126 | } |
| 127 | } |
| 128 | return Futures.immediateFuture( |
| 129 | new CompletedBatchOperation(true, Collections.emptySet())); |
| 130 | } |
| 131 | |
| 132 | private class StatisticTask implements TimerTask { |
| 133 | |
| 134 | @Override |
| 135 | public void run(Timeout to) throws Exception { |
| 136 | for (DeviceId devId : flowTable.keySet()) { |
| 137 | providerService.pushFlowMetrics(devId, flowTable.get(devId)); |
| 138 | } |
| 139 | |
| 140 | timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS); |
| 141 | } |
| 142 | } |
| 143 | } |