[ONOS-6060] Merge scalablegateway app to openstacknode app

Change-Id: I8e152a943f575c2fc0305ae572b5e0150a699d30
diff --git a/apps/openstacknetworking/BUCK b/apps/openstacknetworking/BUCK
index 519f134..86ec137 100644
--- a/apps/openstacknetworking/BUCK
+++ b/apps/openstacknetworking/BUCK
@@ -6,7 +6,6 @@
   '//utils/rest:onlab-rest',
   '//cli:onos-cli',
   '//apps/openstacknode:onos-apps-openstacknode',
-  '//apps/scalablegateway:onos-apps-scalablegateway',
   '//lib:openstack4j-core',
 ]
 
@@ -26,7 +25,6 @@
   url = 'http://onosproject.org',
   description = 'OpenStack Networking application.',
   required_apps = [
-    'org.onosproject.openstacknode',
-    'org.onosproject.scalablegateway'
+    'org.onosproject.openstacknode'
   ]
 )
diff --git a/apps/openstacknetworking/pom.xml b/apps/openstacknetworking/pom.xml
index d9ab54e..6d0bd2c 100644
--- a/apps/openstacknetworking/pom.xml
+++ b/apps/openstacknetworking/pom.xml
@@ -35,8 +35,7 @@
         <onos.app.url>http://onosproject.org</onos.app.url>
         <onos.app.readme>OpenStack networking application</onos.app.readme>
         <onos.app.requires>
-            org.onosproject.openstacknode,
-            org.onosproject.scalablegateway
+            org.onosproject.openstacknode
         </onos.app.requires>
         <web.context>/onos/openstacknetworking</web.context>
         <api.version>1.0.0</api.version>
@@ -104,11 +103,6 @@
             <version>${project.version}</version>
         </dependency>
         <dependency>
-            <groupId>org.onosproject</groupId>
-            <artifactId>onos-app-scalablegateway</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
             <groupId>javax.ws.rs</groupId>
             <artifactId>javax.ws.rs-api</artifactId>
             <version>2.0.1</version>
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingArpHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingArpHandler.java
index 03810c3..f99d2ae 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingArpHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingArpHandler.java
@@ -33,8 +33,8 @@
 import org.onosproject.net.packet.PacketProcessor;
 import org.onosproject.net.packet.PacketService;
 import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
-import org.onosproject.scalablegateway.api.ScalableGatewayService;
 import org.onosproject.openstacknetworking.api.Constants;
+import org.onosproject.openstacknode.OpenstackNodeService;
 import org.slf4j.Logger;
 
 import java.nio.ByteBuffer;
@@ -60,10 +60,10 @@
     protected PacketService packetService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ScalableGatewayService gatewayService;
+    protected OpenstackNetworkService osNetworkService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected OpenstackNetworkService osNetworkService;
+    protected OpenstackNodeService osNodeService;
 
     private final ExecutorService eventExecutor = newSingleThreadExecutor(
             groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
@@ -123,7 +123,7 @@
         public void process(PacketContext context) {
             if (context.isHandled()) {
                 return;
-            } else if (!gatewayService.getGatewayDeviceIds().contains(
+            } else if (!osNodeService.gatewayDeviceIds().contains(
                     context.inPacket().receivedFrom().deviceId())) {
                 // return if the packet is not from gateway nodes
                 return;
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingFloatingIpHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingFloatingIpHandler.java
index 8594aea..dcb6555 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingFloatingIpHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingFloatingIpHandler.java
@@ -48,7 +48,6 @@
 import org.onosproject.openstacknode.OpenstackNodeEvent;
 import org.onosproject.openstacknode.OpenstackNodeListener;
 import org.onosproject.openstacknode.OpenstackNodeService;
-import org.onosproject.scalablegateway.api.ScalableGatewayService;
 import org.openstack4j.model.network.NetFloatingIP;
 import org.openstack4j.model.network.Network;
 import org.openstack4j.model.network.Port;
@@ -102,9 +101,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected OpenstackNetworkService osNetworkService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ScalableGatewayService gatewayService;
-
     private final ExecutorService eventExecutor = newSingleThreadExecutor(
             groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
     private final OpenstackRouterListener floatingIpLisener = new InternalFloatingIpLisener();
@@ -193,7 +189,7 @@
                 .matchIPDst(floating.toIpPrefix())
                 .build();
 
-        gatewayService.getGatewayDeviceIds().forEach(gnodeId -> {
+        osNodeService.gatewayDeviceIds().forEach(gnodeId -> {
             TrafficTreatment externalTreatment = DefaultTrafficTreatment.builder()
                     .setEthSrc(Constants.DEFAULT_GATEWAY_MAC)
                     .setEthDst(instPort.macAddress())
@@ -258,12 +254,12 @@
                 .matchIPSrc(instPort.ipAddress().toIpPrefix())
                 .build();
 
-        gatewayService.getGatewayDeviceIds().forEach(gnodeId -> {
+        osNodeService.gatewayDeviceIds().forEach(gnodeId -> {
             TrafficTreatment treatment = DefaultTrafficTreatment.builder()
                     .setIpSrc(floating.getIp4Address())
                     .setEthSrc(Constants.DEFAULT_GATEWAY_MAC)
                     .setEthDst(Constants.DEFAULT_EXTERNAL_ROUTER_MAC)
-                    .setOutput(gatewayService.getUplinkPort(gnodeId))
+                    .setOutput(osNodeService.externalPort(gnodeId).get())
                     .build();
 
             RulePopulatorUtil.setRule(
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingHandler.java
index 7d722ec..4cace0f 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingHandler.java
@@ -49,7 +49,6 @@
 import org.onosproject.openstacknode.OpenstackNodeEvent;
 import org.onosproject.openstacknode.OpenstackNodeListener;
 import org.onosproject.openstacknode.OpenstackNodeService;
-import org.onosproject.scalablegateway.api.ScalableGatewayService;
 import org.openstack4j.model.network.ExternalGateway;
 import org.openstack4j.model.network.Network;
 import org.openstack4j.model.network.Router;
@@ -96,9 +95,6 @@
     protected OpenstackNodeService osNodeService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ScalableGatewayService gatewayService;
-
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected OpenstackNetworkService osNetworkService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -195,14 +191,14 @@
                 .forEach(osNode -> {
                     setRulesToGateway(
                             osNode.intBridge(),
-                            gatewayService.getGatewayGroupId(osNode.intBridge()),
+                            osNodeService.gatewayGroupId(osNode.intBridge()),
                             Long.valueOf(osNet.getProviderSegID()),
                             IpPrefix.valueOf(osSubnet.getCidr()),
                             install);
                 });
 
         // take the first outgoing packet to controller for source NAT
-        gatewayService.getGatewayDeviceIds()
+        osNodeService.gatewayDeviceIds()
                 .forEach(gwDeviceId -> setRulesToController(
                         gwDeviceId,
                         Long.valueOf(osNet.getProviderSegID()),
@@ -225,13 +221,13 @@
                 .filter(osNode -> osNode.type() == COMPUTE)
                 .forEach(osNode -> setRulesToGatewayWithDstIp(
                         osNode.intBridge(),
-                        gatewayService.getGatewayGroupId(osNode.intBridge()),
+                        osNodeService.gatewayGroupId(osNode.intBridge()),
                         Long.valueOf(network.getProviderSegID()),
                         IpAddress.valueOf(osSubnet.getGateway()),
                         install));
 
         IpAddress gatewayIp = IpAddress.valueOf(osSubnet.getGateway());
-        gatewayService.getGatewayDeviceIds()
+        osNodeService.gatewayDeviceIds()
                 .forEach(gwDeviceId -> setGatewayIcmpRule(
                         gatewayIp,
                         gwDeviceId,
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingIcmpHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingIcmpHandler.java
index 3f5adee..ea716db 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingIcmpHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingIcmpHandler.java
@@ -51,8 +51,6 @@
 import org.onosproject.openstacknode.OpenstackNodeEvent;
 import org.onosproject.openstacknode.OpenstackNodeListener;
 import org.onosproject.openstacknode.OpenstackNodeService;
-import org.onosproject.scalablegateway.api.GatewayNode;
-import org.onosproject.scalablegateway.api.ScalableGatewayService;
 import org.openstack4j.model.network.ExternalGateway;
 import org.openstack4j.model.network.IP;
 import org.openstack4j.model.network.Port;
@@ -100,9 +98,6 @@
     protected MastershipService mastershipService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ScalableGatewayService gatewayService;
-
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected OpenstackNodeService osNodeService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -147,7 +142,7 @@
                 .matchIPProtocol(IPv4.PROTOCOL_ICMP)
                 .build();
 
-        gatewayService.getGatewayDeviceIds().forEach(gateway -> {
+        osNodeService.gatewayDeviceIds().forEach(gateway -> {
             packetService.requestPackets(
                     icmpSelector,
                     PacketPriority.CONTROL,
@@ -335,7 +330,7 @@
                 .setPayload(ipPacket);
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .setOutput(gatewayService.getUplinkPort(srcDevice))
+                .setOutput(osNodeService.externalPort(srcDevice).get())
                 .build();
 
         OutboundPacket packet = new DefaultOutboundPacket(
@@ -386,7 +381,7 @@
         public void process(PacketContext context) {
             if (context.isHandled()) {
                 return;
-            } else if (!gatewayService.getGatewayDeviceIds().contains(
+            } else if (!osNodeService.gatewayDeviceIds().contains(
                     context.inPacket().receivedFrom().deviceId())) {
                 // return if the packet is not from gateway nodes
                 return;
@@ -423,14 +418,6 @@
                     if (osNode.type() == GATEWAY) {
                         log.info("GATEWAY node {} detected", osNode.hostname());
                         eventExecutor.execute(() -> {
-                            // TODO add events to scalable gateway so that we
-                            // don't need to add gateway here and there
-                            GatewayNode gnode = GatewayNode.builder()
-                                    .gatewayDeviceId(osNode.intBridge())
-                                    .dataIpAddress(osNode.dataIp().get().getIp4Address())
-                                    .uplinkIntf(osNode.externalPortName().get())
-                                    .build();
-                            gatewayService.addGatewayNode(gnode);
                             requestPacket(appId);
                         });
                     }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingSnatHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingSnatHandler.java
index a1a1899..eb08cc3 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingSnatHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackRoutingSnatHandler.java
@@ -50,7 +50,6 @@
 import org.onosproject.openstacknetworking.api.OpenstackRouterService;
 import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
 import org.onosproject.openstacknode.OpenstackNodeService;
-import org.onosproject.scalablegateway.api.ScalableGatewayService;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
 import org.onosproject.store.service.DistributedSet;
@@ -118,9 +117,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected OpenstackRouterService osRouterService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ScalableGatewayService gatewayService;
-
     private final ExecutorService eventExecutor = newSingleThreadExecutor(
             groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
     private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor();
@@ -311,7 +307,7 @@
                 break;
         }
 
-        gatewayService.getGatewayDeviceIds().forEach(deviceId -> {
+        osNodeService.gatewayDeviceIds().forEach(deviceId -> {
             DeviceId srcDeviceId = srcInstPort.deviceId();
             TrafficTreatment.Builder tmpBuilder =
                     DefaultTrafficTreatment.builder(tBuilder.build());
@@ -367,10 +363,10 @@
         }
 
         tBuilder.setIpSrc(externalIp);
-        gatewayService.getGatewayDeviceIds().forEach(deviceId -> {
+        osNodeService.gatewayDeviceIds().forEach(deviceId -> {
             TrafficTreatment.Builder tmpBuilder =
                     DefaultTrafficTreatment.builder(tBuilder.build());
-            tmpBuilder.setOutput(gatewayService.getUplinkPort(deviceId));
+            tmpBuilder.setOutput(osNodeService.externalPort(deviceId).get());
             ForwardingObjective fo = DefaultForwardingObjective.builder()
                     .withSelector(sBuilder.build())
                     .withTreatment(tmpBuilder.build())
@@ -415,7 +411,7 @@
         ethPacketIn.setPayload(iPacket);
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .setOutput(gatewayService.getUplinkPort(srcDevice))
+                .setOutput(osNodeService.externalPort(srcDevice).get())
                 .build();
         ethPacketIn.resetChecksum();
         packetService.emit(new DefaultOutboundPacket(
@@ -459,7 +455,7 @@
         public void process(PacketContext context) {
             if (context.isHandled()) {
                 return;
-            } else if (!gatewayService.getGatewayDeviceIds().contains(
+            } else if (!osNodeService.gatewayDeviceIds().contains(
                     context.inPacket().receivedFrom().deviceId())) {
                 // return if the packet is not from gateway nodes
                 return;
diff --git a/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeManager.java b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeManager.java
index 5276c58..0bf78bf 100644
--- a/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeManager.java
+++ b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeManager.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.openstacknode;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -35,6 +37,7 @@
 import org.onosproject.cluster.NodeId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
+import org.onosproject.core.GroupId;
 import org.onosproject.event.ListenerRegistry;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
@@ -60,6 +63,10 @@
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceListener;
 import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupKey;
+import org.onosproject.net.group.GroupService;
 import org.onosproject.openstacknode.OpenstackNodeEvent.NodeState;
 import org.onosproject.ovsdb.controller.OvsdbClientService;
 import org.onosproject.ovsdb.controller.OvsdbController;
@@ -113,6 +120,7 @@
     private static final int DPID_BEGIN = 3;
 
     private static final String APP_ID = "org.onosproject.openstacknode";
+
     private static final Class<OpenstackNodeConfig> CONFIG_CLASS = OpenstackNodeConfig.class;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -139,6 +147,12 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected LeadershipService leadershipService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DriverService driverService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GroupService groupService;
+
     @Property(name = OVSDB_PORT, intValue = DEFAULT_OVSDB_PORT,
             label = "OVSDB server listen port")
     private int ovsdbPort = DEFAULT_OVSDB_PORT;
@@ -164,6 +178,7 @@
 
     private ConsistentMap<String, OpenstackNode> nodeStore;
 
+    private SelectGroupHandler selectGroupHandler;
     private ApplicationId appId;
     private NodeId localNodeId;
 
@@ -187,6 +202,8 @@
         configRegistry.addListener(configListener);
         componentConfigService.registerProperties(getClass());
 
+        selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId);
+
         readConfiguration();
         log.info("Started");
     }
@@ -275,12 +292,25 @@
     @Override
     public void processCompleteState(OpenstackNode node) {
         process(new OpenstackNodeEvent(COMPLETE, node));
+        switch (node.type()) {
+            case COMPUTE:
+                selectGroupHandler.createGatewayGroup(node.intBridge(), gatewayNodes());
+                break;
+            case GATEWAY:
+                updateGatewayGroup(node, true);
+                break;
+            default:
+                break;
+        }
         log.info("Finished init {}", node.hostname());
     }
 
     @Override
     public void processIncompleteState(OpenstackNode node) {
         process(new OpenstackNodeEvent(INCOMPLETE, node));
+        if (node.type().equals(NodeType.GATEWAY)) {
+            updateGatewayGroup(node, false);
+        }
     }
 
     @Override
@@ -331,6 +361,65 @@
                 .map(Port::number).findFirst();
     }
 
+    @Override
+    public OpenstackNode gatewayNode(DeviceId deviceId) {
+        OpenstackNode gatewayNode = nodeByDeviceId(deviceId);
+        if (gatewayNode == null) {
+            log.warn("Gateway with device ID {} does not exist");
+            return null;
+        }
+        return gatewayNode;
+    }
+
+    @Override
+    public synchronized GroupId gatewayGroupId(DeviceId srcDeviceId) {
+        GroupKey groupKey = selectGroupHandler.getGroupKey(srcDeviceId);
+        Group group = groupService.getGroup(srcDeviceId, groupKey);
+        if (group == null) {
+            log.info("Created gateway group for {}", srcDeviceId);
+            return selectGroupHandler.createGatewayGroup(srcDeviceId, gatewayNodes());
+        } else {
+            return group.id();
+        }
+    }
+
+    @Override
+    public List<OpenstackNode> gatewayNodes() {
+        return nodeStore.values()
+                .stream()
+                .map(Versioned::value)
+                .filter(node -> node.type().equals(NodeType.GATEWAY))
+                .filter(node -> node.state().equals(COMPLETE))
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public List<DeviceId> gatewayDeviceIds() {
+        List<DeviceId> deviceIdList = Lists.newArrayList();
+
+        nodeStore.values()
+                .stream()
+                .map(Versioned::value)
+                .filter(node -> node.type().equals(NodeType.GATEWAY))
+                .filter(node -> node.state().equals(COMPLETE))
+                .forEach(node -> deviceIdList.add(node.intBridge()));
+        return deviceIdList;
+    }
+
+    private void updateGatewayGroup(OpenstackNode gatewayNode, boolean isInsert) {
+        nodeStore.values()
+                .stream()
+                .map(Versioned::value)
+                .filter(node -> node.type().equals(NodeType.COMPUTE))
+                .filter(node -> node.state().equals(COMPLETE))
+                .forEach(node -> {
+                    selectGroupHandler.updateGatewayGroupBuckets(node.intBridge(),
+                            ImmutableList.of(gatewayNode),
+                            isInsert);
+                    log.trace("Updated gateway group on {}", node.intBridge());
+                });
+    }
+
     private void initNode(OpenstackNode node) {
         NodeState state = node.state();
         state.process(this, node);
@@ -675,6 +764,7 @@
                         eventExecutor.execute(() -> handler.connected(device));
                     } else {
                         eventExecutor.execute(() -> handler.disconnected(device));
+                        log.warn("OpenstackNode with device ID {} is disconnected", device.id());
                     }
                     break;
                 default:
diff --git a/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeService.java b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeService.java
index f7c16ba..0fb6e77 100644
--- a/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeService.java
+++ b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/OpenstackNodeService.java
@@ -16,6 +16,7 @@
 package org.onosproject.openstacknode;
 
 import org.onlab.packet.IpAddress;
+import org.onosproject.core.GroupId;
 import org.onosproject.event.ListenerService;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
@@ -127,4 +128,34 @@
      * @return port number; or empty value
      */
     Optional<PortNumber> externalPort(DeviceId intBridgeId);
+    /**
+     * Returns gateway node with the given device identifier.
+     *
+     * @param deviceId The gateway node deviceId
+     * @return The gateway node information
+     */
+    OpenstackNode gatewayNode(DeviceId deviceId);
+
+    /**
+     * Returns group id for gateway load balance.
+     * If the group does not exist in the supplied source device, creates one.
+     *
+     * @param srcDeviceId source device id
+     * @return The group id
+     */
+    GroupId gatewayGroupId(DeviceId srcDeviceId);
+
+    /**
+     * Returns the list of gateway node information with the given device identifier.
+     *
+     * @return The list of gateway node information
+     */
+    List<OpenstackNode> gatewayNodes();
+
+    /**
+     * Returns the list of gateway`s device identifiers.
+     *
+     * @return The list of device identifier]
+     */
+    List<DeviceId> gatewayDeviceIds();
 }
diff --git a/apps/openstacknode/src/main/java/org/onosproject/openstacknode/SelectGroupHandler.java b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/SelectGroupHandler.java
new file mode 100644
index 0000000..df963db
--- /dev/null
+++ b/apps/openstacknode/src/main/java/org/onosproject/openstacknode/SelectGroupHandler.java
@@ -0,0 +1,206 @@
+/*
+ * 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.openstacknode;
+
+import com.google.common.collect.Lists;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.GroupId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DefaultDriverData;
+import org.onosproject.net.driver.DefaultDriverHandler;
+import org.onosproject.net.driver.Driver;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.instructions.ExtensionPropertyException;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
+import org.onosproject.net.group.DefaultGroupDescription;
+import org.onosproject.net.group.DefaultGroupKey;
+import org.onosproject.net.group.GroupBucket;
+import org.onosproject.net.group.GroupBuckets;
+import org.onosproject.net.group.GroupDescription;
+import org.onosproject.net.group.GroupKey;
+import org.onosproject.net.group.GroupService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import static org.onosproject.net.AnnotationKeys.PORT_NAME;
+import static org.onosproject.net.group.DefaultGroupBucket.createSelectGroupBucket;
+
+/**
+ * Handles group generation request from OpenstackNode.
+ */
+public class SelectGroupHandler {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private static final String TUNNEL_DESTINATION = "tunnelDst";
+    private static final String PORTNAME_PREFIX_TUNNEL = "vxlan";
+
+    private final GroupService groupService;
+    private final DeviceService deviceService;
+    private final DriverService driverService;
+    private final ApplicationId appId;
+
+    /**
+     * Default constructor.
+     *
+     * @param targetGroupService group service
+     * @param targetDeviceService device service
+     * @param targetDriverService driver service
+     * @param appId application id for group service
+     */
+    public SelectGroupHandler(GroupService targetGroupService, DeviceService targetDeviceService,
+                              DriverService targetDriverService, ApplicationId appId) {
+        groupService = targetGroupService;
+        deviceService = targetDeviceService;
+        driverService = targetDriverService;
+        this.appId = appId;
+    }
+
+    /**
+     * Creates select type group description according to given deviceId.
+     *
+     * @param srcDeviceId target device id for group description
+     * @param gatewayNodeList gateway node list for bucket action
+     * @return created select type group description
+     */
+    public GroupId createGatewayGroup(DeviceId srcDeviceId, List<OpenstackNode> gatewayNodeList) {
+        List<GroupBucket> bucketList = generateBucketsForSelectGroup(srcDeviceId, gatewayNodeList);
+        GroupId groupId = getGroupId(srcDeviceId);
+        GroupDescription groupDescription = new DefaultGroupDescription(
+                srcDeviceId,
+                GroupDescription.Type.SELECT,
+                new GroupBuckets(bucketList),
+                getGroupKey(srcDeviceId),
+                groupId.id(),
+                appId);
+
+        groupService.addGroup(groupDescription);
+        return groupId;
+    }
+
+    /**
+     * Returns unique group key with supplied source device ID as a hash.
+     *
+     * @param srcDeviceId source device id
+     * @return group key
+     */
+    public GroupKey getGroupKey(DeviceId srcDeviceId) {
+        return new DefaultGroupKey(srcDeviceId.toString().getBytes());
+    }
+
+    private GroupId getGroupId(DeviceId srcDeviceId) {
+        return new GroupId(srcDeviceId.toString().hashCode());
+    }
+
+    /**
+     * Updates groupBuckets in select type group.
+     *
+     * @param deviceId target device id to update the group
+     * @param gatewayNodeList updated gateway node list for bucket action
+     * @param isInsert update type(add or remove)
+     */
+    public void updateGatewayGroupBuckets(DeviceId deviceId,
+                                          List<OpenstackNode> gatewayNodeList,
+                                          boolean isInsert) {
+        List<GroupBucket> bucketList = generateBucketsForSelectGroup(deviceId, gatewayNodeList);
+        GroupKey groupKey = getGroupKey(deviceId);
+        if (groupService.getGroup(deviceId, groupKey) == null) {
+            log.error("There's no group in compute node {}", deviceId);
+            return;
+        }
+
+        if (isInsert) {
+            groupService.addBucketsToGroup(
+                    deviceId,
+                    groupKey,
+                    new GroupBuckets(bucketList),
+                    groupKey, appId);
+        } else {
+            groupService.removeBucketsFromGroup(
+                    deviceId,
+                    groupKey,
+                    new GroupBuckets(bucketList),
+                    groupKey, appId);
+        }
+    }
+
+    private List<GroupBucket> generateBucketsForSelectGroup(DeviceId deviceId, List<OpenstackNode> gatewayNodeList) {
+        List<GroupBucket> bucketList = Lists.newArrayList();
+        gatewayNodeList.forEach(node -> {
+            TrafficTreatment tBuilder = DefaultTrafficTreatment.builder()
+                    .extension(buildNiciraExtenstion(deviceId, node.dataIp().get().getIp4Address()), deviceId)
+                    .setOutput(getTunnelPort(deviceId))
+                    .build();
+            bucketList.add(createSelectGroupBucket(tBuilder));
+        });
+        return bucketList;
+    }
+
+    /**
+     * Builds Nicira extension for tagging remoteIp of vxlan.
+     *
+     * @param id device id of vxlan source device
+     * @param hostIp remote ip of vxlan destination device
+     * @return NiciraExtension Treatment
+     */
+    private ExtensionTreatment buildNiciraExtenstion(DeviceId id, Ip4Address hostIp) {
+        Driver driver = driverService.getDriver(id);
+        DriverHandler driverHandler = new DefaultDriverHandler(new DefaultDriverData(driver, id));
+        ExtensionTreatmentResolver resolver = driverHandler.behaviour(ExtensionTreatmentResolver.class);
+
+        ExtensionTreatment extensionInstruction =
+                resolver.getExtensionInstruction(
+                        ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type());
+
+        try {
+            extensionInstruction.setPropertyValue(TUNNEL_DESTINATION, hostIp);
+        } catch (ExtensionPropertyException e) {
+            log.error("Error setting Nicira extension setting {}", e);
+        }
+
+        return extensionInstruction;
+    }
+
+    /**
+     * Returns port number of vxlan tunnel.
+     *
+     * @param deviceId target Device Id
+     * @return portNumber
+     */
+    private PortNumber getTunnelPort(DeviceId deviceId) {
+        Port port = deviceService.getPorts(deviceId).stream()
+                .filter(p -> p.annotations().value(PORT_NAME).equals(PORTNAME_PREFIX_TUNNEL))
+                .findAny().orElse(null);
+
+        if (port == null) {
+            log.error("No TunnelPort was created.");
+            return null;
+        }
+        return port.number();
+
+    }
+}