implementation of NullFlowProvider

Change-Id: Ieeb1006fac2aa14e7f225b7efe95d76fc485d56e
diff --git a/providers/null/device/src/main/java/org/onosproject/provider/nil/device/impl/NullDeviceProvider.java b/providers/null/device/src/main/java/org/onosproject/provider/nil/device/impl/NullDeviceProvider.java
index b9e50ba..5143525 100644
--- a/providers/null/device/src/main/java/org/onosproject/provider/nil/device/impl/NullDeviceProvider.java
+++ b/providers/null/device/src/main/java/org/onosproject/provider/nil/device/impl/NullDeviceProvider.java
@@ -41,6 +41,8 @@
 import org.onosproject.net.provider.ProviderId;
 import org.slf4j.Logger;
 
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
@@ -49,6 +51,7 @@
 
 import static org.onlab.util.Tools.delay;
 import static org.onlab.util.Tools.namedThreads;
+import static org.onlab.util.Tools.toHex;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -74,7 +77,7 @@
 
 
     //currently hardcoded. will be made configurable via rest/cli.
-    private static final String SCHEME = null;
+    private static final String SCHEME = "null";
     private static final int NUMDEVICES = 10;
     private static final int NUMPORTSPERDEVICE = 10;
 
@@ -142,7 +145,11 @@
         @Override
         public void run() {
             if (setup) {
-                advertiseDevices();
+                try {
+                    advertiseDevices();
+                } catch (URISyntaxException e) {
+                    log.warn("URI creation failed during device adverts {}", e.getMessage());
+                }
             } else {
                 removeDevices();
             }
@@ -157,7 +164,7 @@
             descriptions.clear();
         }
 
-        private void advertiseDevices() {
+        private void advertiseDevices() throws URISyntaxException {
             DeviceId did;
             ChassisId cid;
 
@@ -165,7 +172,7 @@
             int nodeIdHash = (clusterService.getLocalNode().hashCode() % NUMDEVICES) * NUMDEVICES;
 
             for (int i = nodeIdHash; i < nodeIdHash + NUMDEVICES; i++) {
-                did = DeviceId.deviceId(String.format("%s:%d", SCHEME, i));
+                did = DeviceId.deviceId(new URI(SCHEME, toHex(i), null));
                 cid = new ChassisId(i);
                 DeviceDescription desc =
                         new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH,
diff --git a/providers/null/flow/src/main/java/org/onosproject/provider/nil/flow/impl/NullFlowRuleProvider.java b/providers/null/flow/src/main/java/org/onosproject/provider/nil/flow/impl/NullFlowRuleProvider.java
new file mode 100644
index 0000000..b021ab0
--- /dev/null
+++ b/providers/null/flow/src/main/java/org/onosproject/provider/nil/flow/impl/NullFlowRuleProvider.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.nil.flow.impl;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.util.concurrent.Futures;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.jboss.netty.util.HashedWheelTimer;
+import org.jboss.netty.util.Timeout;
+import org.jboss.netty.util.TimerTask;
+import org.onlab.util.Timer;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.BatchOperation;
+import org.onosproject.net.flow.CompletedBatchOperation;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleBatchEntry;
+import org.onosproject.net.flow.FlowRuleProvider;
+import org.onosproject.net.flow.FlowRuleProviderRegistry;
+import org.onosproject.net.flow.FlowRuleProviderService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.slf4j.Logger;
+
+import java.util.Collections;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Null provider to accept any flow and report them.
+ */
+@Component(immediate = true)
+public class NullFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleProviderRegistry providerRegistry;
+
+    private Multimap<DeviceId, FlowEntry> flowTable = HashMultimap.create();
+
+    private FlowRuleProviderService providerService;
+
+    private HashedWheelTimer timer = Timer.getTimer();
+    private Timeout timeout;
+
+    public NullFlowRuleProvider() {
+        super(new ProviderId("null", "org.onosproject.provider.nil"));
+    }
+
+    @Activate
+    public void activate() {
+        providerService = providerRegistry.register(this);
+        timeout = timer.newTimeout(new StatisticTask(), 5, TimeUnit.SECONDS);
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        providerRegistry.unregister(this);
+        providerService = null;
+        timeout.cancel();
+
+        log.info("Stopped");
+    }
+
+    @Override
+    public void applyFlowRule(FlowRule... flowRules) {
+        for (int i = 0; i < flowRules.length; i++) {
+            flowTable.put(flowRules[i].deviceId(), new DefaultFlowEntry(flowRules[i]));
+        }
+    }
+
+    @Override
+    public void removeFlowRule(FlowRule... flowRules) {
+        for (int i = 0; i < flowRules.length; i++) {
+            flowTable.remove(flowRules[i].deviceId(), flowRules[i]);
+        }
+    }
+
+    @Override
+    public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
+        log.info("removal by app id not supported in null provider");
+    }
+
+    @Override
+    public Future<CompletedBatchOperation> executeBatch(
+            BatchOperation<FlowRuleBatchEntry> batch) {
+        for (FlowRuleBatchEntry fbe : batch.getOperations()) {
+            switch (fbe.getOperator()) {
+                case ADD:
+                    applyFlowRule(fbe.getTarget());
+                    break;
+                case REMOVE:
+                    removeFlowRule(fbe.getTarget());
+                    break;
+                case MODIFY:
+                    removeFlowRule(fbe.getTarget());
+                    applyFlowRule(fbe.getTarget());
+                    break;
+                default:
+                    log.error("Unknown flow operation: {}", fbe);
+            }
+        }
+        return Futures.immediateFuture(
+                new CompletedBatchOperation(true, Collections.emptySet()));
+    }
+
+    private class StatisticTask implements TimerTask {
+
+        @Override
+        public void run(Timeout to) throws Exception {
+            for (DeviceId devId : flowTable.keySet()) {
+                providerService.pushFlowMetrics(devId, flowTable.get(devId));
+            }
+
+            timeout = timer.newTimeout(to.getTask(), 5, TimeUnit.SECONDS);
+        }
+    }
+}