alshabib
Committed by Gerrit Code Review

implementation of NullFlowProvider

Change-Id: Ieeb1006fac2aa14e7f225b7efe95d76fc485d56e
...@@ -41,6 +41,8 @@ import org.onosproject.net.provider.AbstractProvider; ...@@ -41,6 +41,8 @@ import org.onosproject.net.provider.AbstractProvider;
41 import org.onosproject.net.provider.ProviderId; 41 import org.onosproject.net.provider.ProviderId;
42 import org.slf4j.Logger; 42 import org.slf4j.Logger;
43 43
44 +import java.net.URI;
45 +import java.net.URISyntaxException;
44 import java.util.List; 46 import java.util.List;
45 import java.util.Map; 47 import java.util.Map;
46 import java.util.concurrent.ExecutorService; 48 import java.util.concurrent.ExecutorService;
...@@ -49,6 +51,7 @@ import java.util.concurrent.TimeUnit; ...@@ -49,6 +51,7 @@ import java.util.concurrent.TimeUnit;
49 51
50 import static org.onlab.util.Tools.delay; 52 import static org.onlab.util.Tools.delay;
51 import static org.onlab.util.Tools.namedThreads; 53 import static org.onlab.util.Tools.namedThreads;
54 +import static org.onlab.util.Tools.toHex;
52 import static org.slf4j.LoggerFactory.getLogger; 55 import static org.slf4j.LoggerFactory.getLogger;
53 56
54 /** 57 /**
...@@ -74,7 +77,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid ...@@ -74,7 +77,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid
74 77
75 78
76 //currently hardcoded. will be made configurable via rest/cli. 79 //currently hardcoded. will be made configurable via rest/cli.
77 - private static final String SCHEME = null; 80 + private static final String SCHEME = "null";
78 private static final int NUMDEVICES = 10; 81 private static final int NUMDEVICES = 10;
79 private static final int NUMPORTSPERDEVICE = 10; 82 private static final int NUMPORTSPERDEVICE = 10;
80 83
...@@ -142,7 +145,11 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid ...@@ -142,7 +145,11 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid
142 @Override 145 @Override
143 public void run() { 146 public void run() {
144 if (setup) { 147 if (setup) {
148 + try {
145 advertiseDevices(); 149 advertiseDevices();
150 + } catch (URISyntaxException e) {
151 + log.warn("URI creation failed during device adverts {}", e.getMessage());
152 + }
146 } else { 153 } else {
147 removeDevices(); 154 removeDevices();
148 } 155 }
...@@ -157,7 +164,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid ...@@ -157,7 +164,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid
157 descriptions.clear(); 164 descriptions.clear();
158 } 165 }
159 166
160 - private void advertiseDevices() { 167 + private void advertiseDevices() throws URISyntaxException {
161 DeviceId did; 168 DeviceId did;
162 ChassisId cid; 169 ChassisId cid;
163 170
...@@ -165,7 +172,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid ...@@ -165,7 +172,7 @@ public class NullDeviceProvider extends AbstractProvider implements DeviceProvid
165 int nodeIdHash = (clusterService.getLocalNode().hashCode() % NUMDEVICES) * NUMDEVICES; 172 int nodeIdHash = (clusterService.getLocalNode().hashCode() % NUMDEVICES) * NUMDEVICES;
166 173
167 for (int i = nodeIdHash; i < nodeIdHash + NUMDEVICES; i++) { 174 for (int i = nodeIdHash; i < nodeIdHash + NUMDEVICES; i++) {
168 - did = DeviceId.deviceId(String.format("%s:%d", SCHEME, i)); 175 + did = DeviceId.deviceId(new URI(SCHEME, toHex(i), null));
169 cid = new ChassisId(i); 176 cid = new ChassisId(i);
170 DeviceDescription desc = 177 DeviceDescription desc =
171 new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH, 178 new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
......
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 +}