Adding Encapsulation in VPLS and correcting bugs.

Change-Id: Idc0c1834ae2bbd0fdaf564fd65360cc0f018d18d
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsAppConfig.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsAppConfig.java
new file mode 100644
index 0000000..398962e
--- /dev/null
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsAppConfig.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2016-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.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Sets;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.EncapsulationType;
+import org.onosproject.net.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+
+/**
+ * Represents the VPLS application configuration.
+ */
+public class VplsAppConfig extends Config<ApplicationId> {
+    private static final String VPLS = "vplsList";
+    private static final String NAME = "name";
+    private static final String INTERFACE = "interfaces";
+    private static final String ENCAPSULATION = "encapsulation";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /**
+     * Returns a set of configured VPLSs.
+     *
+     * @return set of VPLSs
+     */
+    public Set<VplsConfig> vplss() {
+        Set<VplsConfig> vplss = Sets.newHashSet();
+
+        JsonNode vplsNode = object.get(VPLS);
+
+        if (vplsNode == null) {
+            return vplss;
+        }
+
+        vplsNode.forEach(jsonNode -> {
+            String name = jsonNode.get(NAME).asText();
+
+            Set<String> ifaces = Sets.newHashSet();
+            jsonNode.path(INTERFACE).forEach(ifacesNode ->
+                    ifaces.add(ifacesNode.asText())
+            );
+
+            String encap = null;
+            if (jsonNode.hasNonNull(ENCAPSULATION)) {
+                encap = jsonNode.get(ENCAPSULATION).asText();
+            }
+            vplss.add(new VplsConfig(name,
+                                    ifaces,
+                                    EncapsulationType.enumFromString(encap)));
+        });
+
+        return vplss;
+    }
+
+    /**
+     * Returns the VPLS configuration given a VPLS name.
+     *
+     * @param name the name of the VPLS
+     * @return the VPLS configuration if it exists; null otherwise
+     */
+    public VplsConfig getVplsWithName(String name) {
+        return vplss().stream()
+                      .filter(vpls -> vpls.name().equals(name))
+                      .findFirst()
+                      .orElse(null);
+    }
+
+    /**
+     * Adds a VPLS to the configuration.
+     *
+     * @param vpls the name of the VPLS
+     */
+    public void addVpls(VplsConfig vpls) {
+        ObjectNode vplsNode = JsonNodeFactory.instance.objectNode();
+
+        vplsNode.put(NAME, vpls.name());
+
+        ArrayNode ifacesNode = vplsNode.putArray(INTERFACE);
+        vpls.ifaces().forEach(ifacesNode::add);
+
+        vplsNode.put(ENCAPSULATION, vpls.encap().toString());
+
+        ArrayNode vplsArray = vplss().isEmpty() ?
+                initVplsConfiguration() : (ArrayNode) object.get(VPLS);
+        vplsArray.add(vplsNode);
+    }
+
+    /**
+     * Removes a VPLS from the configuration.
+     *
+     * @param vplsName the vplsName of the VPLS to be removed
+     */
+    public void removeVpls(String vplsName) {
+        ArrayNode configuredVpls = (ArrayNode) object.get(VPLS);
+
+        for (int i = 0; i < configuredVpls.size(); i++) {
+            if (configuredVpls.get(i).hasNonNull(NAME) &&
+                    configuredVpls.get(i).get(NAME).asText().equals(vplsName)) {
+                configuredVpls.remove(i);
+                return;
+            }
+        }
+    }
+
+    /**
+     * Finds a VPLS with a given network interface.
+     *
+     * @param iface the network interface
+     * @return the VPLS if found; null otherwise
+     */
+    public VplsConfig vplsFromIface(String iface) {
+        for (VplsConfig vpls : vplss()) {
+            if (vpls.isAttached(iface)) {
+                return vpls;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Adds a network interface to a VPLS.
+     *
+     * @param vplsName the vplsName of the VPLS
+     * @param iface the network interface to be added
+     */
+    public void addIface(String vplsName, String iface) {
+        JsonNode vplsNodes = object.get(VPLS);
+        vplsNodes.forEach(vplsNode -> {
+            if (hasNamedNode(vplsNode, vplsName)) {
+                ArrayNode ifacesNode = (ArrayNode) vplsNode.get(INTERFACE);
+                for (int i = 0; i < ifacesNode.size(); i++) {
+                    if (ifacesNode.get(i).asText().equals(iface)) {
+                        return; // Interface already exists.
+                    }
+                }
+                ifacesNode.add(iface);
+            }
+        });
+    }
+
+    /**
+     * Removes a network interface from a VPLS.
+     *
+     * @param name the name of the VPLS
+     * @param iface the network interface to be removed
+     */
+    public void removeIface(VplsConfig name, String iface) {
+        JsonNode vplsNodes = object.get(VPLS);
+        vplsNodes.forEach(vplsNode -> {
+            if (hasNamedNode(vplsNode, name.name())) {
+                ArrayNode ifacesNode = (ArrayNode) vplsNode.get(INTERFACE);
+                for (int i = 0; i < ifacesNode.size(); i++) {
+                    if (ifacesNode.get(i).asText().equals(iface)) {
+                        ifacesNode.remove(i);
+                        return;
+                    }
+                }
+            }
+        });
+    }
+
+    /**
+     * Activate, deactivates, sets the encapsulation type for a given VPLS.
+     *
+     * @param vplsName the vplsName of the VPLS
+     * @param encap the encapsulation type, if set
+     */
+    public void setEncap(String vplsName, EncapsulationType encap) {
+        JsonNode vplsNodes = object.get(VPLS);
+        vplsNodes.forEach(vplsNode -> {
+            if (hasNamedNode(vplsNode, vplsName)) {
+                ((ObjectNode) vplsNode).put(ENCAPSULATION, encap.toString());
+            }
+        });
+    }
+
+    /**
+     * States if a JSON node has a "name" attribute and if the value is equal to
+     * the name given.
+     *
+     * @param jsonNode the JSON node
+     * @param name the node name
+     * @return true if the JSON node has a "name" attribute with value equal to
+     * the name given; false otherwise
+     */
+    private boolean hasNamedNode(JsonNode jsonNode, String name) {
+        return jsonNode.hasNonNull(NAME) &&
+                jsonNode.get(NAME).asText().equals(name);
+    }
+
+    /**
+     * Creates an empty VPLS configuration.
+     *
+     * @return empty ArrayNode to store the VPLS configuration
+     */
+    private ArrayNode initVplsConfiguration() {
+        return object.putArray(VPLS);
+    }
+}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfig.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfig.java
index f8323ef..94e96ab 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfig.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfig.java
@@ -15,180 +15,88 @@
  */
 package org.onosproject.vpls.config;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.collect.Sets;
-import org.onosproject.core.ApplicationId;
-import org.onosproject.net.config.Config;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.net.EncapsulationType;
 
+import java.util.Objects;
 import java.util.Set;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
- * Configuration object for VPLS config.
+ * Configuration of a VPLS.
  */
-public class VplsConfig extends Config<ApplicationId> {
-    private static final String VPLS = "vplsNetworks";
-    private static final String NAME = "name";
-    private static final String INTERFACE = "interfaces";
+public class VplsConfig {
+    private final String name;
+    private final Set<String> ifaces;
+    private final EncapsulationType encap;
 
     /**
-     * Returns a set of configured VPLSs.
-     *
-     * @return set of VPLSs
-     */
-    public Set<VplsNetworkConfig> vplsNetworks() {
-        Set<VplsNetworkConfig> vpls = Sets.newHashSet();
-
-        JsonNode vplsNode = object.get(VPLS);
-
-        if (vplsNode == null) {
-            return vpls;
-        }
-
-        vplsNode.forEach(jsonNode -> {
-            Set<String> ifaces = Sets.newHashSet();
-            jsonNode.path(INTERFACE).forEach(ifacesNode ->
-                    ifaces.add(ifacesNode.asText())
-            );
-
-            String name = jsonNode.get(NAME).asText();
-
-            vpls.add(new VplsNetworkConfig(name, ifaces));
-        });
-
-        return vpls;
-    }
-
-    /**
-     * Returns the VPLS configuration given a VPLS name.
+     * Creates a new VPLS configuration.
      *
      * @param name the VPLS name
-     * @return the VPLS configuration if it exists; null otherwise
+     * @param ifaces the interfaces associated with the VPLS
+     * @param encap the encapsulation type if set
      */
-    public VplsNetworkConfig getVplsWithName(String name) {
-        for (VplsNetworkConfig vpls : vplsNetworks()) {
-            if (vpls.name().equals(name)) {
-                return vpls;
-            }
+    public VplsConfig(String name, Set<String> ifaces, EncapsulationType encap) {
+        this.name = checkNotNull(name);
+        this.ifaces = checkNotNull(ImmutableSet.copyOf(ifaces));
+        this.encap = checkNotNull(encap);
+    }
+
+    /**
+     * The name of the VPLS.
+     *
+     * @return the name of the VPLS
+     */
+    public String name() {
+        return name;
+    }
+
+    /**
+     * The name of the interfaces associated with the VPLS.
+     *
+     * @return a set of interface names associated with the VPLS
+     */
+    public Set<String> ifaces() {
+        return ImmutableSet.copyOf(ifaces);
+    }
+
+    /**
+     * The encapsulation type.
+     *
+     * @return the encapsulation type, if active; null otherwise
+     */
+    public EncapsulationType encap() {
+        return encap;
+    }
+
+    /**
+     * States if a given interface is part of a VPLS.
+     *
+     * @param iface the interface attached to a VPLS
+     * @return true if the interface is associated to the VPLS; false otherwise
+     */
+    public boolean isAttached(String iface) {
+        return ifaces.stream().anyMatch(iface::equals);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
         }
-        return null;
-    }
-
-    /**
-     * Adds a VPLS to the configuration.
-     *
-     * @param name the name of the VPLS to be added
-     */
-    public void addVpls(VplsNetworkConfig name) {
-        ObjectNode vplsNode = JsonNodeFactory.instance.objectNode();
-
-        vplsNode.put(NAME, name.name());
-
-        ArrayNode ifacesNode = vplsNode.putArray(INTERFACE);
-        name.ifaces().forEach(ifacesNode::add);
-
-        ArrayNode vplsArray = vplsNetworks().isEmpty() ?
-                initVplsConfiguration() : (ArrayNode) object.get(VPLS);
-        vplsArray.add(vplsNode);
-    }
-
-    /**
-     * Removes a VPLS from the configuration.
-     *
-     * @param name the name of the VPLS to be removed
-     */
-    public void removeVpls(String name) {
-        ArrayNode vplsArray = (ArrayNode) object.get(VPLS);
-
-        for (int i = 0; i < vplsArray.size(); i++) {
-            if (vplsArray.get(i).hasNonNull(NAME) &&
-                    vplsArray.get(i).get(NAME).asText().equals(name)) {
-                vplsArray.remove(i);
-                return;
-            }
+        if (obj instanceof VplsConfig) {
+            VplsConfig that = (VplsConfig) obj;
+            return Objects.equals(name, that.name) &&
+                   Objects.equals(ifaces, that.ifaces) &&
+                   Objects.equals(encap, that.encap);
         }
+        return false;
     }
 
-    /**
-     * Finds a VPLS with a given network interface.
-     *
-     * @param iface the network interface
-     * @return the VPLS if found; null otherwise
-     */
-    public VplsNetworkConfig getVplsFromInterface(String iface) {
-        for (VplsNetworkConfig vpls : vplsNetworks()) {
-            if (vpls.isAttached(iface)) {
-                return vpls;
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Adds a network interface to a VPLS.
-     *
-     * @param name the name of the VPLS
-     * @param iface the network interface to be added
-     */
-    public void addInterfaceToVpls(String name, String iface) {
-        JsonNode vplsNode = object.get(VPLS);
-        vplsNode.forEach(jsonNode -> {
-
-            if (hasNamedNode(jsonNode, name)) {
-                ArrayNode ifacesNode = (ArrayNode) jsonNode.get(INTERFACE);
-                for (int i = 0; i < ifacesNode.size(); i++) {
-                    if (ifacesNode.get(i).asText().equals(iface)) {
-                        return; // Interface already exists.
-                    }
-                }
-                ifacesNode.add(iface);
-            }
-        });
-    }
-
-    /**
-     * Removes a network interface from a VPLS.
-     *
-     * @param name the name of the VPLS
-     * @param iface the network interface to be removed
-     */
-    public void removeInterfaceFromVpls(VplsNetworkConfig name, String iface) {
-        JsonNode vplsNode = object.get(VPLS);
-        vplsNode.forEach(jsonNode -> {
-            if (hasNamedNode(jsonNode, name.name())) {
-                ArrayNode ifacesNode = (ArrayNode) jsonNode.get(INTERFACE);
-                for (int i = 0; i < ifacesNode.size(); i++) {
-                    if (ifacesNode.get(i).asText().equals(iface)) {
-                        ifacesNode.remove(i);
-                        return;
-                    }
-                }
-            }
-        });
-    }
-
-    /**
-     * States if a JSON node has a "name" attribute and if the value is equal to
-     * the name given.
-     *
-     * @param jsonNode the JSON node
-     * @param name the node name
-     * @return true if the JSON node has a "name" attribute with value equal to
-     * the name given; false otherwise
-     */
-    private boolean hasNamedNode(JsonNode jsonNode, String name) {
-        return jsonNode.hasNonNull(NAME) &&
-                jsonNode.get(NAME).asText().equals(name);
-    }
-
-    /**
-     * Creates an empty VPLS configuration.
-     *
-     * @return empty ArrayNode to store the VPLS configuration
-     */
-    private ArrayNode initVplsConfiguration() {
-        return object.putArray(VPLS);
+    @Override
+    public int hashCode() {
+        return Objects.hash(name, ifaces, encap);
     }
 }
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfigurationService.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfigurationService.java
index 16cd4c4..6579378 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfigurationService.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsConfigurationService.java
@@ -19,102 +19,129 @@
 import org.onlab.packet.VlanId;
 import org.onosproject.incubator.net.intf.Interface;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.EncapsulationType;
 
+import java.util.Map;
 import java.util.Set;
 
 /**
  * Provides information about the VPLS configuration.
  */
 public interface VplsConfigurationService {
-    Class<VplsConfig> CONFIG_CLASS = VplsConfig.class;
+    Class<VplsAppConfig> CONFIG_CLASS = VplsAppConfig.class;
 
     /**
      * Adds a VPLS to the configuration.
      *
-     * @param name the name of the VPLS
+     * @param vplsName the name of the VPLS
      * @param ifaces the interfaces associated with the VPLS
+     * @param encap the encapsulation type
      */
-    void addVpls(String name, Set<String> ifaces);
+    void addVpls(String vplsName, Set<String> ifaces, String encap);
 
     /**
      * Removes a VPLS from the configuration.
      *
-     * @param name the name of the VPLS to be removed
+     * @param vplsName the name of the VPLS to be removed
      */
-    void removeVpls(String name);
+    void removeVpls(String vplsName);
 
     /**
      * Adds a network interface to a VPLS.
      *
-     * @param name the name of the VPLS
+     * @param vplsName the name of the VPLS
      * @param iface the network interface to be added to the VPLS
      */
-    void addInterfaceToVpls(String name, String iface);
+    void addIface(String vplsName, String iface);
+
+    /**
+     * Sets an encapsulation parameter for a VPLS.
+     *
+     * @param vplsName the name of the VPLS
+     * @param encap the encapsulation used (i.e. MPLS or VLAN) or
+     */
+    void setEncap(String vplsName, String encap);
+
+    /**
+     * Returns the encapsulation type in use for a given VPLS.
+     *
+     * @param vplsName the name of the VPLS
+     * @return the encapsulation type in use, if any
+     */
+    EncapsulationType encap(String vplsName);
 
     /**
      * Removes a network interface from a VPLS.
      *
      * @param iface the network interface to be removed from the VPLS
      */
-    void removeInterfaceFromVpls(String iface);
+    void removeIface(String iface);
 
     /**
      * Cleans up the VPLS configuration. Removes all VPLSs.
      */
-    void cleanVpls();
+    void cleanVplsConfig();
 
     /**
      * Retrieves the VPLS names modified from CLI.
      *
-     * @return a set of VPLS names modified from CLI
+     * @return the VPLS names modified from CLI
      */
-    Set<String> getVplsAffectedByApi();
-    // TODO Removes this function after intent framework fix race condition
+    Set<String> vplsAffectedByApi();
+    // TODO Remove this function after the intent framework race condition has been fixed
 
     /**
      * Retrieves the interfaces from the VPLS configuration.
      *
      * @return a set of interfaces contained in the VPLS configuration
      */
-    Set<Interface> getAllInterfaces();
+    Set<Interface> allIfaces();
 
     /**
      * Retrieves the interfaces belonging to the VPLS.
      *
-     * @param name the name of the VPLS
+     * @param vplsName the name of the VPLS
      * @return a set of interfaces belonging to the VPLS
      */
-    Set<Interface> getVplsInterfaces(String name);
+    Set<Interface> ifaces(String vplsName);
 
     /**
      * Retrieves all VPLS names.
      *
      * @return a set of VPLS names
      */
-    Set<String> getAllVpls();
+    Set<String> vplsNames();
 
     /**
      * Retrieves all VPLS names from the old config.
      *
      * @return a set of VPLS names
      */
-    Set<String> getOldVpls();
+    Set<String> vplsNamesOld();
     // TODO Removes this function after intent framework fix race condition
 
     /**
-     * Retrieves the VPLS names and associated interfaces from the configuration.
+     * Returns the VPLS names and associated interfaces from the configuration.
      *
-     * @return a map VPLS names and associated interfaces
+     * @return a map of VPLS names and associated interfaces
      */
-    SetMultimap<String, Interface> getVplsNetworks();
+    SetMultimap<String, Interface> ifacesByVplsName();
 
     /**
-     * Retrieves a VPLS network given a VLAN Id and a connect point.
+     * Returns the list of interfaces grouped by VPLS name, given a VLAN Id and
+     * a connect point.
      *
      * @param vlan the VLAN Id
      * @param connectPoint the connect point
-     * @return a map VPLS names and associated interfaces; null otherwise
+     * @return a map of VPLS names and associated interfaces; null otherwise
      */
-    SetMultimap<String, Interface> getVplsNetwork(VlanId vlan,
-                                                 ConnectPoint connectPoint);
+    SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan,
+                                                    ConnectPoint connectPoint);
+
+    /**
+     * Returns the VPLS names and associated encapsulation type.
+     *
+     * @return a map of VPLS names and associated encapsulation type
+     */
+    Map<String, EncapsulationType> encapByVplsName();
 }
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsNetworkConfig.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsNetworkConfig.java
deleted file mode 100644
index b3c721b..0000000
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/VplsNetworkConfig.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright 2016-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.config;
-
-import com.google.common.collect.ImmutableSet;
-
-import java.util.Objects;
-import java.util.Set;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * Configuration of a VPLS Network.
- */
-public class VplsNetworkConfig {
-    private final String name;
-    private final Set<String> ifaces;
-
-    /**
-     * Creates a new VPLS configuration.
-     *
-     * @param name the VPLS name
-     * @param ifaces the interfaces associated with the VPLS
-     */
-    public VplsNetworkConfig(String name, Set<String> ifaces) {
-        this.name = checkNotNull(name);
-        this.ifaces = checkNotNull(ImmutableSet.copyOf(ifaces));
-    }
-
-    /**
-     * Returns the name of the VPLS.
-     *
-     * @return the name of the VPLS
-     */
-    public String name() {
-        return name;
-    }
-
-    /**
-     * Returns the name of interfaces associated with the VPLS.
-     *
-     * @return a set of interface names associated with the VPLS
-     */
-    public Set<String> ifaces() {
-        return ImmutableSet.copyOf(ifaces);
-    }
-
-    /**
-     * States if a given interface is part of a VPLS.
-     *
-     * @param iface the interface attached to a VPLS
-     * @return true if the interface is associated to the VPLS; false otherwise
-     */
-    public boolean isAttached(String iface) {
-        return ifaces.stream().anyMatch(i -> i.equals(iface));
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj instanceof VplsNetworkConfig) {
-            final VplsNetworkConfig that = (VplsNetworkConfig) obj;
-            return Objects.equals(this.name, that.name) &&
-                    Objects.equals(this.ifaces, that.ifaces);
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(name, ifaces);
-    }
-}
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/VplsConfigurationImpl.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/VplsConfigurationImpl.java
index e977170..0c2f09b 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/VplsConfigurationImpl.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/VplsConfigurationImpl.java
@@ -16,8 +16,10 @@
 package org.onosproject.vpls.config.impl;
 
 import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.collect.Maps;
 import com.google.common.collect.SetMultimap;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -31,19 +33,22 @@
 import org.onosproject.incubator.net.intf.Interface;
 import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.EncapsulationType;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
 import org.onosproject.net.config.NetworkConfigRegistry;
 import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.net.config.NetworkConfigListener;
-import org.onosproject.net.config.NetworkConfigEvent;
-import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.basics.SubjectFactories;
+import org.onosproject.vpls.config.VplsAppConfig;
 import org.onosproject.vpls.config.VplsConfig;
-import org.onosproject.vpls.config.VplsNetworkConfig;
 import org.onosproject.vpls.config.VplsConfigurationService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -80,21 +85,22 @@
 
     private final Set<String> vplsAffectedByApi = new HashSet<>();
 
-    private VplsConfig vplsConfig = new VplsConfig();
+    private VplsAppConfig vplsAppConfig = new VplsAppConfig();
 
     private SetMultimap<String, String> ifacesOfVpls = HashMultimap.create();
     private SetMultimap<String, String> oldIfacesOfVpls = HashMultimap.create();
-    private SetMultimap<String, Interface> vplsNetworks = HashMultimap.create();
+    private SetMultimap<String, Interface> vplsIfaces = HashMultimap.create();
+    private Map<String, EncapsulationType> vplsEncaps = Maps.newHashMap();
 
     private final InternalNetworkConfigListener configListener =
             new InternalNetworkConfigListener();
 
-    private ConfigFactory<ApplicationId, VplsConfig> vplsConfigFactory =
-            new ConfigFactory<ApplicationId, VplsConfig>(
-                    SubjectFactories.APP_SUBJECT_FACTORY, VplsConfig.class, VPLS) {
+    private ConfigFactory<ApplicationId, VplsAppConfig> vplsConfigFactory =
+            new ConfigFactory<ApplicationId, VplsAppConfig>(
+                    SubjectFactories.APP_SUBJECT_FACTORY, VplsAppConfig.class, VPLS) {
                 @Override
-                public VplsConfig createConfig() {
-                    return new VplsConfig();
+                public VplsAppConfig createConfig() {
+                    return new VplsAppConfig();
                 }
             };
 
@@ -115,23 +121,164 @@
         log.info("Stopped");
     }
 
+    @Override
+    public void addVpls(String vplsName, Set<String> ifaces, String encap) {
+        EncapsulationType encapType = EncapsulationType.enumFromString(encap);
+
+        if (ifacesOfVpls.containsKey(vplsName)) {
+            if (ifaces.isEmpty()) {
+                return;
+            }
+            ifaces.forEach(iface -> vplsAppConfig.addIface(vplsName, iface));
+            vplsAppConfig.setEncap(vplsName, encapType);
+        } else {
+            vplsAppConfig.addVpls(new VplsConfig(vplsName, ifaces, encapType));
+        }
+
+        vplsAffectedByApi.add(vplsName);
+        applyConfig(vplsAppConfig);
+    }
+
+    @Override
+    public void removeVpls(String vplsName) {
+        if (ifacesOfVpls.containsKey(vplsName)) {
+            vplsAppConfig.removeVpls(vplsName);
+            vplsAffectedByApi.add(vplsName);
+            applyConfig(vplsAppConfig);
+        }
+    }
+
+    @Override
+    public void addIface(String vplsName, String iface) {
+        if (ifacesOfVpls.containsKey(vplsName)) {
+            vplsAppConfig.addIface(vplsName, iface);
+            vplsAffectedByApi.add(vplsName);
+            applyConfig(vplsAppConfig);
+        }
+    }
+
+    @Override
+    public void setEncap(String vplsName, String encap) {
+        EncapsulationType encapType = EncapsulationType.enumFromString(encap);
+
+        if (ifacesOfVpls.containsKey(vplsName)) {
+            vplsAppConfig.setEncap(vplsName, encapType);
+            vplsAffectedByApi.add(vplsName);
+            applyConfig(vplsAppConfig);
+        }
+    }
+
+    @Override
+    public void removeIface(String iface) {
+        if (ifacesOfVpls.containsValue(iface)) {
+            VplsConfig vpls = vplsAppConfig.vplsFromIface(iface);
+            vplsAppConfig.removeIface(vpls, iface);
+            vplsAffectedByApi.add(vpls.name());
+            applyConfig(vplsAppConfig);
+        }
+    }
+
+    @Override
+    public void cleanVplsConfig() {
+        ifacesOfVpls.entries().forEach(e -> {
+            vplsAppConfig.removeVpls(e.getKey());
+            vplsAffectedByApi.add(e.getKey());
+        });
+        applyConfig(vplsAppConfig);
+    }
+
+    @Override
+    public EncapsulationType encap(String vplsName) {
+        EncapsulationType encap = null;
+        if (vplsEncaps.containsKey(vplsName)) {
+            encap = vplsEncaps.get(vplsName);
+        }
+
+        return encap;
+    }
+
+    @Override
+    public Set<String> vplsAffectedByApi() {
+        Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectedByApi);
+        vplsAffectedByApi.clear();
+
+        return vplsNames;
+    }
+
+    @Override
+    public Set<Interface> allIfaces() {
+        Set<Interface> allVplsInterfaces = new HashSet<>();
+        vplsIfaces.values().forEach(allVplsInterfaces::add);
+
+        return allVplsInterfaces;
+    }
+
+    @Override
+    public Set<Interface> ifaces(String vplsName) {
+        Set<Interface> vplsInterfaces = new HashSet<>();
+        vplsIfaces.get(vplsName).forEach(vplsInterfaces::add);
+
+        return vplsInterfaces;
+    }
+
+    @Override
+    public Set<String> vplsNames() {
+        return ifacesOfVpls.keySet();
+    }
+
+    @Override
+    public Set<String> vplsNamesOld() {
+        return oldIfacesOfVpls.keySet();
+    }
+
+    @Override
+    public SetMultimap<String, Interface> ifacesByVplsName() {
+        return ImmutableSetMultimap.copyOf(vplsIfaces);
+    }
+
+    @Override
+    public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan,
+                                                           ConnectPoint connectPoint) {
+        String vplsName =
+                vplsIfaces.entries().stream()
+                        .filter(e -> e.getValue().connectPoint().equals(connectPoint))
+                        .filter(e -> e.getValue().vlan().equals(vlan))
+                        .map(e -> e.getKey())
+                        .findFirst()
+                        .orElse(null);
+        SetMultimap<String, Interface> result = HashMultimap.create();
+        if (vplsName != null && vplsIfaces.containsKey(vplsName)) {
+            vplsIfaces.get(vplsName)
+                    .forEach(intf -> result.put(vplsName, intf));
+            return result;
+        }
+        return null;
+    }
+
+    @Override
+    public Map<String, EncapsulationType> encapByVplsName() {
+        return ImmutableMap.copyOf(vplsEncaps);
+    }
+
     /**
      * Retrieves the VPLS configuration from network configuration.
      */
     private void loadConfiguration() {
         loadAppId();
 
-        vplsConfig = configService.getConfig(vplsAppId, VplsConfig.class);
+        vplsAppConfig = configService.getConfig(vplsAppId, VplsAppConfig.class);
 
-        if (vplsConfig == null) {
+        if (vplsAppConfig == null) {
             log.warn(CONFIG_NULL);
-            configService.addConfig(vplsAppId, VplsConfig.class);
+            configService.addConfig(vplsAppId, VplsAppConfig.class);
             return;
         }
 
         oldIfacesOfVpls = ifacesOfVpls;
         ifacesOfVpls = getConfigInterfaces();
-        vplsNetworks = getConfigCPoints();
+        vplsIfaces = getConfigCPoints();
+        vplsEncaps = getConfigEncap();
+
         log.debug(CONFIG_CHANGED, ifacesOfVpls);
     }
 
@@ -148,21 +295,37 @@
     /**
      * Applies a given configuration to the VPLS application.
      */
-    private void applyConfig(VplsConfig vplsConfig) {
+    private void applyConfig(VplsAppConfig vplsAppConfig) {
         loadAppId();
-        configService.applyConfig(vplsAppId, VplsConfig.class, vplsConfig.node());
+        configService.applyConfig(vplsAppId, VplsAppConfig.class, vplsAppConfig.node());
     }
 
     /**
-     * Retrieves the VPLS names and associated interfaces names from the configuration.
+     * Retrieves the VPLS names and related encapsulation types from the
+     * configuration.
      *
-     * @return a map VPLS names and associated interface names
+     * @return a map of VPLS names and associated encapsulation types
+     */
+    private Map<String, EncapsulationType> getConfigEncap() {
+        Map<String, EncapsulationType> configEncap = new HashMap<>();
+
+        vplsAppConfig.vplss().forEach(vpls -> {
+                configEncap.put(vpls.name(), vpls.encap());
+        });
+
+        return configEncap;
+    }
+
+    /**
+     * Retrieves the VPLS names and related interfaces names from the configuration.
+     *
+     * @return a map of VPLS names and related interface names
      */
     private SetMultimap<String, String> getConfigInterfaces() {
         SetMultimap<String, String> confIntfByVpls =
                 HashMultimap.create();
 
-        vplsConfig.vplsNetworks().forEach(vpls -> {
+        vplsAppConfig.vplss().forEach(vpls -> {
             if (vpls.ifaces().isEmpty()) {
                 confIntfByVpls.put(vpls.name(), EMPTY);
             } else {
@@ -174,9 +337,9 @@
     }
 
     /**
-     * Retrieves the VPLS names and associated interfaces from the configuration.
+     * Retrieves the VPLS names and related interfaces from the configuration.
      *
-     * @return a map VPLS names and associated interfaces
+     * @return a map of VPLS names and related interfaces
      */
     private SetMultimap<String, Interface> getConfigCPoints() {
         log.debug(CHECK_CONFIG);
@@ -209,127 +372,10 @@
                     case CONFIG_REMOVED:
                         loadConfiguration();
                         break;
-
                     default:
                         break;
                 }
             }
         }
     }
-
-    @Override
-    public void addVpls(String name, Set<String> ifaces) {
-        VplsNetworkConfig vpls;
-
-        if (ifacesOfVpls.containsKey(name)) {
-            if (ifaces.isEmpty()) {
-                return;
-            }
-
-            ifaces.forEach(iface ->
-                    vplsConfig.addInterfaceToVpls(name, iface));
-        } else {
-            vpls = new VplsNetworkConfig(name, ifaces);
-            vplsConfig.addVpls(vpls);
-        }
-
-        vplsAffectedByApi.add(name);
-        applyConfig(vplsConfig);
-    }
-
-    @Override
-    public void removeVpls(String name) {
-        if (ifacesOfVpls.containsKey(name)) {
-            vplsConfig.removeVpls(name);
-            vplsAffectedByApi.add(name);
-            applyConfig(vplsConfig);
-        }
-    }
-
-    @Override
-    public void addInterfaceToVpls(String name, String iface) {
-        if (ifacesOfVpls.containsKey(name)) {
-            vplsConfig.addInterfaceToVpls(name, iface);
-            vplsAffectedByApi.add(name);
-            applyConfig(vplsConfig);
-        }
-    }
-
-    @Override
-    public void removeInterfaceFromVpls(String iface) {
-        if (ifacesOfVpls.containsValue(iface)) {
-            VplsNetworkConfig vpls = vplsConfig.getVplsFromInterface(iface);
-            vplsConfig.removeInterfaceFromVpls(vpls, iface);
-            vplsAffectedByApi.add(vpls.name());
-            applyConfig(vplsConfig);
-        }
-    }
-
-    @Override
-    public void cleanVpls() {
-        ifacesOfVpls.entries().forEach(e -> {
-            vplsConfig.removeVpls(e.getKey());
-            vplsAffectedByApi.add(e.getKey());
-        });
-        applyConfig(vplsConfig);
-    }
-
-    @Override
-    public Set<String> getVplsAffectedByApi() {
-        Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectedByApi);
-
-        vplsAffectedByApi.clear();
-
-        return vplsNames;
-    }
-
-    @Override
-    public Set<Interface> getAllInterfaces() {
-        Set<Interface> allInterfaces = new HashSet<>();
-        vplsNetworks.values().forEach(allInterfaces::add);
-
-        return allInterfaces;
-    }
-
-    @Override
-    public Set<Interface> getVplsInterfaces(String name) {
-        Set<Interface> vplsInterfaces = new HashSet<>();
-        vplsNetworks.get(name).forEach(vplsInterfaces::add);
-
-        return vplsInterfaces;
-    }
-
-    @Override
-    public Set<String> getAllVpls() {
-        return ifacesOfVpls.keySet();
-    }
-
-    @Override
-    public Set<String> getOldVpls() {
-        return oldIfacesOfVpls.keySet();
-    }
-
-    @Override
-    public SetMultimap<String, Interface> getVplsNetworks() {
-        return ImmutableSetMultimap.copyOf(vplsNetworks);
-    }
-
-    @Override
-    public SetMultimap<String, Interface> getVplsNetwork(VlanId vlan,
-                                                        ConnectPoint connectPoint) {
-        String vplsNetworkName =
-                vplsNetworks.entries().stream()
-                        .filter(e -> e.getValue().connectPoint().equals(connectPoint))
-                        .filter(e -> e.getValue().vlan().equals(vlan))
-                        .map(e -> e.getKey())
-                        .findFirst()
-                        .orElse(null);
-        SetMultimap<String, Interface> result = HashMultimap.create();
-        if (vplsNetworkName != null && vplsNetworks.containsKey(vplsNetworkName)) {
-            vplsNetworks.get(vplsNetworkName)
-                    .forEach(intf -> result.put(vplsNetworkName, intf));
-            return result;
-        }
-        return null;
-    }
 }
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/package-info.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/package-info.java
index 3229c7b..b5d6ec9 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/package-info.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/impl/package-info.java
@@ -15,6 +15,6 @@
  */
 
 /**
- * Configuration implementation  to create L2 broadcast network using VLAN.
+ * Configuration service implementation for VPLS.
  */
 package org.onosproject.vpls.config.impl;
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/config/package-info.java b/apps/vpls/src/main/java/org/onosproject/vpls/config/package-info.java
index f080044..a0a94d2 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/config/package-info.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/config/package-info.java
@@ -17,4 +17,4 @@
 /**
  * Configuration to create L2 broadcast network using VLAN.
  */
-package org.onosproject.vpls.config;
+package org.onosproject.vpls.config;
\ No newline at end of file