blob: 068634939afc67ed3fcce845606e524ebdf10be0 [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
Brian O'Connor72cb19a2015-01-16 16:14:41 -080018import com.google.common.collect.Sets;
alshabib14233372015-01-21 13:45:25 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.jboss.netty.util.HashedWheelTimer;
25import org.jboss.netty.util.Timeout;
26import org.jboss.netty.util.TimerTask;
27import org.onlab.util.Timer;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.net.DeviceId;
alshabib14233372015-01-21 13:45:25 -080030import org.onosproject.net.flow.CompletedBatchOperation;
31import org.onosproject.net.flow.DefaultFlowEntry;
32import org.onosproject.net.flow.FlowEntry;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080035import org.onosproject.net.flow.FlowRuleBatchOperation;
alshabib14233372015-01-21 13:45:25 -080036import org.onosproject.net.flow.FlowRuleProvider;
37import org.onosproject.net.flow.FlowRuleProviderRegistry;
38import org.onosproject.net.flow.FlowRuleProviderService;
39import org.onosproject.net.provider.AbstractProvider;
40import org.onosproject.net.provider.ProviderId;
41import org.slf4j.Logger;
42
43import java.util.Collections;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080044import java.util.Set;
45import java.util.concurrent.ConcurrentHashMap;
46import java.util.concurrent.ConcurrentMap;
alshabib14233372015-01-21 13:45:25 -080047import 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
Brian O'Connor72cb19a2015-01-16 16:14:41 -080062 private ConcurrentMap<DeviceId, Set<FlowEntry>> flowTable = new ConcurrentHashMap<>();
alshabib14233372015-01-21 13:45:25 -080063
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'Connor72cb19a2015-01-16 16:14:41 -080091 public void applyFlowRule(FlowRule... flowRules) {}
alshabib14233372015-01-21 13:45:25 -080092
93 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -080094 public void removeFlowRule(FlowRule... flowRules) {}
alshabib14233372015-01-21 13:45:25 -080095
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'Connor72cb19a2015-01-16 16:14:41 -0800102 public void executeBatch(
103 FlowRuleBatchOperation batch) {
104 Set<FlowEntry> flowRules = flowTable.getOrDefault(batch.deviceId(), Sets.newConcurrentHashSet());
alshabib14233372015-01-21 13:45:25 -0800105 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800106 switch (fbe.operator()) {
alshabib14233372015-01-21 13:45:25 -0800107 case ADD:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800108 flowRules.add(new DefaultFlowEntry(fbe.target()));
alshabib14233372015-01-21 13:45:25 -0800109 break;
110 case REMOVE:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800111 flowRules.remove(new DefaultFlowEntry(fbe.target()));
alshabib14233372015-01-21 13:45:25 -0800112 break;
113 case MODIFY:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800114 FlowEntry entry = new DefaultFlowEntry(fbe.target());
115 flowRules.remove(entry);
116 flowRules.add(entry);
alshabib14233372015-01-21 13:45:25 -0800117 break;
118 default:
119 log.error("Unknown flow operation: {}", fbe);
120 }
121 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 flowTable.put(batch.deviceId(), flowRules);
123 providerService.batchOperationCompleted(batch.id(),
124 new CompletedBatchOperation(
125 true,
126 Collections.emptySet(),
127 batch.deviceId()));
alshabib14233372015-01-21 13:45:25 -0800128 }
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'Connor72cb19a2015-01-16 16:14:41 -0800135 providerService.pushFlowMetrics(devId,
136 flowTable.getOrDefault(devId, Collections.emptySet()));
alshabib14233372015-01-21 13:45:25 -0800137 }
alshabib14233372015-01-21 13:45:25 -0800138 timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800139
alshabib14233372015-01-21 13:45:25 -0800140 }
141 }
142}