[ONOS-7592] Add NullGroupProvider

Change-Id: I7843f235951ce7f7be1345a9c8ff52f7ca88dabc
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/NullGroupProvider.java b/providers/null/src/main/java/org/onosproject/provider/nil/NullGroupProvider.java
new file mode 100644
index 0000000..3cf175c
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/NullGroupProvider.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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;
+
+import io.netty.util.Timeout;
+import io.netty.util.TimerTask;
+import org.onlab.util.Timer;
+import org.onosproject.core.GroupId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.group.DefaultGroup;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupOperation;
+import org.onosproject.net.group.GroupOperations;
+import org.onosproject.net.group.GroupProvider;
+import org.onosproject.net.group.GroupProviderService;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Null provider to accept any group and report them.
+ */
+public class NullGroupProvider extends NullProviders.AbstractNullProvider
+        implements GroupProvider {
+
+    private static final long DEFAULT_POLL_DELAY = 1;
+
+    private final Logger log = getLogger(getClass());
+    private final Map<DeviceId, Map<GroupId, Group>> groupTables = new ConcurrentHashMap<>();
+    private GroupProviderService providerService;
+    private Timeout timeout;
+
+    /**
+     * Start group provider.
+     *
+     * @param providerService group provider service
+     */
+    void start(GroupProviderService providerService) {
+        this.providerService = providerService;
+        timeout = Timer.newTimeout(new StatisticTask(), DEFAULT_POLL_DELAY, TimeUnit.SECONDS);
+    }
+
+    /**
+     * Stops the group provider simulation.
+     */
+    void stop() {
+        timeout.cancel();
+    }
+
+    public void initDevicesGroupTable(Collection<DeviceId> deviceIds) {
+        // Initialize group table for the device
+        deviceIds.forEach(deviceId -> groupTables.put(deviceId, new ConcurrentHashMap<>()));
+        pushGroupMetrics();
+    }
+
+    @Override
+    public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
+        groupOps.operations().forEach(go -> {
+            switch (go.opType()) {
+                case ADD:
+                    groupAdd(deviceId, go);
+                    break;
+                case MODIFY:
+                    groupModify(deviceId, go);
+                    break;
+                case DELETE:
+                    groupDelete(deviceId, go);
+                    break;
+                default:
+                    log.warn("Unsupported op type: {}", go.opType());
+                    break;
+            }
+        });
+    }
+
+    private void groupAdd(DeviceId deviceId, GroupOperation go) {
+        GroupId gid = go.groupId();
+        DefaultGroup group = new DefaultGroup(gid, deviceId, go.groupType(), go.buckets());
+        group.setState(Group.GroupState.ADDED);
+        groupTables.get(deviceId).put(gid, group);
+    }
+
+    private void groupModify(DeviceId deviceId, GroupOperation go) {
+        groupTables.get(deviceId).computeIfPresent(go.groupId(), (gid, group) -> {
+            DefaultGroup grp = new DefaultGroup(gid, deviceId, go.groupType(), go.buckets());
+            grp.setState(Group.GroupState.ADDED);
+            return grp;
+        });
+    }
+
+    private void groupDelete(DeviceId deviceId, GroupOperation go) {
+        groupTables.get(deviceId).remove(go.groupId());
+    }
+
+    private void pushGroupMetrics() {
+        groupTables.forEach((deviceId, groups) -> {
+            providerService.pushGroupMetrics(deviceId, groups.values());
+        });
+    }
+
+    // Periodically reports flow rule statistics.
+    private class StatisticTask implements TimerTask {
+        @Override
+        public void run(Timeout to) {
+            pushGroupMetrics();
+            timeout = to.timer().newTimeout(to.task(), DEFAULT_POLL_DELAY, TimeUnit.SECONDS);
+        }
+    }
+}
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java b/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
index 83471a5..7c45df3 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/NullProviders.java
@@ -39,6 +39,8 @@
 import org.onosproject.net.device.DeviceProviderService;
 import org.onosproject.net.flow.FlowRuleProviderRegistry;
 import org.onosproject.net.flow.FlowRuleProviderService;
+import org.onosproject.net.group.GroupProviderRegistry;
+import org.onosproject.net.group.GroupProviderService;
 import org.onosproject.net.host.HostProvider;
 import org.onosproject.net.host.HostProviderRegistry;
 import org.onosproject.net.host.HostProviderService;
@@ -116,12 +118,16 @@
     protected FlowRuleProviderRegistry flowRuleProviderRegistry;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GroupProviderRegistry groupProviderRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected PacketProviderRegistry packetProviderRegistry;
 
     private final NullDeviceProvider deviceProvider = new NullDeviceProvider();
     private final NullLinkProvider linkProvider = new NullLinkProvider();
     private final NullHostProvider hostProvider = new NullHostProvider();
     private final NullFlowRuleProvider flowRuleProvider = new NullFlowRuleProvider();
+    private final NullGroupProvider groupProvider = new NullGroupProvider();
     private final NullPacketProvider packetProvider = new NullPacketProvider();
     private final TopologyMutationDriver topologyMutationDriver = new TopologyMutationDriver();
 
@@ -129,6 +135,7 @@
     private HostProviderService hostProviderService;
     private LinkProviderService linkProviderService;
     private FlowRuleProviderService flowRuleProviderService;
+    private GroupProviderService groupProviderService;
     private PacketProviderService packetProviderService;
 
     private TopologySimulator simulator;
@@ -176,6 +183,7 @@
         hostProviderService = hostProviderRegistry.register(hostProvider);
         linkProviderService = linkProviderRegistry.register(linkProvider);
         flowRuleProviderService = flowRuleProviderRegistry.register(flowRuleProvider);
+        groupProviderService = groupProviderRegistry.register(groupProvider);
         packetProviderService = packetProviderRegistry.register(packetProvider);
         log.info("Started");
     }
@@ -189,12 +197,14 @@
         hostProviderRegistry.unregister(hostProvider);
         linkProviderRegistry.unregister(linkProvider);
         flowRuleProviderRegistry.unregister(flowRuleProvider);
+        groupProviderRegistry.unregister(groupProvider);
         packetProviderRegistry.unregister(packetProvider);
 
         deviceProviderService = null;
         hostProviderService = null;
         linkProviderService = null;
         flowRuleProviderService = null;
+        groupProviderService = null;
         packetProviderService = null;
 
         log.info("Stopped");
@@ -339,9 +349,11 @@
                        deviceProviderService, hostProviderService,
                        linkProviderService);
         flowRuleProvider.start(flowRuleProviderService);
+        groupProvider.start(groupProviderService);
         packetProvider.start(packetRate, hostService, deviceService,
                              packetProviderService);
         simulator.setUpTopology();
+        groupProvider.initDevicesGroupTable(simulator.deviceIds);
         topologyMutationDriver.start(mutationRate, linkService, deviceService,
                                      linkProviderService, deviceProviderService,
                                      simulator);
@@ -382,6 +394,7 @@
             topologyMutationDriver.stop();
             packetProvider.stop();
             flowRuleProvider.stop();
+            groupProvider.stop();
             delay(500);
             simulator.tearDownTopology();
             simulator = null;