[ONOS-6248] VPLS refactoring

Change-Id: I8ffb2199ca108ad8dfe271681068636fc4af2a40
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/Vpls.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/Vpls.java
new file mode 100644
index 0000000..5dec2b2
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/Vpls.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.net.EncapsulationType;
+
+import java.util.Collection;
+
+public interface Vpls {
+
+    /**
+     * Creates a new VPLS.
+     *
+     * @param vplsName the name of the VPLS
+     * @param encapsulationType the encapsulation type
+     * @return a VPLS instance if the operation is successful; null otherwise
+     */
+    VplsData createVpls(String vplsName, EncapsulationType encapsulationType);
+
+    /**
+     * Removes a VPLS.
+     *
+     * @param vplsData the VPLS to be removed
+     * @return the VPLS removed if the operation is successful; null otherwise
+     */
+    VplsData removeVpls(VplsData vplsData);
+
+    /**
+     * Adds network interfaces to a VPLS.
+     *
+     * @param vplsData the VPLS to which the interfaces have to be added to
+     * @param interfaces the interfaces to add
+     */
+    void addInterfaces(VplsData vplsData, Collection<Interface> interfaces);
+
+    /**
+     * Adds a network interface to a VPLS.
+     *
+     * @param vplsData the VPLS to which the interface has to be added to
+     * @param iface the interface to add
+     */
+    void addInterface(VplsData vplsData, Interface iface);
+
+    /**
+     * Sets an encapsulation type for a VPLS.
+     *
+     * @param vplsData the VPLS for which the encapsulation has to be set
+     * @param encapsulationType the encapsulation type
+     */
+    void setEncapsulationType(VplsData vplsData, EncapsulationType encapsulationType);
+
+    /**
+     * Retrieves a VPLS.
+     *
+     * @param vplsName the name of the VPLS
+     * @return the VPLS instance if the VPLS exists; null otherwise
+     */
+    VplsData getVpls(String vplsName);
+
+    /**
+     * Gets all VPLSs.
+     *
+     * @return a collection of VPLSs
+     */
+    Collection<VplsData> getAllVpls();
+
+    /**
+     * Removes the interfaces specified from a VPLS.
+     *
+     * @param vplsData the VPLS from which the interfaces are to be removed
+     * @param interfaces the interfaces to remove
+     * @return the interfaces removed
+     */
+    Collection<Interface> removeInterfaces(VplsData vplsData, Collection<Interface> interfaces);
+
+    /**
+     * Removes the interface specified from a VPLS.
+     *
+     * @param vplsData the VPLS from which the interface is to be removed
+     * @param iface the interface to remove
+     * @return the interface removed
+     */
+    Interface removeInterface(VplsData vplsData, Interface iface);
+
+    /**
+     * Removes all VPLSs and cleans up the VPLS configuration.
+     */
+    void removeAllVpls();
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsData.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsData.java
new file mode 100644
index 0000000..bba9d78
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsData.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.net.EncapsulationType;
+
+import java.util.Collection;
+import java.util.Objects;
+import java.util.Set;
+
+import static java.util.Objects.*;
+
+/**
+ * Class stores a VPLS information.
+ */
+public final class VplsData {
+    /**
+     * States of a VPLS.
+     */
+    public enum VplsState {
+        UPDATING,
+        ADDING,
+        REMOVING,
+        ADDED,
+        REMOVED,
+        FAILED
+    }
+
+    private String name;
+    private Set<Interface> interfaces;
+    private EncapsulationType encapsulationType;
+    private VplsState state;
+
+    /**
+     * Constructs a VPLS data by given name and encapsulation type.
+     *
+     * @param name the given name
+     * @param encapType the encapsulation type
+     */
+    private VplsData(String name, EncapsulationType encapType) {
+        this.name = name;
+        this.encapsulationType = encapType;
+        this.interfaces = Sets.newHashSet();
+        this.state = VplsState.ADDING;
+    }
+
+    /**
+     * Creates a VPLS data by given name.
+     * The encapsulation type of the VPLS will be NONE.
+     *
+     * @param name the given name
+     * @return the VPLS data
+     */
+    public static VplsData of(String name) {
+        requireNonNull(name);
+        return new VplsData(name, EncapsulationType.NONE);
+    }
+
+    /**
+     * Creates a VPLS data by given name and encapsulation type.
+     *
+     * @param name the given name
+     * @param encapType the encapsulation type
+     * @return the VPLS data
+     */
+    public static VplsData of(String name, EncapsulationType encapType) {
+        requireNonNull(name);
+        if (encapType == null) {
+            return new VplsData(name, EncapsulationType.NONE);
+        } else {
+            return new VplsData(name, encapType);
+        }
+    }
+
+    /**
+     * Creates a copy of VPLS data.
+     *
+     * @param vplsData the VPLS data
+     * @return the copy of the VPLS data
+     */
+    public static VplsData of(VplsData vplsData) {
+        requireNonNull(vplsData);
+        VplsData vplsDataCopy = new VplsData(vplsData.name(), vplsData.encapsulationType());
+        vplsDataCopy.addInterfaces(vplsData.interfaces());
+        return vplsData;
+    }
+
+    /**
+     * Gets name of the VPLS.
+     *
+     * @return the name of the VPLS
+     */
+    public String name() {
+        return name;
+    }
+
+    public Set<Interface> interfaces() {
+        return ImmutableSet.copyOf(interfaces);
+    }
+
+    public EncapsulationType encapsulationType() {
+        return encapsulationType;
+    }
+
+    public void addInterfaces(Collection<Interface> interfaces) {
+        requireNonNull(interfaces);
+        this.interfaces.addAll(interfaces);
+    }
+
+    public void addInterface(Interface iface) {
+        requireNonNull(iface);
+        this.interfaces.add(iface);
+    }
+
+    public void removeInterfaces(Collection<Interface> interfaces) {
+        requireNonNull(interfaces);
+        this.interfaces.removeAll(interfaces);
+    }
+
+    public void removeInterface(Interface iface) {
+        requireNonNull(iface);
+        this.interfaces.remove(iface);
+    }
+
+    public void encapsulationType(EncapsulationType encapType) {
+        this.encapsulationType = encapType;
+    }
+
+    public VplsState state() {
+        return state;
+    }
+
+    public void state(VplsState state) {
+        this.state = state;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("name", name)
+                .add("interfaces", interfaces)
+                .add("encap type", encapsulationType)
+                .add("state", state)
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof VplsData)) {
+            return false;
+        }
+        VplsData other = (VplsData) obj;
+        return Objects.equals(other.name, this.name) &&
+                Objects.equals(other.interfaces, this.interfaces) &&
+                Objects.equals(other.encapsulationType, this.encapsulationType);
+    }
+
+    @Override
+    public int hashCode() {
+        return hash(name, interfaces, encapsulationType);
+    }
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperation.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperation.java
new file mode 100644
index 0000000..abc5025
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperation.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+import static java.util.Objects.*;
+
+/**
+ * Operation for VPLS.
+ */
+public class VplsOperation {
+
+    /**
+     * The operation type.
+     */
+    public enum Operation {
+        REMOVE,
+        ADD,
+        UPDATE
+    }
+
+    private VplsData vplsData;
+    private Operation op;
+
+    /**
+     * Defines a VPLS operation by binding a given VPLS and operation type.
+     *
+     * @param vplsData the VPLS
+     * @param op the operation
+     */
+    protected VplsOperation(VplsData vplsData, Operation op) {
+        requireNonNull(vplsData);
+        requireNonNull(op);
+        // Make a copy of the VPLS data to ensure other thread won't change it.
+        this.vplsData = VplsData.of(vplsData);
+        this.op = op;
+    }
+
+    /**
+     * Defines a VPLS operation by binding a given VPLS and operation type.
+     *
+     * @param vplsData the VPLS
+     * @param op the operation
+     * @return the VPLS operation
+     */
+    public static VplsOperation of(VplsData vplsData, Operation op) {
+        return new VplsOperation(vplsData, op);
+    }
+
+    /**
+     * Retrieves the VPLS from the operation.
+     *
+     * @return the VPLS
+     */
+    public VplsData vpls() {
+        return vplsData;
+    }
+
+    /**
+     * Retrieves the operation type from the operation.
+     *
+     * @return the operation type
+     */
+    public Operation op() {
+        return op;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("vplsData", vplsData.toString())
+                .add("op", op.toString())
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof VplsOperation)) {
+            return false;
+        }
+        VplsOperation other = (VplsOperation) obj;
+        return Objects.equals(other.vplsData, this.vplsData) &&
+                Objects.equals(other.op, this.op);
+    }
+
+    @Override
+    public int hashCode() {
+        return hash(vplsData, op);
+    }
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationException.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationException.java
new file mode 100644
index 0000000..95dfdfa
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationException.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+/**
+ * Exception for VPLS operation.
+ */
+public class VplsOperationException extends RuntimeException {
+    private static final long serialVersionUID = 4514685940685335886L;
+    private VplsOperation vplsOperation;
+
+    /**
+     * Constructs a VPLS operation exception with given VPLS operation and
+     * message.
+     *
+     * @param operation the VPLS operation
+     * @param msg the description of the exception
+     */
+    public VplsOperationException(VplsOperation operation, String msg) {
+        super(msg);
+        this.vplsOperation = operation;
+    }
+
+    /**
+     * Gets VPLS operation for this exception.
+     * @return the VPLS operation
+     */
+    public VplsOperation vplsOperation() {
+        return vplsOperation;
+    }
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationService.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationService.java
new file mode 100644
index 0000000..3044f7e
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsOperationService.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+/**
+ * Service for application submitting VPLS operation.
+ */
+public interface VplsOperationService {
+
+    /**
+     * Submits a VPLS operation.
+     *
+     * @param vplsOperation the VPLS operation to submit
+     */
+    void submit(VplsOperation vplsOperation);
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsStore.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsStore.java
new file mode 100644
index 0000000..caf7550
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/VplsStore.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2017-present 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.vpls.api;
+
+import org.onosproject.store.Store;
+import org.onosproject.store.StoreDelegate;
+import org.onosproject.vpls.store.VplsStoreEvent;
+
+import java.util.Collection;
+
+/**
+ * Definition of the operations regarding the management of the VPLS elements.
+ */
+public interface VplsStore extends Store<VplsStoreEvent, StoreDelegate<VplsStoreEvent>> {
+    /**
+     * Adds a VPLS to the configuration.
+     *
+     * @param vplsData the VPLS to add
+     */
+    void addVpls(VplsData vplsData);
+
+    /**
+     * Removes a VPLS from the configuration.
+     *
+     * @param vplsData the VPLS to remove
+     */
+    void removeVpls(VplsData vplsData);
+
+    /**
+     * Updates a VPLS.
+     *
+     * @param vplsData the VPLS to update
+     */
+    void updateVpls(VplsData vplsData);
+
+    /**
+     * Gets a VPLS by name.
+     *
+     * @param vplsName the VPLS name
+     * @return the VPLS instance if it exists; null otherwise
+     */
+    VplsData getVpls(String vplsName);
+
+    /**
+     * Gets all the VPLSs.
+     *
+     * @return all the VPLSs
+     */
+    Collection<VplsData> getAllVpls();
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/api/package-info.java b/apps/vpls/src/main/java/org/onosproject/vpls/api/package-info.java
new file mode 100644
index 0000000..91d3937
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+/**
+ * APIs for VPLS.
+ */
+package org.onosproject.vpls.api;
\ No newline at end of file