SEBA-640 IgmpProxy should use distributed storage infrastructure of ONOS

Change-Id: I4b1c4d326a5501e9c0e046e3ee8d973ca5f73d70
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/AbstractStateMachineStore.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/AbstractStateMachineStore.java
new file mode 100644
index 0000000..0ecbb14
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/AbstractStateMachineStore.java
@@ -0,0 +1,87 @@
+/*
+ * 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.opencord.igmpproxy.impl.store.machine;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.store.AbstractStore;
+import org.opencord.igmpproxy.statemachine.StateMachine;
+import org.opencord.igmpproxy.statemachine.StateMachineId;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Abstract implementation of state-machine store.
+ */
+public abstract class AbstractStateMachineStore
+        extends AbstractStore<StateMachineEvent, StateMachineStoreDelegate>
+        implements StateMachineStore {
+
+    protected Map<StateMachineId, StateMachine> stateMachineMap;
+
+    protected AbstractStateMachineStore() {
+    }
+
+    protected AbstractStateMachineStore(Map<StateMachineId, StateMachine> stateMachineMap) {
+        this.stateMachineMap = stateMachineMap;
+    }
+
+    @Override
+    public StateMachine putStateMachine(StateMachine stateMachine) {
+        return stateMachineMap.put(stateMachine.getStateMachineId(), stateMachine);
+    }
+
+    @Override
+    public StateMachine updateStateMachine(StateMachine machine) {
+        return stateMachineMap.replace(machine.getStateMachineId(), machine);
+    }
+
+    @Override
+    public StateMachine removeStateMachine(StateMachineId id) {
+        return stateMachineMap.remove(id);
+    }
+
+    @Override
+    public StateMachine getStateMachine(StateMachineId id) {
+        return stateMachineMap.get(id);
+    }
+
+    @Override
+    public Collection<StateMachine> getAllStateMachines() {
+        return ImmutableList.copyOf(stateMachineMap.values());
+    }
+
+    @Override
+    public void clearAllStateMachines() {
+        stateMachineMap.clear();
+        stateMachineMap = null;
+    }
+
+    @Override
+    public void decreaseTimeout(StateMachineId machineId) {
+        StateMachine machine = stateMachineMap.get(machineId);
+        machine.decreaseTimeOut();
+        updateStateMachine(machine);
+    }
+
+    @Override
+    public void increaseTimeout(StateMachineId machineId) {
+        StateMachine machine = stateMachineMap.get(machineId);
+        machine.increaseTimeOut();
+        updateStateMachine(machine);
+    }
+
+}
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/DistributedStateMachineStore.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/DistributedStateMachineStore.java
new file mode 100644
index 0000000..f337b09
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/DistributedStateMachineStore.java
@@ -0,0 +1,119 @@
+/*
+ * 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.opencord.igmpproxy.impl.store.machine;
+
+import org.onlab.util.KryoNamespace;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.AtomicCounterMap;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.opencord.igmpproxy.impl.SingleStateMachine;
+import org.opencord.igmpproxy.impl.SingleStateMachineSerializer;
+import org.opencord.igmpproxy.statemachine.StateMachine;
+import org.opencord.igmpproxy.statemachine.StateMachineId;
+import org.opencord.igmpproxy.statemachine.StateMachineIdSerializer;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Deactivate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * State machine store based on distributed storage.
+ */
+@Component(service = StateMachineStore.class)
+public class DistributedStateMachineStore extends AbstractStateMachineStore {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private static final String STATE_MACHINE_COUNTER_STORE = "onos-state-machine-counter-store";
+    private static final String STATE_MACHINE_MAP_NAME = "onos-state-machine-store";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected StorageService storageService;
+
+    private AtomicCounterMap<StateMachineId> stateMachineCounters;
+
+    private ConsistentMap<StateMachineId, StateMachine> consistentMap;
+
+    public DistributedStateMachineStore() {
+        super();
+    }
+
+    @Activate
+    public void activate() {
+        KryoNamespace.Builder stateMachineKryoBuilder = KryoNamespace.newBuilder()
+                .register(KryoNamespaces.API)
+                .register(new StateMachineIdSerializer(), StateMachineId.class);
+
+        stateMachineCounters = storageService.<StateMachineId>atomicCounterMapBuilder()
+                .withName(STATE_MACHINE_COUNTER_STORE)
+                .withSerializer(Serializer.using(stateMachineKryoBuilder.build())).build();
+
+        stateMachineKryoBuilder
+                .register(new SingleStateMachineSerializer(), SingleStateMachine.class);
+        this.consistentMap = storageService.<StateMachineId, StateMachine>consistentMapBuilder()
+                .withName(STATE_MACHINE_MAP_NAME)
+                .withSerializer(Serializer.using(stateMachineKryoBuilder.build()))
+                .build();
+        super.stateMachineMap = consistentMap.asJavaMap();
+
+
+        log.info("Started.");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        stateMachineMap.clear();
+        stateMachineMap = null;
+        consistentMap.destroy();
+        stateMachineCounters.clear();
+        stateMachineCounters.destroy();
+        log.info("Stopped.");
+    }
+
+    @Override
+    public long increaseAndGetCounter(StateMachineId stateMachineId) {
+        return stateMachineCounters.incrementAndGet(stateMachineId);
+    }
+
+    @Override
+    public long decreaseAndGetCounter(StateMachineId stateMachineId) {
+        if (stateMachineCounters.get(stateMachineId) > 0) {
+            return stateMachineCounters.decrementAndGet(stateMachineId);
+        } else {
+            return stateMachineCounters.get(stateMachineId);
+        }
+    }
+
+    @Override
+    public long getCounter(StateMachineId stateMachineId) {
+        return stateMachineCounters.get(stateMachineId);
+    }
+
+    @Override
+    public boolean removeCounter(StateMachineId stateMachineId) {
+        stateMachineCounters.remove(stateMachineId);
+        return true;
+    }
+
+    @Override
+    public void clearAllStateMachines() {
+        super.clearAllStateMachines();
+        stateMachineCounters.clear();
+    }
+}
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineEvent.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineEvent.java
new file mode 100644
index 0000000..ab9df23
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineEvent.java
@@ -0,0 +1,58 @@
+/*
+ * 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.opencord.igmpproxy.impl.store.machine;
+
+import org.onosproject.event.AbstractEvent;
+import org.opencord.igmpproxy.statemachine.StateMachine;
+
+/**
+ * State machine event.
+ */
+public class StateMachineEvent extends
+        AbstractEvent<StateMachineEvent.Type, StateMachine> {
+    /**
+     * Internal state-machine event type.
+     */
+    public enum Type {
+        /**
+         * Signifies that state-machine added to store.
+         */
+        STATE_MACHINE_ADDED,
+        /**
+         * Signifies that state-machine updated in store.
+         */
+        STATE_MACHINE_UPDATED,
+        /**
+         * Signifies that state-machine removed from store.
+         */
+        STATE_MACHINE_REMOVED
+    }
+
+    /**
+     * Creates new state machine event.
+     *
+     * @param type    state-machine event type.
+     * @param subject state machine.
+     */
+    public StateMachineEvent(Type type, StateMachine subject) {
+        super(type, subject);
+    }
+
+    protected StateMachineEvent(Type type, StateMachine subject, long time) {
+        super(type, subject, time);
+    }
+}
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStore.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStore.java
new file mode 100644
index 0000000..ddb566a
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStore.java
@@ -0,0 +1,116 @@
+/*
+ * 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.opencord.igmpproxy.impl.store.machine;
+
+import org.opencord.igmpproxy.statemachine.StateMachine;
+import org.opencord.igmpproxy.statemachine.StateMachineId;
+
+import java.util.Collection;
+
+/**
+ * State Machine Store.
+ */
+public interface StateMachineStore {
+    /**
+     * Increases counter state-machine.
+     *
+     * @param stateMachineId identification of machine
+     * @return counter
+     */
+    long increaseAndGetCounter(StateMachineId stateMachineId);
+
+    /**
+     * Decreases counter of state-machine.
+     *
+     * @param stateMachineId identification of machine
+     * @return if counter greater than zero, decreases counter and return counter
+     */
+    long decreaseAndGetCounter(StateMachineId stateMachineId);
+
+    /**
+     * Removes counter from counter-map.
+     *
+     * @param stateMachineId identification of machine
+     * @return true
+     */
+    boolean removeCounter(StateMachineId stateMachineId);
+
+    /**
+     * Get counter using state-machine id.
+     *
+     * @param stateMachineId identification of machine
+     * @return counter of member that use this id
+     */
+    long getCounter(StateMachineId stateMachineId);
+
+    /**
+     * Add new machine to store.
+     *
+     * @param stateMachine given machine
+     * @return added informations
+     */
+    StateMachine putStateMachine(StateMachine stateMachine);
+
+    /**
+     * Update information in store.
+     *
+     * @param stateMachine given machine
+     * @return updated info
+     */
+    StateMachine updateStateMachine(StateMachine stateMachine);
+
+    /**
+     * Get information from store.
+     *
+     * @param id given identification of machine
+     * @return information of machine
+     */
+    StateMachine getStateMachine(StateMachineId id);
+
+    /**
+     * Remove machine from store.
+     *
+     * @param id given identification of machine
+     * @return removed info
+     */
+    StateMachine removeStateMachine(StateMachineId id);
+
+    /**
+     * Get informations of all machine.
+     *
+     * @return machine informations
+     */
+    Collection<StateMachine> getAllStateMachines();
+
+    /**
+     * clear information map.
+     */
+    void clearAllStateMachines();
+
+    /**
+     * Decreases timeout of timer.
+     *
+     * @param machineId given machine id
+     */
+    void decreaseTimeout(StateMachineId machineId);
+
+    /**
+     * Increases timeout of timer.
+     *
+     * @param machineId given machine id
+     */
+    void increaseTimeout(StateMachineId machineId);
+}
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStoreDelegate.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStoreDelegate.java
new file mode 100644
index 0000000..f8db089
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/StateMachineStoreDelegate.java
@@ -0,0 +1,25 @@
+/*
+ * 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.opencord.igmpproxy.impl.store.machine;
+
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * State machine store delegate abstraction.
+ */
+public interface StateMachineStoreDelegate extends StoreDelegate<StateMachineEvent> {
+}
diff --git a/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/package-info.java b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/package-info.java
new file mode 100644
index 0000000..2b9bc9e
--- /dev/null
+++ b/app/src/main/java/org/opencord/igmpproxy/impl/store/machine/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementation of state-machine store.
+ */
+package org.opencord.igmpproxy.impl.store.machine;
\ No newline at end of file