SEBA-640 IgmpProxy should use distributed storage infrastructure of ONOS

Change-Id: I4b1c4d326a5501e9c0e046e3ee8d973ca5f73d70
diff --git a/api/src/main/java/org/opencord/igmpproxy/GroupMemberId.java b/api/src/main/java/org/opencord/igmpproxy/GroupMemberId.java
new file mode 100644
index 0000000..137a259
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/GroupMemberId.java
@@ -0,0 +1,115 @@
+/*
+ * 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;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+
+/**
+ * Identification of group member.
+ */
+public final class GroupMemberId {
+    private Ip4Address groupIp;
+    private DeviceId deviceId;
+    private PortNumber portNum;
+
+    /**
+     * Constructor for serializer.
+     */
+    private GroupMemberId() {
+        this.groupIp = null;
+        this.deviceId = null;
+        this.portNum = null;
+    }
+
+    private GroupMemberId(Ip4Address groupIp, DeviceId deviceId, PortNumber portNum) {
+        this.groupIp = groupIp;
+        this.deviceId = deviceId;
+        this.portNum = portNum;
+    }
+
+    /**
+     * Creates new group member identification.
+     *
+     * @param groupIp  group ip of member
+     * @param deviceId device id
+     * @param portNum  port number of connect point
+     * @return new identification of group-member
+     */
+    public static GroupMemberId of(Ip4Address groupIp, DeviceId deviceId, PortNumber portNum) {
+        return new GroupMemberId(groupIp, deviceId, portNum);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("groupIp", groupIp)
+                .add("deviceId", deviceId)
+                .add("portNumber", portNum).toString();
+    }
+
+    /**
+     * Get group ip of group.
+     *
+     * @return group ip
+     */
+    public Ip4Address getGroupIp() {
+        return groupIp;
+    }
+
+    /**
+     * Get device id of connect point.
+     *
+     * @return device id
+     */
+    public DeviceId getDeviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Get port number of connect point.
+     *
+     * @return port number
+     */
+    public PortNumber getPortNum() {
+        return portNum;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GroupMemberId that = (GroupMemberId) o;
+        return Objects.equals(groupIp, that.groupIp) &&
+                Objects.equals(deviceId, that.deviceId) &&
+                Objects.equals(portNum, that.portNum);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(groupIp, deviceId, portNum);
+    }
+}
+
diff --git a/api/src/main/java/org/opencord/igmpproxy/GroupMemberIdSerializer.java b/api/src/main/java/org/opencord/igmpproxy/GroupMemberIdSerializer.java
new file mode 100644
index 0000000..370148a
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/GroupMemberIdSerializer.java
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Custom serializer for {@link GroupMemberId}.
+ */
+public class GroupMemberIdSerializer extends Serializer<GroupMemberId> {
+    /**
+     * Creates serializer instance.
+     */
+    public GroupMemberIdSerializer() {
+        // non-null, immutable
+        super(false, true);
+    }
+
+    @Override
+    public void write(Kryo kryo, Output output, GroupMemberId groupMemberId) {
+        output.writeString(groupMemberId.getDeviceId().toString());
+        kryo.writeClassAndObject(output, groupMemberId.getGroupIp());
+        kryo.writeClassAndObject(output, groupMemberId.getPortNum());
+    }
+
+    @Override
+    public GroupMemberId read(Kryo kryo, Input input, Class<GroupMemberId> aClass) {
+        DeviceId deviceId = DeviceId.deviceId(input.readString());
+        Ip4Address groupIp = (Ip4Address) kryo.readClassAndObject(input);
+        PortNumber portNum = (PortNumber) kryo.readClassAndObject(input);
+        return GroupMemberId.of(groupIp, deviceId, portNum);
+    }
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/IgmpLeadershipService.java b/api/src/main/java/org/opencord/igmpproxy/IgmpLeadershipService.java
new file mode 100644
index 0000000..1b8d0fe
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/IgmpLeadershipService.java
@@ -0,0 +1,31 @@
+/*
+ * 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;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * Leadership control service.
+ */
+public interface IgmpLeadershipService {
+    /**
+     * Makes leadership control.
+     *
+     * @param deviceId received deviceId
+     * @return if it is leadership of this device, return true
+     */
+    boolean isLocalLeader(DeviceId deviceId);
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/package-info.java b/api/src/main/java/org/opencord/igmpproxy/package-info.java
index a2f9362..f261bd7 100644
--- a/api/src/main/java/org/opencord/igmpproxy/package-info.java
+++ b/api/src/main/java/org/opencord/igmpproxy/package-info.java
@@ -15,6 +15,6 @@
  */
 
 /**
- * Created by onos on 17-3-9.
+ * Package of igmpproxy.
  */
 package org.opencord.igmpproxy;
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/State.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/State.java
new file mode 100644
index 0000000..022d509
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/State.java
@@ -0,0 +1,73 @@
+/*
+ * 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.statemachine;
+
+/**
+ * State of machine.
+ */
+public interface State {
+    /**
+     * Default state.
+     */
+    int STATE_NON = 0;
+    /**
+     * Delay state.
+     */
+    int STATE_DELAY = 1;
+    /**
+     * Idle state.
+     */
+    int STATE_IDLE = 2;
+
+    /**
+     * Transition to join.
+     */
+    int TRANSITION_JOIN = 0;
+    /**
+     * Transition to leave.
+     */
+    int TRANSITION_LEAVE = 1;
+    /**
+     * Transition to query.
+     */
+    int TRANSITION_QUERY = 2;
+    /**
+     * Transition to timeout.
+     */
+    int TRANSITION_TIMEOUT = 3;
+
+    /**
+     * Makes the requirements of join request.
+     */
+    void join();
+
+    /**
+     * Makes the requirements of leave request.
+     */
+    void leave();
+
+    /**
+     * Makes the requirements of query request.
+     *
+     * @param maxResp maximum resp
+     */
+    void query(int maxResp);
+
+    /**
+     * Makes the requirements of timeout .
+     */
+    void timeOut();
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachine.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachine.java
new file mode 100644
index 0000000..ae79931
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachine.java
@@ -0,0 +1,136 @@
+/*
+ * 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.statemachine;
+
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * State machine object.
+ */
+public interface StateMachine {
+    /**
+     * Returns identification of state-machine.
+     *
+     * @return identification
+     */
+    StateMachineId getStateMachineId();
+
+    /**
+     * Returns group ip of state-machine.
+     *
+     * @return ip of group
+     */
+    Ip4Address getGroupIp();
+
+    /**
+     * Returns device id of state-machine.
+     *
+     * @return device id
+     */
+    DeviceId getDeviceId();
+
+    /**
+     * Returns source ip of state-machine.
+     *
+     * @return source ip
+     */
+    Ip4Address getSrcIp();
+
+    /**
+     * Returns up-link port of state-machine.
+     *
+     * @return up-link port
+     */
+    PortNumber getUpLinkPort();
+
+    /**
+     * Returns timeout. it is nullable.
+     *
+     * @return timeout
+     */
+    Integer getTimeOut();
+
+    /**
+     * Set timer to max timeout.
+     */
+    void setMaxTimeout();
+
+    /**
+     * Start timer.
+     *
+     * @param timeout timeout
+     */
+    void startTimer(int timeout);
+
+    /**
+     * Resets timer.
+     *
+     * @param timeout timeout of timer
+     */
+    void resetTimer(int timeout);
+
+    /**
+     * Destroy timeout.
+     */
+    void destroyTimer();
+
+    /**
+     * Increases timeout of timer.
+     */
+    void increaseTimeOut();
+
+    /**
+     * Decreases timeout of timer.
+     */
+    void decreaseTimeOut();
+
+    /**
+     * Returns current state.
+     *
+     * @return current state
+     */
+    int currentState();
+
+    /**
+     * Makes the requirements of join request and transition to next station.
+     *
+     * @param messageOutAllowed message out allowed
+     */
+    void join(boolean messageOutAllowed);
+
+    /**
+     * Makes the requirements of leave request and transition to next station.
+     *
+     * @param messageOutAllowed message out allowed
+     */
+    void leave(boolean messageOutAllowed);
+
+    /**
+     * Makes the requirements of query request and transition to next station.
+     *
+     * @param maxResp maximum resp
+     */
+    void query(int maxResp);
+
+    /**
+     * Makes the requirements of timeout request and transition to next station.
+     *
+     * @param sendQuery send query for test
+     */
+    void timeOut(boolean sendQuery);
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineId.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineId.java
new file mode 100644
index 0000000..38d2bf1
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineId.java
@@ -0,0 +1,98 @@
+/*
+ * 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.statemachine;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+
+import java.util.Objects;
+
+/**
+ * Identification of state machine.
+ */
+public final class StateMachineId {
+    private DeviceId deviceId;
+    private Ip4Address groupIp;
+
+    /**
+     * Constructor for serializer.
+     */
+    private StateMachineId() {
+        this.deviceId = null;
+        this.groupIp = null;
+    }
+
+    private StateMachineId(DeviceId deviceId, Ip4Address groupIp) {
+        this.deviceId = deviceId;
+        this.groupIp = groupIp;
+    }
+
+    /**
+     * Creates new state-machine identification using device-id and group-ip.
+     *
+     * @param deviceId device id of state-machie
+     * @param groupIp  group ip of state-machine
+     * @return created identification
+     */
+    public static StateMachineId of(DeviceId deviceId, Ip4Address groupIp) {
+        return new StateMachineId(deviceId, groupIp);
+    }
+
+    /**
+     * Returns device id of identification.
+     *
+     * @return device id
+     */
+    public DeviceId getDeviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Returns group ip of identification.
+     *
+     * @return group ip
+     */
+    public Ip4Address getGroupIp() {
+        return groupIp;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("deviceId", deviceId)
+                .add("groupIp", groupIp)
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        StateMachineId that = (StateMachineId) o;
+        return Objects.equals(deviceId, that.deviceId) &&
+                Objects.equals(groupIp, that.groupIp);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(deviceId, groupIp);
+    }
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineIdSerializer.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineIdSerializer.java
new file mode 100644
index 0000000..f7c747e
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineIdSerializer.java
@@ -0,0 +1,49 @@
+/*
+ * 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.statemachine;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Custom serializer for {@link StateMachineId}.
+ */
+public class StateMachineIdSerializer extends Serializer<StateMachineId> {
+    /**
+     * Creates serializer instance.
+     */
+    public StateMachineIdSerializer() {
+        // non-null, immutable
+        super(false, true);
+    }
+
+    @Override
+    public void write(Kryo kryo, Output output, StateMachineId stateMachineId) {
+        output.writeString(stateMachineId.getDeviceId().toString());
+        kryo.writeClassAndObject(output, stateMachineId.getGroupIp());
+    }
+
+    @Override
+    public StateMachineId read(Kryo kryo, Input input, Class<StateMachineId> aClass) {
+        DeviceId deviceId = DeviceId.deviceId(input.readString());
+        Ip4Address groupIp = (Ip4Address) kryo.readClassAndObject(input);
+        return StateMachineId.of(deviceId, groupIp);
+    }
+}
\ No newline at end of file
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineService.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineService.java
new file mode 100644
index 0000000..c8ed49e
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/StateMachineService.java
@@ -0,0 +1,88 @@
+/*
+ * 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.statemachine;
+
+import org.onlab.packet.Ip4Address;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Manage State Machine and Igmp Timer.
+ */
+public interface StateMachineService {
+
+    /**
+     * Makes the requirements of igmp-join request.
+     *
+     * @param devId      device id of connect point
+     * @param groupIp    group ip of igmp group
+     * @param srcIP      source ip of device
+     * @param upLinkPort uplink port of device
+     * @return if this is first join from the device and group-ip return true
+     */
+    boolean join(DeviceId devId, Ip4Address groupIp, Ip4Address srcIP, PortNumber upLinkPort);
+
+    /**
+     * Makes the requirements of igmp-leave request.
+     *
+     * @param devId   device id of connect point
+     * @param groupIp group ip of group-member
+     * @return If there are no more members of that device and group returns true
+     */
+    boolean leave(DeviceId devId, Ip4Address groupIp);
+
+    /**
+     * clear map of state-machine.
+     */
+    void clearAllMaps();
+
+    /**
+     * Makes the requirements of special query.
+     *
+     * @param devId   device id
+     * @param groupIp group ip of igmp group
+     * @param maxResp maximum resp of igmp
+     */
+    void specialQuery(DeviceId devId, Ip4Address groupIp, int maxResp);
+
+    /**
+     * Makes the requirements of igmp-query request.
+     *
+     * @param devId   device id
+     * @param maxResp maximum resp of igmp
+     */
+    void generalQuery(DeviceId devId, int maxResp);
+
+    /**
+     * Makes the requirements of igmp-query request.
+     *
+     * @param maxResp maximum resp of igmp
+     */
+    void generalQuery(int maxResp);
+
+    /**
+     * Makes the requirements of timeout.
+     *
+     * @param devId   device id
+     * @param groupIp group ip of igmp group
+     */
+    void timeOut(DeviceId devId, Ip4Address groupIp);
+
+    /**
+     * Checks all state-machines periodically.
+     */
+    void timeOut1s();
+}
diff --git a/api/src/main/java/org/opencord/igmpproxy/statemachine/package-info.java b/api/src/main/java/org/opencord/igmpproxy/statemachine/package-info.java
new file mode 100644
index 0000000..3270750
--- /dev/null
+++ b/api/src/main/java/org/opencord/igmpproxy/statemachine/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.
+ */
+
+/**
+ * Package of state machine.
+ */
+package org.opencord.igmpproxy.statemachine;
\ No newline at end of file