cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ni...@apache.org
Subject [10/57] [abbrv] [partial] merge master
Date Mon, 13 May 2013 12:25:40 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java
new file mode 100644
index 0000000..1bc8973
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java
@@ -0,0 +1,621 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.persistence.TableGenerator;
+
+import org.apache.cloudstack.acl.ControlledEntity.ACLType;
+import org.springframework.stereotype.Component;
+
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.network.Network.State;
+import com.cloud.network.Network.Event;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.Networks.Mode;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.NetworkOfferingVO;
+import com.cloud.offerings.dao.NetworkOfferingDao;
+import com.cloud.offerings.dao.NetworkOfferingDaoImpl;
+import com.cloud.server.ResourceTag.TaggedResourceType;
+import com.cloud.tags.dao.ResourceTagDao;
+import com.cloud.tags.dao.ResourceTagsDaoImpl;
+import com.cloud.utils.db.*;
+import com.cloud.utils.db.JoinBuilder.JoinType;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.net.NetUtils;
+
+@Component
+@Local(value = NetworkDao.class)
+@DB(txn = false)
+public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements NetworkDao {
+    SearchBuilder<NetworkVO> AllFieldsSearch;
+    SearchBuilder<NetworkVO> AccountSearch;
+    SearchBuilder<NetworkVO> RelatedConfigSearch;
+    SearchBuilder<NetworkVO> AccountNetworkSearch;
+    SearchBuilder<NetworkVO> ZoneBroadcastUriSearch;
+    SearchBuilder<NetworkVO> ZoneSecurityGroupSearch;
+    GenericSearchBuilder<NetworkVO, Integer> CountBy;
+    SearchBuilder<NetworkVO> PhysicalNetworkSearch;
+    SearchBuilder<NetworkVO> SecurityGroupSearch;
+    GenericSearchBuilder<NetworkVO, Long> NetworksRegularUserCanCreateSearch;
+    GenericSearchBuilder<NetworkVO, Integer> NetworksCount;
+    SearchBuilder<NetworkVO> SourceNATSearch;
+    GenericSearchBuilder<NetworkVO, Long>  CountByZoneAndURI;
+    GenericSearchBuilder<NetworkVO, Long> VpcNetworksCount;
+    SearchBuilder<NetworkVO> OfferingAccountNetworkSearch;
+
+    GenericSearchBuilder<NetworkVO, Long> GarbageCollectedSearch;
+    
+    
+    
+    @Inject ResourceTagDao _tagsDao;
+    @Inject NetworkAccountDao _accountsDao;
+    @Inject NetworkDomainDao _domainsDao;
+    @Inject NetworkOpDao _opDao;
+    @Inject NetworkServiceMapDao _ntwkSvcMap;
+    @Inject NetworkOfferingDao _ntwkOffDao;
+    @Inject NetworkOpDao _ntwkOpDao;
+
+    TableGenerator _tgMacAddress;
+
+    Random _rand = new Random(System.currentTimeMillis());
+    long _prefix = 0x2;
+
+    public NetworkDaoImpl() {
+    }
+
+    @PostConstruct
+    protected void init() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
+        AllFieldsSearch.and("cidr", AllFieldsSearch.entity().getCidr(), Op.EQ);
+        AllFieldsSearch.and("broadcastType", AllFieldsSearch.entity().getBroadcastDomainType(), Op.EQ);
+        AllFieldsSearch.and("offering", AllFieldsSearch.entity().getNetworkOfferingId(), Op.EQ);
+        AllFieldsSearch.and("datacenter", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
+        AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
+        AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
+        AllFieldsSearch.and("guestType", AllFieldsSearch.entity().getGuestType(), Op.EQ);
+        AllFieldsSearch.and("physicalNetwork", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
+        AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
+        SearchBuilder<NetworkOfferingVO> join1 = _ntwkOffDao.createSearchBuilder();
+        join1.and("isSystem", join1.entity().isSystemOnly(), Op.EQ);
+        join1.and("isRedundant", join1.entity().getRedundantRouter(), Op.EQ);
+        AllFieldsSearch.join("offerings", join1, AllFieldsSearch.entity().getNetworkOfferingId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
+        AllFieldsSearch.done();
+
+        AccountSearch = createSearchBuilder();
+        AccountSearch.and("offering", AccountSearch.entity().getNetworkOfferingId(), Op.EQ);
+        SearchBuilder<NetworkAccountVO> join = _accountsDao.createSearchBuilder();
+        join.and("account", join.entity().getAccountId(), Op.EQ);
+        AccountSearch.join("accounts", join, AccountSearch.entity().getId(), join.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        AccountSearch.and("datacenter", AccountSearch.entity().getDataCenterId(), Op.EQ);
+        AccountSearch.and("cidr", AccountSearch.entity().getCidr(), Op.EQ);
+        AccountSearch.and("vpcId", AccountSearch.entity().getVpcId(), Op.EQ);
+        AccountSearch.done();
+
+        RelatedConfigSearch = createSearchBuilder();
+        RelatedConfigSearch.and("offering", RelatedConfigSearch.entity().getNetworkOfferingId(), Op.EQ);
+        RelatedConfigSearch.and("datacenter", RelatedConfigSearch.entity().getDataCenterId(), Op.EQ);
+        SearchBuilder<NetworkAccountVO> join2 = _accountsDao.createSearchBuilder();
+        join2.and("account", join2.entity().getAccountId(), Op.EQ);
+        RelatedConfigSearch.join("account", join2, join2.entity().getNetworkId(), RelatedConfigSearch.entity().getId(), JoinType.INNER);
+        RelatedConfigSearch.done();
+
+        AccountNetworkSearch = createSearchBuilder();
+        AccountNetworkSearch.and("networkId", AccountNetworkSearch.entity().getId(), Op.EQ);
+        SearchBuilder<NetworkAccountVO> mapJoin = _accountsDao.createSearchBuilder();
+        mapJoin.and("accountId", mapJoin.entity().getAccountId(), Op.EQ);
+        AccountNetworkSearch.join("networkSearch", mapJoin, AccountNetworkSearch.entity().getId(), mapJoin.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        AccountNetworkSearch.done();
+
+        ZoneBroadcastUriSearch = createSearchBuilder();
+        ZoneBroadcastUriSearch.and("dataCenterId", ZoneBroadcastUriSearch.entity().getDataCenterId(), Op.EQ);
+        ZoneBroadcastUriSearch.and("broadcastUri", ZoneBroadcastUriSearch.entity().getBroadcastUri(), Op.EQ);
+        ZoneBroadcastUriSearch.and("guestType", ZoneBroadcastUriSearch.entity().getGuestType(), Op.EQ);
+        ZoneBroadcastUriSearch.done();
+
+        CountByZoneAndURI = createSearchBuilder(Long.class);
+        CountByZoneAndURI.select(null, Func.COUNT, null);
+        CountByZoneAndURI.and("dataCenterId", CountByZoneAndURI.entity().getDataCenterId(), Op.EQ);
+        CountByZoneAndURI.and("broadcastUri", CountByZoneAndURI.entity().getBroadcastUri(), Op.EQ);
+        CountByZoneAndURI.and("guestType", CountByZoneAndURI.entity().getGuestType(), Op.EQ);
+
+        CountByZoneAndURI.done();
+        
+        ZoneSecurityGroupSearch = createSearchBuilder();
+        ZoneSecurityGroupSearch.and("dataCenterId", ZoneSecurityGroupSearch.entity().getDataCenterId(), Op.EQ);
+        SearchBuilder<NetworkServiceMapVO> offJoin = _ntwkSvcMap.createSearchBuilder();
+        offJoin.and("service", offJoin.entity().getService(), Op.EQ);
+        ZoneSecurityGroupSearch.join("services", offJoin, ZoneSecurityGroupSearch.entity().getId(), offJoin.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        ZoneSecurityGroupSearch.done();
+
+        CountBy = createSearchBuilder(Integer.class);
+        CountBy.select(null, Func.COUNT, CountBy.entity().getId());
+        CountBy.and("offeringId", CountBy.entity().getNetworkOfferingId(), Op.EQ);
+        CountBy.and("vpcId", CountBy.entity().getVpcId(), Op.EQ);
+        CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
+        CountBy.done();
+
+        PhysicalNetworkSearch = createSearchBuilder();
+        PhysicalNetworkSearch.and("physicalNetworkId", PhysicalNetworkSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        PhysicalNetworkSearch.done();
+
+        SecurityGroupSearch = createSearchBuilder();
+        SearchBuilder<NetworkServiceMapVO> join3 = _ntwkSvcMap.createSearchBuilder();
+        join3.and("service", join3.entity().getService(), Op.EQ);
+        SecurityGroupSearch.join("services", join3, SecurityGroupSearch.entity().getId(), join3.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        SecurityGroupSearch.done();
+        
+        NetworksCount = createSearchBuilder(Integer.class);
+        NetworksCount.select(null, Func.COUNT, NetworksCount.entity().getId());
+        NetworksCount.and("networkOfferingId", NetworksCount.entity().getNetworkOfferingId(), SearchCriteria.Op.EQ);
+        NetworksCount.done();
+        
+        NetworksRegularUserCanCreateSearch = createSearchBuilder(Long.class);
+        NetworksRegularUserCanCreateSearch.and("aclType", NetworksRegularUserCanCreateSearch.entity().getAclType(), Op.EQ);
+        NetworksRegularUserCanCreateSearch.select(null, Func.COUNT, NetworksRegularUserCanCreateSearch.entity().getId());
+        SearchBuilder<NetworkAccountVO> join4 = _accountsDao.createSearchBuilder();
+        join4.and("account", join4.entity().getAccountId(), Op.EQ);
+        join4.and("isOwner", join4.entity().isOwner(), Op.EQ);
+        NetworksRegularUserCanCreateSearch.join("accounts", join4, NetworksRegularUserCanCreateSearch.entity().getId(), join4.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        SearchBuilder<NetworkOfferingVO> join5 = _ntwkOffDao.createSearchBuilder();
+        join5.and("specifyVlan", join5.entity().getSpecifyVlan(), Op.EQ);
+        NetworksRegularUserCanCreateSearch.join("ntwkOff", join5, NetworksRegularUserCanCreateSearch.entity().getNetworkOfferingId(), join5.entity().getId(), JoinBuilder.JoinType.INNER);    
+        NetworksRegularUserCanCreateSearch.done();
+
+        _tgMacAddress = _tgs.get("macAddress");
+        
+        SourceNATSearch = createSearchBuilder();
+        SourceNATSearch.and("account", SourceNATSearch.entity().getAccountId(), Op.EQ);
+        SourceNATSearch.and("datacenter", SourceNATSearch.entity().getDataCenterId(), Op.EQ);
+        SourceNATSearch.and("guestType", SourceNATSearch.entity().getGuestType(), Op.EQ);
+        SearchBuilder<NetworkServiceMapVO> join6 = _ntwkSvcMap.createSearchBuilder();
+        join6.and("service", join6.entity().getService(), Op.EQ);
+        SourceNATSearch.join("services", join6, SourceNATSearch.entity().getId(), join6.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        SourceNATSearch.done();
+        
+        VpcNetworksCount = createSearchBuilder(Long.class);
+        VpcNetworksCount.and("vpcId", VpcNetworksCount.entity().getVpcId(), Op.EQ);
+        VpcNetworksCount.select(null, Func.COUNT, VpcNetworksCount.entity().getId());
+        VpcNetworksCount.done();
+
+        OfferingAccountNetworkSearch = createSearchBuilder();
+        OfferingAccountNetworkSearch.select(null, Func.DISTINCT, OfferingAccountNetworkSearch.entity().getId());
+        SearchBuilder<NetworkOfferingVO> ntwkOfferingJoin = _ntwkOffDao.createSearchBuilder();
+        ntwkOfferingJoin.and("isSystem", ntwkOfferingJoin.entity().isSystemOnly(), Op.EQ);
+        OfferingAccountNetworkSearch.join("ntwkOfferingSearch", ntwkOfferingJoin, OfferingAccountNetworkSearch.entity().getNetworkOfferingId(), ntwkOfferingJoin.entity().getId(), JoinBuilder.JoinType.LEFT);
+        SearchBuilder<NetworkAccountVO> ntwkAccountJoin = _accountsDao.createSearchBuilder();
+        ntwkAccountJoin.and("accountId", ntwkAccountJoin.entity().getAccountId(), Op.EQ);
+        OfferingAccountNetworkSearch.join("ntwkAccountSearch", ntwkAccountJoin, OfferingAccountNetworkSearch.entity().getId(), ntwkAccountJoin.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
+        OfferingAccountNetworkSearch.and("zoneId", OfferingAccountNetworkSearch.entity().getDataCenterId(), Op.EQ);
+        OfferingAccountNetworkSearch.and("type", OfferingAccountNetworkSearch.entity().getGuestType(), Op.EQ);
+        OfferingAccountNetworkSearch.done();
+
+        GarbageCollectedSearch = createSearchBuilder(Long.class);
+        GarbageCollectedSearch.selectField(GarbageCollectedSearch.entity().getId());
+        SearchBuilder<NetworkOpVO> join7 = _ntwkOpDao.createSearchBuilder();
+        join7.and("activenics", join7.entity().getActiveNicsCount(), Op.EQ);
+        join7.and("gc", join7.entity().isGarbageCollected(), Op.EQ);
+        join7.and("check", join7.entity().isCheckForGc(), Op.EQ);
+        GarbageCollectedSearch.join("ntwkOpGC", join7, GarbageCollectedSearch.entity().getId(), join7.entity().getId(), JoinBuilder.JoinType.INNER);
+        SearchBuilder<NetworkOfferingVO> join8 = _ntwkOffDao.createSearchBuilder();
+        join8.and("isPersistent", join8.entity().getIsPersistent(), Op.EQ);
+        GarbageCollectedSearch.join("ntwkOffGC", join8, GarbageCollectedSearch.entity().getNetworkOfferingId(), join8.entity().getId(), JoinBuilder.JoinType.INNER);
+        GarbageCollectedSearch.done();
+
+    }
+
+    @Override
+    public List<NetworkVO> listByZoneAndGuestType(long accountId, long dataCenterId, Network.GuestType type, Boolean isSystem) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("datacenter", dataCenterId);
+        sc.setParameters("account", accountId);
+        if (type != null) {
+            sc.setParameters("guestType", type);
+        }
+        
+        if (isSystem != null) {
+            sc.setJoinParameters("offerings", "isSystem", isSystem);
+        }
+        
+        return listBy(sc, null);
+    }
+
+    public List<NetworkVO> findBy(TrafficType trafficType, Mode mode, BroadcastDomainType broadcastType, long networkOfferingId, long dataCenterId) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("trafficType", trafficType);
+        sc.setParameters("broadcastType", broadcastType);
+        sc.setParameters("offering", networkOfferingId);
+        sc.setParameters("datacenter", dataCenterId);
+
+        return search(sc, null);
+    }
+
+    @Override
+    public List<NetworkVO> listBy(long accountId, long offeringId, long dataCenterId) {
+        SearchCriteria<NetworkVO> sc = AccountSearch.create();
+        sc.setParameters("offering", offeringId);
+        sc.setJoinParameters("accounts", "account", accountId);
+        sc.setParameters("datacenter", dataCenterId);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NetworkVO> listBy(long accountId, long dataCenterId, String cidr, boolean skipVpc) {
+        SearchCriteria<NetworkVO> sc = AccountSearch.create();
+        sc.setJoinParameters("accounts", "account", accountId);
+        sc.setParameters("datacenter", dataCenterId);
+        sc.setParameters("cidr", cidr);
+        if (skipVpc) {
+            sc.setParameters("vpcId", (Object)null);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override
+    @DB
+    public NetworkVO persist(NetworkVO network, boolean gc, Map<String, String> serviceProviderMap) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+
+        // 1) create network
+        NetworkVO newNetwork = super.persist(network);
+        // 2) add account to the network
+        addAccountToNetwork(network.getId(), network.getAccountId(), true);
+        // 3) add network to gc monitor table
+        NetworkOpVO op = new NetworkOpVO(network.getId(), gc);
+        _opDao.persist(op);
+        // 4) add services/providers for the network
+        persistNetworkServiceProviders(newNetwork.getId(), serviceProviderMap);
+
+        txn.commit();
+        return newNetwork;
+    }
+
+    @Override
+    @DB
+    public boolean update(Long networkId, NetworkVO network, Map<String, String> serviceProviderMap) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+
+        super.update(networkId, network);
+        if (serviceProviderMap != null) {
+            _ntwkSvcMap.deleteByNetworkId(networkId);
+            persistNetworkServiceProviders(networkId, serviceProviderMap);
+        }
+
+        txn.commit();
+        return true;
+    }
+
+    @Override
+    @DB
+    public void persistNetworkServiceProviders(long networkId, Map<String, String> serviceProviderMap) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        for (String service : serviceProviderMap.keySet()) {
+            NetworkServiceMapVO serviceMap = new NetworkServiceMapVO(networkId, Service.getService(service), Provider.getProvider(serviceProviderMap.get(service)));
+            _ntwkSvcMap.persist(serviceMap);
+        }
+        txn.commit();
+    }
+
+    protected void addAccountToNetwork(long networkId, long accountId, boolean isOwner) {
+        NetworkAccountVO account = new NetworkAccountVO(networkId, accountId, isOwner);
+        _accountsDao.persist(account);
+    }
+
+    @Override
+    public SearchBuilder<NetworkAccountVO> createSearchBuilderForAccount() {
+        return _accountsDao.createSearchBuilder();
+    }
+
+    @Override
+    public List<NetworkVO> getNetworksForOffering(long offeringId, long dataCenterId, long accountId) {
+        SearchCriteria<NetworkVO> sc = RelatedConfigSearch.create();
+        sc.setParameters("offering", offeringId);
+        sc.setParameters("dc", dataCenterId);
+        sc.setJoinParameters("account", "account", accountId);
+        return search(sc, null);
+    }
+
+    @Override
+    public String getNextAvailableMacAddress(long networkConfigId) {
+        SequenceFetcher fetch = SequenceFetcher.getInstance();
+
+        long seq = fetch.getNextSequence(Long.class, _tgMacAddress, networkConfigId);
+        seq = seq | _prefix << 40 | ((_rand.nextInt(Short.MAX_VALUE) << 16) & 0x00000000ffff0000l);
+        return NetUtils.long2Mac(seq);
+    }
+
+    @Override
+    public List<NetworkVO> listBy(long accountId, long networkId) {
+        SearchCriteria<NetworkVO> sc = AccountNetworkSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setJoinParameters("networkSearch", "accountId", accountId);
+        return listBy(sc);
+    }
+
+    @Override
+    public long countByZoneAndUri(long zoneId, String broadcastUri) {
+
+        SearchCriteria<Long> sc = CountByZoneAndURI.create();
+        sc.setParameters("dataCenterId", zoneId);
+        sc.setParameters("broadcastUri", broadcastUri);
+        
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public List<NetworkVO> listByZone(long zoneId) {
+        SearchCriteria<NetworkVO> sc = ZoneBroadcastUriSearch.create();
+        sc.setParameters("dataCenterId", zoneId);
+        return search(sc, null);
+    }
+
+    @Override
+    public long countByZoneUriAndGuestType(long zoneId, String broadcastUri, GuestType guestType) {
+        SearchCriteria<Long> sc = CountByZoneAndURI.create();
+        sc.setParameters("dataCenterId", zoneId);
+        sc.setParameters("broadcastUri", broadcastUri);
+        sc.setParameters("guestType", guestType);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public List<NetworkVO> listByZoneSecurityGroup(Long zoneId) {
+        SearchCriteria<NetworkVO> sc = ZoneSecurityGroupSearch.create();
+        if (zoneId != null) {
+            sc.setParameters("dataCenterId", zoneId);
+        }
+        sc.setJoinParameters("services", "service", Service.SecurityGroup.getName());
+        return search(sc, null);
+    }
+
+    @Override
+    public void changeActiveNicsBy(long networkId, int count) {
+        _opDao.changeActiveNicsBy(networkId, count);
+    }
+
+    @Override
+    public int getActiveNicsIn(long networkId) {
+        return _opDao.getActiveNics(networkId);
+    }
+
+    @Override
+    public List<Long> findNetworksToGarbageCollect() {
+        SearchCriteria<Long> sc = GarbageCollectedSearch.create();
+        sc.setJoinParameters("ntwkOffGC", "isPersistent", false);
+        sc.setJoinParameters("ntwkOpGC", "activenics", 0);
+        sc.setJoinParameters("ntwkOpGC", "gc", true);
+        sc.setJoinParameters("ntwkOpGC", "check", true);
+        return customSearch(sc, null);
+    }
+
+    @Override
+    public void clearCheckForGc(long networkId) {
+        _opDao.clearCheckForGc(networkId);
+    }
+    
+    @Override
+    public void setCheckForGc(long networkId) {
+        _opDao.setCheckForGc(networkId);
+    }
+
+    @Override
+    public List<NetworkVO> listByOwner(long ownerId) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("account", ownerId);
+        return listBy(sc);
+    }
+
+    @Override
+    public void addDomainToNetwork(long networkId, long domainId, Boolean subdomainAccess) {
+        addDomainToNetworknetwork(networkId, domainId, subdomainAccess);
+    }
+
+    protected void addDomainToNetworknetwork(long networkId, long domainId, Boolean subdomainAccess) {
+        NetworkDomainVO domain = new NetworkDomainVO(networkId, domainId, subdomainAccess);
+        _domainsDao.persist(domain);
+    }
+    
+    
+    @Override
+    public int getNetworkCountByVpcId(long vpcId) {
+        SearchCriteria<Integer> sc = CountBy.create();
+        sc.setParameters("vpcId", vpcId);
+        List<Integer> results = customSearch(sc, null);
+        return results.get(0);
+    }
+    
+
+    @Override
+    public List<NetworkVO> listSecurityGroupEnabledNetworks() {
+        SearchCriteria<NetworkVO> sc = SecurityGroupSearch.create();
+        sc.setJoinParameters("services", "service", Service.SecurityGroup.getName());
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NetworkVO> listByPhysicalNetwork(long physicalNetworkId) {
+        SearchCriteria<NetworkVO> sc = PhysicalNetworkSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NetworkVO> listByPhysicalNetworkTrafficType(long physicalNetworkId, TrafficType trafficType) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("trafficType", trafficType);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NetworkVO> listByPhysicalNetworkAndProvider(long physicalNetworkId, String providerName) {
+        SearchBuilder<NetworkServiceMapVO> svcProviderMapSearch = _ntwkSvcMap.createSearchBuilder();
+        NetworkServiceMapVO svcProviderEntry = svcProviderMapSearch.entity();
+        svcProviderMapSearch.and("Provider", svcProviderMapSearch.entity().getProvider(), SearchCriteria.Op.EQ);
+
+        SearchBuilder<NetworkVO> networksSearch = createSearchBuilder();
+        networksSearch.and("physicalNetworkId", networksSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        networksSearch.join("svcProviderMapSearch", svcProviderMapSearch, networksSearch.entity().getId(), svcProviderEntry.getNetworkId(), JoinBuilder.JoinType.INNER);
+
+        SearchCriteria<NetworkVO> sc = networksSearch.create();
+        sc.setJoinParameters("svcProviderMapSearch", "Provider", providerName);
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<NetworkVO> listBy(long accountId, long dataCenterId, Network.GuestType type, TrafficType trafficType) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("datacenter", dataCenterId);
+        sc.setParameters("account", accountId);
+        sc.setParameters("guestType", type);
+        sc.setParameters("trafficType", trafficType);
+
+        return listBy(sc, null);
+    }
+
+    @Override
+    public List<NetworkVO> listByZoneAndTrafficType(long zoneId, TrafficType trafficType) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("datacenter", zoneId);
+        sc.setParameters("trafficType", trafficType);
+
+        return listBy(sc, null);
+    }
+    
+    @Override
+    public int getNetworkCountByNetworkOffId(long networkOfferingId) {
+        SearchCriteria<Integer> sc = NetworksCount.create();
+        sc.setParameters("networkOfferingId", networkOfferingId);
+        List<Integer> count = customSearch(sc, null);
+        return count.get(0);
+    }
+    
+    @Override
+    public long countNetworksUserCanCreate(long ownerId) {
+        SearchCriteria<Long> sc = NetworksRegularUserCanCreateSearch.create();
+        sc.setParameters("aclType", ACLType.Account);
+        sc.setJoinParameters("accounts", "account", ownerId);
+        sc.setJoinParameters("ntwkOff", "specifyVlan", false);
+        return customSearch(sc, null).get(0);
+    }
+    
+    
+    @Override
+    public List<NetworkVO> listSourceNATEnabledNetworks(long accountId, long dataCenterId, Network.GuestType type) {
+        SearchCriteria<NetworkVO> sc = SourceNATSearch.create();
+        sc.setParameters("datacenter", dataCenterId);
+        sc.setParameters("account", accountId);
+        sc.setParameters("guestType", type);
+        sc.setJoinParameters("services", "service", Service.SourceNat.getName());
+        return listBy(sc);
+    }
+    
+    @Override
+    public List<NetworkVO> listByVpc(long vpcId) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("vpcId", vpcId);
+
+        return listBy(sc, null);
+    }
+
+
+    @Override
+    public NetworkVO getPrivateNetwork(String broadcastUri, String cidr, long accountId, long zoneId) {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setParameters("datacenter", zoneId);
+        sc.setParameters("broadcastUri", broadcastUri);
+        sc.setParameters("cidr", cidr);
+        sc.setParameters("account", accountId);
+        sc.setParameters("offering", _ntwkOffDao.findByUniqueName(NetworkOffering.SystemPrivateGatewayNetworkOffering).getId());
+        return findOneBy(sc);
+    }
+
+    @Override
+    @DB
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        NetworkVO entry = findById(id);
+        if (entry != null) {
+            _tagsDao.removeByIdAndType(id, TaggedResourceType.Network);
+        }
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+
+    @Override
+    public long countVpcNetworks(long vpcId) {
+        SearchCriteria<Long> sc = VpcNetworksCount.create();
+        sc.setParameters("vpcId", vpcId);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public boolean updateState(State currentState, Event event, State nextState, Network vo, Object data) {
+       // TODO: ensure this update is correct
+       Transaction txn = Transaction.currentTxn();
+       txn.start();
+
+       NetworkVO networkVo = (NetworkVO) vo;
+       networkVo.setState(nextState);
+       super.update(networkVo.getId(), networkVo);
+
+       txn.commit();
+       return true;
+    }
+
+    @Override
+    public List<NetworkVO> listNetworksByAccount(long accountId, long zoneId, Network.GuestType type, boolean isSystem) {
+        SearchCriteria<NetworkVO> sc = OfferingAccountNetworkSearch.create();
+        sc.setJoinParameters("ntwkOfferingSearch", "isSystem", isSystem);
+        sc.setJoinParameters("ntwkAccountSearch", "accountId", accountId);
+        sc.setParameters("zoneId", zoneId);
+        sc.setParameters("type", type);
+
+        List<NetworkVO> networks = search(sc, null);
+        return networks;
+    }
+
+    @Override
+    public List<NetworkVO> listRedundantNetworks() {
+        SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
+        sc.setJoinParameters("offerings", "isRedundant", true);
+        return listBy(sc, null);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkDomainDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDomainDao.java b/engine/schema/src/com/cloud/network/dao/NetworkDomainDao.java
new file mode 100644
index 0000000..6689892
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkDomainDao.java
@@ -0,0 +1,27 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface NetworkDomainDao extends GenericDao<NetworkDomainVO, Long>{
+    List<NetworkDomainVO> listDomainNetworkMapByDomain(Object... domainId);
+    NetworkDomainVO getDomainNetworkMapByNetworkId(long networkId);
+    List<Long> listNetworkIdsByDomain(long domainId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkDomainDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDomainDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkDomainDaoImpl.java
new file mode 100644
index 0000000..bbb9203
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkDomainDaoImpl.java
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value=NetworkDomainDao.class) @DB(txn=false)
+public class NetworkDomainDaoImpl extends GenericDaoBase<NetworkDomainVO, Long> implements NetworkDomainDao {
+    final SearchBuilder<NetworkDomainVO> AllFieldsSearch;
+    final SearchBuilder<NetworkDomainVO> DomainsSearch;
+    
+    protected NetworkDomainDaoImpl() {
+        super();
+        
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("domainId", AllFieldsSearch.entity().getDomainId(), Op.EQ);
+        AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
+        AllFieldsSearch.done();
+        
+        DomainsSearch = createSearchBuilder();
+        DomainsSearch.and("domainId", DomainsSearch.entity().getDomainId(), Op.IN);
+        DomainsSearch.done();
+    }
+    
+    @Override
+    public List<NetworkDomainVO> listDomainNetworkMapByDomain(Object... domainId) {
+        SearchCriteria<NetworkDomainVO> sc = DomainsSearch.create();
+        sc.setParameters("domainId", (Object[])domainId);
+        
+        return listBy(sc);
+    }
+    
+    @Override
+    public NetworkDomainVO getDomainNetworkMapByNetworkId(long networkId) {
+        SearchCriteria<NetworkDomainVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        return findOneBy(sc);
+    }
+    
+    @Override
+    public List<Long> listNetworkIdsByDomain(long domainId) {
+        List<Long> networkIdsToReturn = new ArrayList<Long>();
+        List<NetworkDomainVO> maps = listDomainNetworkMapByDomain(domainId);
+        for (NetworkDomainVO map : maps) {
+            networkIdsToReturn.add(map.getNetworkId());
+        } 
+        return networkIdsToReturn;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkDomainVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDomainVO.java b/engine/schema/src/com/cloud/network/dao/NetworkDomainVO.java
new file mode 100644
index 0000000..bb4e904
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkDomainVO.java
@@ -0,0 +1,72 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.domain.PartOf;
+import org.apache.cloudstack.api.InternalIdentity;
+
+@Entity
+@Table(name="domain_network_ref")
+public class NetworkDomainVO implements PartOf, InternalIdentity {
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    long id;
+    
+    @Column(name="domain_id")
+    long domainId;
+    
+    @Column(name="network_id")
+    long networkId;
+    
+    @Column(name="subdomain_access")
+    public
+    Boolean subdomainAccess;
+
+    protected NetworkDomainVO() {
+    }
+    
+    public NetworkDomainVO(long networkId, long domainId, Boolean subdomainAccess) {
+        this.networkId = networkId;
+        this.domainId = domainId;
+        this.subdomainAccess = subdomainAccess;
+    }
+
+    @Override
+    public long getId() {
+        return id;
+    }
+
+    @Override
+    public long getDomainId() {
+        return domainId;
+    }
+    
+    public long getNetworkId() {
+        return networkId;
+    }
+
+	public Boolean isSubdomainAccess() {
+		return subdomainAccess;
+	}
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDao.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDao.java
new file mode 100644
index 0000000..9e255dc
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDao.java
@@ -0,0 +1,38 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface NetworkExternalFirewallDao extends GenericDao<NetworkExternalFirewallVO, Long> {
+
+    /**
+     * find the network to firewall device mapping corresponding to a network
+     * @param lbDeviceId guest network Id
+     * @return return NetworkExternalFirewallDao for the guest network
+     */    
+    NetworkExternalFirewallVO findByNetworkId(long networkId);
+
+    /**
+     * list all network to firewall device mappings corresponding to a firewall device Id
+     * @param lbDeviceId firewall device Id
+     * @return list of NetworkExternalFirewallVO mappings corresponding to the networks mapped to the firewall device 
+     */    
+    List<NetworkExternalFirewallVO> listByFirewallDeviceId(long lbDeviceId);	
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDaoImpl.java
new file mode 100644
index 0000000..b176760
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallDaoImpl.java
@@ -0,0 +1,61 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value=NetworkExternalFirewallDao.class) @DB(txn=false)
+public class NetworkExternalFirewallDaoImpl extends GenericDaoBase<NetworkExternalFirewallVO, Long> implements NetworkExternalFirewallDao {
+
+    final SearchBuilder<NetworkExternalFirewallVO> networkIdSearch;
+    final SearchBuilder<NetworkExternalFirewallVO> deviceIdSearch;
+    protected NetworkExternalFirewallDaoImpl() {
+        super();
+        networkIdSearch = createSearchBuilder();
+        networkIdSearch.and("networkId", networkIdSearch.entity().getNetworkId(), Op.EQ);
+        networkIdSearch.done();
+        deviceIdSearch = createSearchBuilder();
+        deviceIdSearch.and("externalFWDeviceId", deviceIdSearch.entity().getExternalFirewallDeviceId(), Op.EQ);
+        deviceIdSearch.done();
+    }
+
+    @Override
+    public NetworkExternalFirewallVO findByNetworkId(long networkId) {
+        SearchCriteria<NetworkExternalFirewallVO> sc = networkIdSearch.create();
+        sc.setParameters("networkId", networkId);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<NetworkExternalFirewallVO> listByFirewallDeviceId(long fwDeviceId) {
+        SearchCriteria<NetworkExternalFirewallVO> sc = deviceIdSearch.create();
+        sc.setParameters("externalFWDeviceId", fwDeviceId);
+        return search(sc, null);
+    }    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallVO.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallVO.java
new file mode 100644
index 0000000..3c33dd5
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalFirewallVO.java
@@ -0,0 +1,88 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.Date;
+import java.util.UUID;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.utils.db.GenericDao;
+import org.apache.cloudstack.api.InternalIdentity;
+
+/**
+ * NetworkExternalFirewallVO contains information on the networks that are using external firewall
+  */
+
+@Entity
+@Table(name="network_external_firewall_device_map")
+public class NetworkExternalFirewallVO implements InternalIdentity {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private long id;
+
+    @Column(name="uuid")
+    private String uuid;
+
+    @Column(name = "network_id")
+    private long networkId;
+
+    @Column(name = "external_firewall_device_id")
+    private long externalFirewallDeviceId;
+
+    @Column(name=GenericDao.CREATED_COLUMN)
+    Date created;
+
+    @Column(name=GenericDao.REMOVED_COLUMN)
+    Date removed;
+
+    public NetworkExternalFirewallVO(long networkId, long externalFirewallDeviceId) {
+        this.networkId = networkId;
+        this.externalFirewallDeviceId = externalFirewallDeviceId;
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public NetworkExternalFirewallVO() {
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getNetworkId() {
+        return networkId;
+    }
+
+    public long getExternalFirewallDeviceId() {
+        return externalFirewallDeviceId;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDao.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDao.java
new file mode 100644
index 0000000..527d376
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDao.java
@@ -0,0 +1,38 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface  NetworkExternalLoadBalancerDao extends GenericDao<NetworkExternalLoadBalancerVO, Long> {
+
+    /**
+     * find the network to load balancer device mapping corresponding to a network
+     * @param networkId guest network Id
+     * @return return NetworkExternalLoadBalancerVO for the guest network
+     */
+    NetworkExternalLoadBalancerVO findByNetworkId(long networkId);
+
+    /**
+     * list all network to load balancer device mappings corresponding to a load balancer device Id
+     * @param lbDeviceId load balancer device Id
+     * @return list of NetworkExternalLoadBalancerVO mappings corresponding to the networks mapped to the load balancer device 
+     */
+    List<NetworkExternalLoadBalancerVO> listByLoadBalancerDeviceId(long lbDeviceId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDaoImpl.java
new file mode 100644
index 0000000..c29c164
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerDaoImpl.java
@@ -0,0 +1,62 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value=NetworkExternalLoadBalancerDao.class) @DB(txn=false)
+public class NetworkExternalLoadBalancerDaoImpl extends GenericDaoBase<NetworkExternalLoadBalancerVO, Long> implements NetworkExternalLoadBalancerDao {
+
+    final SearchBuilder<NetworkExternalLoadBalancerVO> networkIdSearch;
+    final SearchBuilder<NetworkExternalLoadBalancerVO> deviceIdSearch;
+    
+    protected NetworkExternalLoadBalancerDaoImpl() {
+        super();
+        networkIdSearch = createSearchBuilder();
+        networkIdSearch.and("networkId", networkIdSearch.entity().getNetworkId(), Op.EQ);
+        networkIdSearch.done();
+
+        deviceIdSearch = createSearchBuilder();
+        deviceIdSearch.and("externalLBDeviceId", deviceIdSearch.entity().getExternalLBDeviceId(), Op.EQ);
+        deviceIdSearch.done();
+    }
+
+    @Override
+    public NetworkExternalLoadBalancerVO findByNetworkId(long networkId) {
+        SearchCriteria<NetworkExternalLoadBalancerVO> sc = networkIdSearch.create();
+        sc.setParameters("networkId", networkId);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<NetworkExternalLoadBalancerVO> listByLoadBalancerDeviceId(long lbDeviceId) {
+        SearchCriteria<NetworkExternalLoadBalancerVO> sc = deviceIdSearch.create();
+        sc.setParameters("externalLBDeviceId", lbDeviceId);
+        return search(sc, null);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerVO.java b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerVO.java
new file mode 100644
index 0000000..820759c
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkExternalLoadBalancerVO.java
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.Date;
+import java.util.UUID;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.utils.db.GenericDao;
+import org.apache.cloudstack.api.InternalIdentity;
+
+/**
+ * NetworkExternalLoadBalancerVO contains mapping of a network and the external load balancer device id assigned to the network
+  */
+
+@Entity
+@Table(name="network_external_lb_device_map")
+public class NetworkExternalLoadBalancerVO implements InternalIdentity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private long id;
+
+    @Column(name="uuid")
+    private String uuid;
+
+    @Column(name = "network_id")
+    private long networkId;
+
+    @Column(name = "external_load_balancer_device_id")
+    private long externalLBDeviceId;
+
+    @Column(name=GenericDao.CREATED_COLUMN)
+    Date created;
+
+    @Column(name=GenericDao.REMOVED_COLUMN)
+    Date removed;
+
+    public NetworkExternalLoadBalancerVO(long networkId, long externalLBDeviceID) {
+        this.networkId = networkId;
+        this.externalLBDeviceId = externalLBDeviceID;
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public NetworkExternalLoadBalancerVO(){
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getNetworkId() {
+        return networkId;
+    }
+
+    public long getExternalLBDeviceId() {
+        return externalLBDeviceId;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkOpDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkOpDao.java b/engine/schema/src/com/cloud/network/dao/NetworkOpDao.java
new file mode 100644
index 0000000..f492dbd
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkOpDao.java
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface NetworkOpDao extends GenericDao<NetworkOpVO, Long> {
+    public int getActiveNics(long networkId);
+    public void changeActiveNicsBy(long networkId, int count);
+    public void setCheckForGc(long networkId);
+    public void clearCheckForGc(long networkId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java
new file mode 100644
index 0000000..a3f54b7
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java
@@ -0,0 +1,84 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.Attribute;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.UpdateBuilder;
+
+@Component
+public class NetworkOpDaoImpl extends GenericDaoBase<NetworkOpVO, Long> implements NetworkOpDao {
+    protected final SearchBuilder<NetworkOpVO> AllFieldsSearch;
+    protected final GenericSearchBuilder<NetworkOpVO, Integer> ActiveNicsSearch;
+    protected final Attribute _activeNicsAttribute;
+    
+    protected NetworkOpDaoImpl() {
+        super();
+        
+        ActiveNicsSearch = createSearchBuilder(Integer.class);
+        ActiveNicsSearch.selectField(ActiveNicsSearch.entity().getActiveNicsCount());
+        ActiveNicsSearch.and("network", ActiveNicsSearch.entity().getId(), Op.EQ);
+        ActiveNicsSearch.done();
+        
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("network", AllFieldsSearch.entity().getId(), Op.EQ);
+        AllFieldsSearch.done();
+
+        _activeNicsAttribute = _allAttributes.get("activeNicsCount");
+        assert _activeNicsAttribute != null : "Cannot find activeNicsCount";
+    }
+
+    public int getActiveNics(long networkId) {
+        SearchCriteria<Integer> sc = ActiveNicsSearch.create();
+        sc.setParameters("network", networkId);
+        
+        return customSearch(sc, null).get(0);
+    }
+    
+    public void changeActiveNicsBy(long networkId, int count) {
+        
+        SearchCriteria<NetworkOpVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+
+        NetworkOpVO vo = createForUpdate();
+        UpdateBuilder builder = getUpdateBuilder(vo);
+        builder.incr(_activeNicsAttribute, count);
+        
+        update(builder, sc, null);
+    }
+    
+    public void setCheckForGc(long networkId) {
+        NetworkOpVO vo = createForUpdate();
+        vo.setCheckForGc(true);
+        update(networkId, vo);
+    }
+    
+    public void clearCheckForGc(long networkId) {
+        NetworkOpVO vo = createForUpdate();
+        vo.setCheckForGc(false);
+        update(networkId, vo);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkOpVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkOpVO.java b/engine/schema/src/com/cloud/network/dao/NetworkOpVO.java
new file mode 100644
index 0000000..8d41727
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkOpVO.java
@@ -0,0 +1,76 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import org.apache.cloudstack.api.InternalIdentity;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="op_networks")
+public class NetworkOpVO implements InternalIdentity {
+    
+    @Id
+    @Column(name="id")
+    long id;
+    
+    @Column(name="nics_count")
+    int activeNicsCount;
+    
+    @Column(name="gc")
+    boolean garbageCollected;
+    
+    @Column(name="check_for_gc")
+    boolean checkForGc;
+
+    protected NetworkOpVO() {
+    }
+    
+    public NetworkOpVO(long id, boolean gc) {
+        this.id = id;
+        this.garbageCollected = gc;
+        this.checkForGc = gc;
+        this.activeNicsCount = 0;
+    }
+    
+    public long getId() {
+        return id;
+    }
+    
+    public long getActiveNicsCount() {
+        return activeNicsCount;
+    }
+    
+    public void setActiveNicsCount(int number) {
+        activeNicsCount += number;
+    }
+    
+    public boolean isGarbageCollected() {
+        return garbageCollected;
+    }
+    
+    public boolean isCheckForGc() {
+        return checkForGc;
+    }
+    
+    public void setCheckForGc(boolean check) {
+        checkForGc = check;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDao.java b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDao.java
new file mode 100644
index 0000000..80e070a
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDao.java
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.utils.db.GenericDao;
+
+public interface NetworkRuleConfigDao extends GenericDao<NetworkRuleConfigVO, Long> {
+    List<NetworkRuleConfigVO> listBySecurityGroupId(long securityGroupId);
+    void deleteBySecurityGroup(long securityGroupId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDaoImpl.java
new file mode 100644
index 0000000..a5af4fe
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigDaoImpl.java
@@ -0,0 +1,51 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+@Component
+@Local(value={NetworkRuleConfigDao.class})
+public class NetworkRuleConfigDaoImpl extends GenericDaoBase<NetworkRuleConfigVO, Long> implements NetworkRuleConfigDao {
+    protected SearchBuilder<NetworkRuleConfigVO> SecurityGroupIdSearch;
+
+    protected NetworkRuleConfigDaoImpl() {
+        SecurityGroupIdSearch  = createSearchBuilder();
+        SecurityGroupIdSearch.and("securityGroupId", SecurityGroupIdSearch.entity().getSecurityGroupId(), SearchCriteria.Op.EQ);
+        SecurityGroupIdSearch.done();
+    }
+
+    public List<NetworkRuleConfigVO> listBySecurityGroupId(long securityGroupId) {
+        SearchCriteria<NetworkRuleConfigVO> sc = SecurityGroupIdSearch.create();
+        sc.setParameters("securityGroupId", securityGroupId);
+        return listBy(sc);
+    }
+
+    public void deleteBySecurityGroup(long securityGroupId) {
+        SearchCriteria<NetworkRuleConfigVO> sc = SecurityGroupIdSearch.create();
+        sc.setParameters("securityGroupId", securityGroupId);
+        expunge(sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigVO.java b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigVO.java
new file mode 100644
index 0000000..542c0bb
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkRuleConfigVO.java
@@ -0,0 +1,93 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.async.AsyncInstanceCreateStatus;
+import com.google.gson.annotations.Expose;
+import org.apache.cloudstack.api.InternalIdentity;
+
+@Entity
+@Table(name=("network_rule_config"))
+public class NetworkRuleConfigVO implements InternalIdentity {
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    private Long id;
+
+    @Column(name="security_group_id")
+    private long securityGroupId;
+
+    @Column(name="public_port")
+    private String publicPort;
+
+    @Column(name="private_port")
+    private String privatePort;
+
+    @Column(name="protocol")
+    private String protocol;
+    
+    @Expose
+    @Column(name="create_status", updatable = true, nullable=false)
+    @Enumerated(value=EnumType.STRING)
+    private AsyncInstanceCreateStatus createStatus;
+
+    public NetworkRuleConfigVO() {}
+
+    public NetworkRuleConfigVO(long securityGroupId, String publicPort, String privatePort, String protocol) {
+        this.securityGroupId = securityGroupId;
+        this.publicPort = publicPort;
+        this.privatePort = privatePort;
+        this.protocol = protocol;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getSecurityGroupId() {
+        return securityGroupId;
+    }
+
+    public String getPublicPort() {
+        return publicPort;
+    }
+
+    public String getPrivatePort() {
+        return privatePort;
+    }
+
+    public String getProtocol() {
+        return protocol;
+    }
+    
+    public AsyncInstanceCreateStatus getCreateStatus() {
+    	return createStatus;
+    }
+    
+    public void setCreateStatus(AsyncInstanceCreateStatus createStatus) {
+    	this.createStatus = createStatus;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDao.java b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDao.java
new file mode 100644
index 0000000..6d401c4
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDao.java
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.utils.db.GenericDao;
+
+/**
+ * NetworkServiceDao deals with searches and operations done on the
+ * ntwk_service_map table.
+ *
+ */
+public interface NetworkServiceMapDao extends GenericDao<NetworkServiceMapVO, Long>{
+    boolean areServicesSupportedInNetwork(long networkId, Service... services);
+    boolean canProviderSupportServiceInNetwork(long networkId, Service service, Provider provider);
+    List<NetworkServiceMapVO> getServicesInNetwork(long networkId);
+    String getProviderForServiceInNetwork(long networkid, Service service);
+	void deleteByNetworkId(long networkId);
+	List<String> getDistinctProviders(long networkId);
+	String isProviderForNetwork(long networkId, Provider provider);
+	List<String> getProvidersForServiceInNetwork(long networkId, Service service);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java
new file mode 100644
index 0000000..3cdd738
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java
@@ -0,0 +1,176 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.exception.UnsupportedServiceException;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+@Component
+@Local(value=NetworkServiceMapDao.class) @DB(txn=false)
+public class NetworkServiceMapDaoImpl extends GenericDaoBase<NetworkServiceMapVO, Long> implements NetworkServiceMapDao {
+    final SearchBuilder<NetworkServiceMapVO> AllFieldsSearch;
+    final SearchBuilder<NetworkServiceMapVO> MultipleServicesSearch;
+    final GenericSearchBuilder<NetworkServiceMapVO, String> DistinctProvidersSearch;
+    
+    protected NetworkServiceMapDaoImpl() {
+        super();
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("service", AllFieldsSearch.entity().getService(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("provider", AllFieldsSearch.entity().getProvider(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.done();
+        
+        MultipleServicesSearch = createSearchBuilder();
+        MultipleServicesSearch.and("networkId", MultipleServicesSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        MultipleServicesSearch.and("service", MultipleServicesSearch.entity().getService(), SearchCriteria.Op.IN);
+        MultipleServicesSearch.and("provider", MultipleServicesSearch.entity().getProvider(), SearchCriteria.Op.EQ);
+        MultipleServicesSearch.done();
+        
+        DistinctProvidersSearch = createSearchBuilder(String.class);
+        DistinctProvidersSearch.and("networkId", DistinctProvidersSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        DistinctProvidersSearch.and("provider", DistinctProvidersSearch.entity().getProvider(), SearchCriteria.Op.EQ);
+        DistinctProvidersSearch.and("service", DistinctProvidersSearch.entity().getService(), SearchCriteria.Op.EQ);
+        DistinctProvidersSearch.selectField(DistinctProvidersSearch.entity().getProvider());
+        DistinctProvidersSearch.done();
+    }
+    
+    @Override
+    public boolean areServicesSupportedInNetwork(long networkId, Service... services) {
+        SearchCriteria<NetworkServiceMapVO> sc = MultipleServicesSearch.create();
+        sc.setParameters("networkId", networkId);
+        
+        if (services != null) {
+            String[] servicesStr = new String[services.length];
+            
+            int i = 0;
+            for (Service service : services) {
+                servicesStr[i] = service.getName();
+                i++;
+            }
+            
+            sc.setParameters("service", (Object[])servicesStr);
+        }
+        
+        List<NetworkServiceMapVO> networkServices = listBy(sc);
+        
+        if (services != null) {
+            if (networkServices.size() == services.length) {
+                return true;
+            }
+        } else if (!networkServices.isEmpty()) {
+            return true;
+        }
+        
+        return false;
+    }
+    
+    @Override
+    public boolean canProviderSupportServiceInNetwork(long networkId, Service service, Provider provider) {
+        SearchCriteria<NetworkServiceMapVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("service", service.getName());
+        sc.setParameters("provider", provider.getName());
+        if (findOneBy(sc) != null) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+    
+    protected List<String> getServicesForProviderInNetwork(long networkId, Provider provider) {
+        List<String> services = new ArrayList<String>();
+        SearchCriteria<NetworkServiceMapVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("provider", provider.getName());
+        List<NetworkServiceMapVO> map = listBy(sc);
+        for (NetworkServiceMapVO instance : map) {
+            services.add(instance.getService());
+        }
+        
+        return services;
+    }
+    
+    @Override
+    public String getProviderForServiceInNetwork(long networkId, Service service) {
+        SearchCriteria<NetworkServiceMapVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("service", service.getName());
+        NetworkServiceMapVO ntwkSvc = findOneBy(sc);
+        if (ntwkSvc == null) {
+            throw new UnsupportedServiceException("Service " + service.getName() + " is not supported in the network id=" + networkId);
+        }
+        
+        return ntwkSvc.getProvider();
+    }
+ 
+    @Override
+    public List<NetworkServiceMapVO> getServicesInNetwork(long networkId) {
+        SearchCriteria<NetworkServiceMapVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        return listBy(sc);
+    }
+    
+    @Override
+    public void deleteByNetworkId(long networkId) {
+        SearchCriteria<NetworkServiceMapVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+        remove(sc);
+    }
+    
+    @Override
+    public List<String> getDistinctProviders(long networkId) {
+        SearchCriteria<String> sc = DistinctProvidersSearch.create();
+        sc.setParameters("networkId", networkId);
+        List<String> results = customSearch(sc, null);
+        return results;
+    }
+    
+    @Override
+    public String isProviderForNetwork(long networkId, Provider provider) {
+    	SearchCriteria<String> sc = DistinctProvidersSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("provider", provider.getName());
+        List<String> results = customSearch(sc, null);
+        if (results.isEmpty()) {
+        	return null;
+        } else {
+        	return results.get(0);
+        }
+    }
+
+    @Override
+    public List<String> getProvidersForServiceInNetwork(long networkId, Service service) {
+        SearchCriteria<String> sc = DistinctProvidersSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("service", service.getName());
+        return customSearch(sc, null);
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/network/dao/NetworkServiceMapVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/NetworkServiceMapVO.java b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapVO.java
new file mode 100644
index 0000000..7bb480b
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapVO.java
@@ -0,0 +1,92 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 com.cloud.network.dao;
+
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.network.Network;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.utils.db.GenericDao;
+import org.apache.cloudstack.api.InternalIdentity;
+
+@Entity
+@Table(name="ntwk_service_map")
+public class NetworkServiceMapVO implements InternalIdentity {
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    long id;
+    
+    @Column(name="network_id")
+    long networkId;
+    
+    @Column(name="service")
+    String service;
+    
+    @Column(name="provider")
+    String provider;
+    
+    @Column(name=GenericDao.CREATED_COLUMN)
+    Date created;
+
+    public long getId() {
+        return id;
+    }
+
+    public long getNetworkId() {
+        return networkId;
+    }
+
+    public String getService() {
+        return service;
+    }
+
+    public String getProvider() {
+        return provider;
+    }
+
+    public Date getCreated() {
+        return created;
+    }
+    
+    public NetworkServiceMapVO() {
+    }
+    
+    public NetworkServiceMapVO(long networkId, Service service, Provider provider) {
+        this.networkId = networkId;
+        this.service = service.getName();
+        this.provider = provider.getName();
+    }
+    
+    public String toString() {
+        StringBuilder buf = new StringBuilder("[Network Service[");
+        return buf.append(networkId).append("-").append(service).append("-").append(provider).append("]").toString();
+    }
+}
+
+
+
+
+


Mime
View raw message