blob: b021ab0fad10aec8801e23c1be852dc96a00b18c [file] [log] [blame]
alshabib14233372015-01-21 13:45:25 -08001/*
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 */
16package org.onosproject.provider.nil.flow.impl;
17
18import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Multimap;
20import com.google.common.util.concurrent.Futures;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.jboss.netty.util.HashedWheelTimer;
27import org.jboss.netty.util.Timeout;
28import org.jboss.netty.util.TimerTask;
29import org.onlab.util.Timer;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.flow.BatchOperation;
33import org.onosproject.net.flow.CompletedBatchOperation;
34import org.onosproject.net.flow.DefaultFlowEntry;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.FlowRuleBatchEntry;
38import org.onosproject.net.flow.FlowRuleProvider;
39import org.onosproject.net.flow.FlowRuleProviderRegistry;
40import org.onosproject.net.flow.FlowRuleProviderService;
41import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.slf4j.Logger;
44
45import java.util.Collections;
46import java.util.concurrent.Future;
47import java.util.concurrent.TimeUnit;
48
49import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Null provider to accept any flow and report them.
53 */
54@Component(immediate = true)
55public 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}