[ONOS-2831] Refactor L2 code according to the L3 plan. 

Change-Id: Ibc9dc47a18208b9b1602261064a33bc63b131fc3
diff --git a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/DataPathIdGenerator.java b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/DataPathIdGenerator.java
new file mode 100644
index 0000000..c241347
--- /dev/null
+++ b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/DataPathIdGenerator.java
@@ -0,0 +1,64 @@
+package org.onosproject.vtn.util;
+
+import static org.onlab.util.Tools.toHex;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Calendar;
+
+import org.onosproject.core.IdGenerator;
+import org.onosproject.net.DeviceId;
+
+public final class DataPathIdGenerator implements IdGenerator {
+    private static final String SCHEME = "of";
+    private String ipAddress;
+    private String timeStamp;
+
+    private DataPathIdGenerator(Builder builder) {
+        this.ipAddress = builder.ipAddress;
+        Calendar cal = Calendar.getInstance();
+        this.timeStamp = String.valueOf(cal.get(Calendar.SECOND))
+                + String.valueOf(cal.get(Calendar.MILLISECOND));
+    }
+
+    @Override
+    public long getNewId() {
+        String dpid = ipAddress.replace(".", "") + timeStamp;
+        return Long.parseLong(dpid);
+    }
+
+    public String getDpId() {
+        return toHex(getNewId());
+    }
+
+    public DeviceId getDeviceId() {
+        try {
+            URI uri = new URI(SCHEME, toHex(getNewId()), null);
+            return DeviceId.deviceId(uri);
+        } catch (URISyntaxException e) {
+            return null;
+        }
+    }
+
+    /**
+     * Returns a new builder.
+     *
+     * @return new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static final class Builder {
+        private String ipAddress;
+
+        public Builder addIpAddress(String ipAddress) {
+            this.ipAddress = ipAddress;
+            return this;
+        }
+
+        public DataPathIdGenerator build() {
+            return new DataPathIdGenerator(this);
+        }
+    }
+}
diff --git a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnConfig.java b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnConfig.java
new file mode 100644
index 0000000..1f98e55
--- /dev/null
+++ b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnConfig.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2015 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.vtn.util;
+
+import java.util.Set;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.BridgeConfig;
+import org.onosproject.net.behaviour.BridgeName;
+import org.onosproject.net.behaviour.DefaultTunnelDescription;
+import org.onosproject.net.behaviour.IpTunnelEndPoint;
+import org.onosproject.net.behaviour.TunnelConfig;
+import org.onosproject.net.behaviour.TunnelDescription;
+import org.onosproject.net.behaviour.TunnelEndPoint;
+import org.onosproject.net.driver.DriverHandler;
+
+/**
+ * Applies configuration to the device.
+ */
+public final class VtnConfig {
+
+    private static final String DEFAULT_BRIDGE_NAME = "br-int";
+
+    /**
+     * Constructs a vtn config object. Utility classes should not have a
+     * public or default constructor, otherwise IDE will compile unsuccessfully. This
+     * class should not be instantiated.
+     */
+    private VtnConfig() {
+    }
+
+    /**
+     * Creates or update bridge in the controller device.
+     *
+     * @param handler DriverHandler
+     * @param dpid datapath id
+     * @param exPortName external port name
+     */
+    public static void applyBridgeConfig(DriverHandler handler, String dpid, String exPortName) {
+        BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
+        bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE_NAME), dpid, exPortName);
+    }
+
+    /**
+     * Creates or update tunnel in the controller device.
+     *
+     * @param handler DriverHandler
+     * @param srcIp the ipAddress of the local controller device
+     * @param dstIp the ipAddress of the remote controller device
+     */
+    public static void applyTunnelConfig(DriverHandler handler, IpAddress srcIp,
+                                  IpAddress dstIp) {
+        TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
+        TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
+        TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
+        TunnelDescription tunnel = new DefaultTunnelDescription(
+                                                                tunnelAsSrc,
+                                                                tunnelAsDst,
+                                                                TunnelDescription.Type.VXLAN,
+                                                                null);
+        tunnelConfig.createTunnel(tunnel);
+    }
+
+    /**
+     * Creates or update tunnel in the controller device.
+     *
+     * @param handler DriverHandler
+     * @param srcIp the ipAddress of the local controller device
+     * @param dstIp the ipAddress of the remote controller device
+     */
+    public static void removeTunnelConfig(DriverHandler handler, IpAddress srcIp,
+                                   IpAddress dstIp) {
+        TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
+        TunnelEndPoint tunnelAsSrc = IpTunnelEndPoint.ipTunnelPoint(srcIp);
+        TunnelEndPoint tunnelAsDst = IpTunnelEndPoint.ipTunnelPoint(dstIp);
+        TunnelDescription tunnel = new DefaultTunnelDescription(
+                                                                tunnelAsSrc,
+                                                                tunnelAsDst,
+                                                                TunnelDescription.Type.VXLAN,
+                                                                null);
+        tunnelConfig.removeTunnel(tunnel);
+    }
+
+    /**
+     * Gets ports in the controller device.
+     *
+     * @param handler DriverHandler
+     */
+    public static Set<PortNumber> getPortNumbers(DriverHandler handler) {
+        BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
+        return bridgeConfig.getPortNumbers();
+    }
+
+}
diff --git a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnData.java b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnData.java
new file mode 100644
index 0000000..a8562e7
--- /dev/null
+++ b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/VtnData.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2015 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.vtn.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+
+/**
+ * VtnData utility class.
+ */
+public final class VtnData {
+
+    private static final Logger log = LoggerFactory.getLogger(VtnData.class);
+    private static final String SWITCH_CHANNEL_ID = "channelId";
+    private static final String PORT_HEAD = "vxlan";
+
+    /**
+     * Constructs a VtnData object. Utility classes should not have a public or
+     * default constructor, otherwise IDE will compile unsuccessfully. This
+     * class should not be instantiated.
+     */
+    private VtnData() {
+    }
+
+    /**
+     * Get the ControllerIp from the device .
+     *
+     * @param device Device
+     * @return Controller Ip
+     */
+    public static String getControllerIpOfSwitch(Device device) {
+        String url = device.annotations().value(SWITCH_CHANNEL_ID);
+        return url.substring(0, url.lastIndexOf(":"));
+    }
+
+    /**
+     * Get the ControllerId from the device .
+     *
+     * @param device Device
+     * @param devices Devices
+     * @return Controller Id
+     */
+    public static DeviceId getControllerId(Device device,
+                                           Iterable<Device> devices) {
+        for (Device d : devices) {
+            if (d.type() == Device.Type.CONTROLLER && d.id().toString()
+                    .contains(getControllerIpOfSwitch(device))) {
+                return d.id();
+            }
+        }
+        log.info("Can not find controller for device : {}", device.id());
+        return null;
+    }
+
+    /**
+     * Get local tunnel ports.
+     *
+     * @param ports Iterable of Port
+     * @return Collection of PortNumber
+     */
+    public static Collection<PortNumber> getLocalTunnelPorts(Iterable<Port> ports) {
+        Collection<PortNumber> localTunnelPorts = new ArrayList<>();
+        Sets.newHashSet(ports).stream()
+                .filter(p -> !p.number().equals(PortNumber.LOCAL))
+                .forEach(p -> {
+                    if (p.annotations().value(AnnotationKeys.PORT_NAME)
+                            .startsWith(PORT_HEAD)) {
+                        localTunnelPorts.add(p.number());
+                    }
+                });
+        return localTunnelPorts;
+    }
+
+}
diff --git a/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/package-info.java b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/package-info.java
new file mode 100644
index 0000000..213b9e2
--- /dev/null
+++ b/apps/vtn/vtnmgr/src/main/java/org/onosproject/vtn/util/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * VTN application that applies configuration and flows to the device.
+ */
+package org.onosproject.vtn.util;