cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kis...@apache.org
Subject [43/85] [abbrv] [partial] Moved most of the VOs and DAOs from server package into engine-schema as well
Date Tue, 07 May 2013 14:47:38 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/as/dao/CounterDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/as/dao/CounterDao.java b/engine/schema/src/com/cloud/network/as/dao/CounterDao.java
new file mode 100644
index 0000000..47b0d48
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/as/dao/CounterDao.java
@@ -0,0 +1,29 @@
+// 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.as.dao;
+
+import java.util.List;
+
+import com.cloud.network.as.CounterVO;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDao;
+
+public interface CounterDao extends GenericDao<CounterVO, Long> {
+    public List<CounterVO> listCounters(Long id, String name, String source, String keyword, Filter filter);
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/as/dao/CounterDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/as/dao/CounterDaoImpl.java b/engine/schema/src/com/cloud/network/as/dao/CounterDaoImpl.java
new file mode 100644
index 0000000..0abc3a0
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/as/dao/CounterDaoImpl.java
@@ -0,0 +1,70 @@
+// 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.as.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.network.as.CounterVO;
+import com.cloud.utils.db.Filter;
+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 = CounterDao.class)
+public class CounterDaoImpl extends GenericDaoBase<CounterVO, Long> implements CounterDao {
+    final SearchBuilder<CounterVO> AllFieldsSearch;
+
+    protected CounterDaoImpl() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("id", AllFieldsSearch.entity().getId(), Op.EQ);
+        AllFieldsSearch.and("name", AllFieldsSearch.entity().getName(), Op.LIKE);
+        AllFieldsSearch.and("source", AllFieldsSearch.entity().getSource(), Op.EQ);
+        AllFieldsSearch.done();
+    }
+
+    @Override
+    public List<CounterVO> listCounters(Long id, String name, String source, String keyword, Filter filter) {
+        SearchCriteria<CounterVO> sc = AllFieldsSearch.create();
+
+        if (keyword != null) {
+            SearchCriteria<CounterVO> ssc = createSearchCriteria();
+            ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
+            sc.addAnd("name", SearchCriteria.Op.SC, ssc);
+        }
+
+        if (name != null) {
+            sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + name + "%");
+        }
+
+        if (id != null) {
+            sc.addAnd("id", SearchCriteria.Op.EQ, id);
+        }
+
+        if (source != null) {
+            sc.addAnd("source", SearchCriteria.Op.EQ, source);
+        }
+        return listBy(sc, filter);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDao.java b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDao.java
new file mode 100644
index 0000000..bb3ebef
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDao.java
@@ -0,0 +1,58 @@
+// 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.dao.ExternalFirewallDeviceVO.FirewallDeviceAllocationState;
+import com.cloud.network.dao.ExternalFirewallDeviceVO.FirewallDeviceState;
+import com.cloud.utils.db.GenericDao;
+
+public interface ExternalFirewallDeviceDao extends GenericDao<ExternalFirewallDeviceVO, Long> {
+
+    /**
+     * list all the firewall devices added in to this physical network?
+     * @param physicalNetworkId physical Network Id
+     * @return list of ExternalFirewallDeviceVO for the devices added in to this physical network.
+     */
+    List<ExternalFirewallDeviceVO> listByPhysicalNetwork(long physicalNetworkId);
+
+    /**
+     * list the firewall devices added in to this physical network of certain provider type?
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     */
+    List<ExternalFirewallDeviceVO> listByPhysicalNetworkAndProvider(long physicalNetworkId, String provider_name);
+
+    /**
+     * list the firewall devices added in to this physical network by their allocation state
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     * @param allocationState firewall device allocation state
+     * @return list of ExternalFirewallDeviceVO for the devices in the physical network with a device allocation state
+     */
+    List<ExternalFirewallDeviceVO> listByProviderAndDeviceAllocationState(long physicalNetworkId, String provider_name, FirewallDeviceAllocationState allocationState);
+
+    /**
+     * list the load balancer devices added in to this physical network by the device status (enabled/disabled)
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     * @param state firewall device status
+     * @return list of ExternalFirewallDeviceVO for the devices in the physical network with a device state
+     */
+    List<ExternalFirewallDeviceVO> listByProviderAndDeviceStaus(long physicalNetworkId, String provider_name, FirewallDeviceState state);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDaoImpl.java b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDaoImpl.java
new file mode 100644
index 0000000..01f8861
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceDaoImpl.java
@@ -0,0 +1,96 @@
+// 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.network.dao.ExternalFirewallDeviceVO.FirewallDeviceAllocationState;
+import com.cloud.network.dao.ExternalFirewallDeviceVO.FirewallDeviceState;
+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=ExternalFirewallDeviceDao.class) @DB(txn=false)
+public class ExternalFirewallDeviceDaoImpl extends GenericDaoBase<ExternalFirewallDeviceVO, Long> implements ExternalFirewallDeviceDao {
+    final SearchBuilder<ExternalFirewallDeviceVO> physicalNetworkServiceProviderSearch;
+    final SearchBuilder<ExternalFirewallDeviceVO> physicalNetworkIdSearch;
+    final SearchBuilder<ExternalFirewallDeviceVO> allocationStateSearch;
+    final SearchBuilder<ExternalFirewallDeviceVO> deviceStatusSearch;
+
+    protected ExternalFirewallDeviceDaoImpl() {
+        physicalNetworkIdSearch = createSearchBuilder();
+        physicalNetworkIdSearch.and("physicalNetworkId", physicalNetworkIdSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        physicalNetworkIdSearch.done();
+
+        physicalNetworkServiceProviderSearch = createSearchBuilder();
+        physicalNetworkServiceProviderSearch.and("physicalNetworkId", physicalNetworkServiceProviderSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        physicalNetworkServiceProviderSearch.and("networkServiceProviderName", physicalNetworkServiceProviderSearch.entity().getProviderName(), Op.EQ);
+        physicalNetworkServiceProviderSearch.done();
+
+        allocationStateSearch = createSearchBuilder();
+        allocationStateSearch.and("physicalNetworkId", allocationStateSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        allocationStateSearch.and("providerName", allocationStateSearch.entity().getProviderName(), Op.EQ);
+        allocationStateSearch.and("allocationState", allocationStateSearch.entity().getAllocationState(), Op.EQ);
+        allocationStateSearch.done();
+
+        deviceStatusSearch = createSearchBuilder();
+        deviceStatusSearch.and("physicalNetworkId", deviceStatusSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        deviceStatusSearch.and("providerName", deviceStatusSearch.entity().getProviderName(), Op.EQ);
+        deviceStatusSearch.and("deviceState", deviceStatusSearch.entity().getDeviceState(), Op.EQ);
+        deviceStatusSearch.done();
+    }
+
+	@Override
+	public List<ExternalFirewallDeviceVO> listByPhysicalNetwork(long physicalNetworkId) {
+        SearchCriteria<ExternalFirewallDeviceVO> sc = physicalNetworkIdSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return search(sc, null);
+	}
+
+	@Override
+	public List<ExternalFirewallDeviceVO> listByPhysicalNetworkAndProvider(long physicalNetworkId, String providerName) {
+        SearchCriteria<ExternalFirewallDeviceVO> sc = physicalNetworkServiceProviderSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("networkServiceProviderName", providerName);
+        return search(sc, null);
+	}
+
+    @Override
+    public List<ExternalFirewallDeviceVO> listByProviderAndDeviceAllocationState(long physicalNetworkId, String providerName, FirewallDeviceAllocationState allocationState) {
+        SearchCriteria<ExternalFirewallDeviceVO> sc = allocationStateSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", providerName);
+        sc.setParameters("allocationState", allocationState);
+        return search(sc, null);
+    }
+
+    @Override
+    public List<ExternalFirewallDeviceVO> listByProviderAndDeviceStaus(long physicalNetworkId, String providerName, FirewallDeviceState state) {
+        SearchCriteria<ExternalFirewallDeviceVO> sc = deviceStatusSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", providerName);
+        sc.setParameters("deviceState", state);
+        return search(sc, null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceVO.java b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceVO.java
new file mode 100644
index 0000000..31750b4
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalFirewallDeviceVO.java
@@ -0,0 +1,161 @@
+// 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.Identity;
+import org.apache.cloudstack.api.InternalIdentity;
+
+import java.util.UUID;
+
+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;
+
+/**
+ * ExternalFirewallDeviceVO contains information of a external firewall device (Juniper SRX) added into a deployment
+  */
+
+@Entity
+@Table(name="external_firewall_devices")
+public class ExternalFirewallDeviceVO implements InternalIdentity, Identity {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private long id;
+
+    @Column(name="uuid")
+    private String uuid;
+
+    @Column(name = "host_id")
+    private long hostId;
+
+    @Column(name = "physical_network_id")
+    private long physicalNetworkId;
+
+    @Column(name = "provider_name")
+    private String providerName;
+
+    @Column(name = "device_name")
+    private String deviceName;
+
+    @Column(name="device_state")
+    @Enumerated(value=EnumType.STRING)
+    FirewallDeviceState deviceState;
+
+    @Column(name="is_dedicated")
+    private boolean isDedicatedDevice;
+
+    @Column(name = "capacity")
+    private long capacity;
+
+    @Column(name = "allocation_state")
+    @Enumerated(value=EnumType.STRING)
+    private FirewallDeviceAllocationState allocationState;
+
+    //keeping it enum for future possible states Maintenance, Shutdown
+    public enum FirewallDeviceState {
+        Enabled,
+        Disabled
+    }
+
+    public enum FirewallDeviceAllocationState {
+        Free,
+        Allocated
+    }
+
+    public ExternalFirewallDeviceVO(long hostId, long physicalNetworkId, String provider_name, String device_name, long capacity, boolean dedicated) {
+        this.physicalNetworkId = physicalNetworkId;
+        this.providerName = provider_name;
+        this.deviceName = device_name;
+        this.hostId = hostId;
+        this.deviceState = FirewallDeviceState.Disabled;
+        this.allocationState = FirewallDeviceAllocationState.Free;
+        this.capacity = capacity;
+        this.isDedicatedDevice = dedicated;
+        this.deviceState = FirewallDeviceState.Enabled;
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public ExternalFirewallDeviceVO() {
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getPhysicalNetworkId() {
+        return physicalNetworkId;
+    }
+
+    public String getProviderName() {
+        return providerName;
+    }
+
+    public String getDeviceName() {
+        return deviceName;
+    }
+
+    public long getHostId() {
+        return hostId;
+    }
+
+    public long getCapacity() {
+        return capacity;
+    }
+
+    public void setCapacity(long capacity) {
+        this.capacity = capacity;
+    }
+
+    public FirewallDeviceState getDeviceState() {
+        return deviceState;
+    }
+
+    public void setDeviceState(FirewallDeviceState state) {
+        this.deviceState = state;
+    }
+
+    public FirewallDeviceAllocationState getAllocationState() {
+        return allocationState;
+    }
+
+    public void setAllocationState(FirewallDeviceAllocationState allocationState) {
+        this.allocationState = allocationState;
+    }
+
+    public boolean getIsDedicatedDevice() {
+        return isDedicatedDevice;
+    }
+
+    public void setIsDedicatedDevice(boolean isDedicated) {
+        isDedicatedDevice = isDedicated;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDao.java b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDao.java
new file mode 100644
index 0000000..b7baa7e
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDao.java
@@ -0,0 +1,74 @@
+// 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.network.dao.ExternalLoadBalancerDeviceVO.LBDeviceAllocationState;
+import com.cloud.network.dao.ExternalLoadBalancerDeviceVO.LBDeviceState;
+import com.cloud.utils.db.GenericDao;
+
+import java.util.List;
+
+public interface ExternalLoadBalancerDeviceDao extends GenericDao<ExternalLoadBalancerDeviceVO, Long> {
+
+    /**
+     * list all the load balancer devices added in to this physical network?
+     * @param physicalNetworkId physical Network Id
+     * @return list of ExternalLoadBalancerDeviceVO for the devices in the physical network.
+     */
+    List<ExternalLoadBalancerDeviceVO> listByPhysicalNetwork(long physicalNetworkId);
+
+    /**
+     * list the load balancer devices added in to this physical network of certain provider type?
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     */
+    List<ExternalLoadBalancerDeviceVO> listByPhysicalNetworkAndProvider(long physicalNetworkId, String provider_name);
+
+    /**
+     * list the load balancer devices added in to this physical network by their allocation state
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     * @param allocationState load balancer device allocation state
+     * @return list of ExternalLoadBalancerDeviceVO for the devices in the physical network with a device allocation state
+     */
+    List<ExternalLoadBalancerDeviceVO> listByProviderAndDeviceAllocationState(long physicalNetworkId, String provider_name, LBDeviceAllocationState allocationState);
+
+    /**
+     * list the load balancer devices added in to this physical network by the device status (enabled/disabled)
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     * @param state load balancer device status
+     * @return list of ExternalLoadBalancerDeviceVO for the devices in the physical network with a device state
+     */
+    List<ExternalLoadBalancerDeviceVO> listByProviderAndDeviceStaus(long physicalNetworkId, String provider_name, LBDeviceState state);
+
+    /**
+     * list the load balancer devices added in to this physical network by the managed type (external/cloudstack managed)
+     * @param physicalNetworkId physical Network Id
+     * @param provider_name netwrok service provider name
+     * @param managed managed type
+     * @return list of ExternalLoadBalancerDeviceVO for the devices in to this physical network of a managed type
+     */
+    List<ExternalLoadBalancerDeviceVO> listByProviderAndManagedType(long physicalNetworkId, String provider_name, boolean managed);
+
+    /**
+     * Find the external load balancer device that is provisioned as GSLB service provider in the pyshical network
+     * @param physicalNetworkId physical Network Id
+     * @return ExternalLoadBalancerDeviceVO for the device acting as GSLB provider in the physical network
+     */
+    ExternalLoadBalancerDeviceVO findGslbServiceProvider(long physicalNetworkId, String providerName);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDaoImpl.java b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDaoImpl.java
new file mode 100644
index 0000000..ea6437d
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceDaoImpl.java
@@ -0,0 +1,127 @@
+// 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.network.dao.ExternalLoadBalancerDeviceVO.LBDeviceAllocationState;
+import com.cloud.network.dao.ExternalLoadBalancerDeviceVO.LBDeviceState;
+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;
+import org.springframework.stereotype.Component;
+
+import javax.ejb.Local;
+import java.util.List;
+
+@Component
+@Local(value=ExternalLoadBalancerDeviceDao.class) @DB(txn=false)
+public class ExternalLoadBalancerDeviceDaoImpl extends GenericDaoBase<ExternalLoadBalancerDeviceVO, Long> implements ExternalLoadBalancerDeviceDao {
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> physicalNetworkIdSearch;
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> physicalNetworkServiceProviderSearch;
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> allocationStateSearch;
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> deviceStatusSearch;
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> deviceManagedTypeSearch;
+    final SearchBuilder<ExternalLoadBalancerDeviceVO> gslbProviderSearch;
+
+    public ExternalLoadBalancerDeviceDaoImpl() {
+        super();
+
+        physicalNetworkIdSearch = createSearchBuilder();
+        physicalNetworkIdSearch.and("physicalNetworkId", physicalNetworkIdSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        physicalNetworkIdSearch.done();
+
+        physicalNetworkServiceProviderSearch = createSearchBuilder();
+        physicalNetworkServiceProviderSearch.and("physicalNetworkId", physicalNetworkServiceProviderSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        physicalNetworkServiceProviderSearch.and("providerName", physicalNetworkServiceProviderSearch.entity().getProviderName(), Op.EQ);
+        physicalNetworkServiceProviderSearch.done();
+
+        allocationStateSearch = createSearchBuilder();
+        allocationStateSearch.and("physicalNetworkId", allocationStateSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        allocationStateSearch.and("providerName", allocationStateSearch.entity().getProviderName(), Op.EQ);
+        allocationStateSearch.and("allocationState", allocationStateSearch.entity().getAllocationState(), Op.EQ);
+        allocationStateSearch.done();
+
+        deviceStatusSearch = createSearchBuilder();
+        deviceStatusSearch.and("physicalNetworkId", deviceStatusSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        deviceStatusSearch.and("providerName", deviceStatusSearch.entity().getProviderName(), Op.EQ);
+        deviceStatusSearch.and("deviceState", deviceStatusSearch.entity().getState(), Op.EQ);
+        deviceStatusSearch.done();
+
+        deviceManagedTypeSearch = createSearchBuilder();
+        deviceManagedTypeSearch.and("physicalNetworkId", deviceManagedTypeSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        deviceManagedTypeSearch.and("providerName", deviceManagedTypeSearch.entity().getProviderName(), Op.EQ);
+        deviceManagedTypeSearch.and("managedType", deviceManagedTypeSearch.entity().getIsManagedDevice(), Op.EQ);
+        deviceManagedTypeSearch.done();
+
+        gslbProviderSearch = createSearchBuilder();
+        gslbProviderSearch.and("physicalNetworkId", gslbProviderSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        gslbProviderSearch.and("providerName", gslbProviderSearch.entity().getProviderName(), Op.EQ);
+        gslbProviderSearch.and("gslbProvider", gslbProviderSearch.entity().getGslbProvider(), Op.EQ);
+
+    }
+
+    public List<ExternalLoadBalancerDeviceVO> listByPhysicalNetwork(long physicalNetworkId) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = physicalNetworkIdSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return search(sc, null);
+    }
+
+    @Override
+    public List<ExternalLoadBalancerDeviceVO> listByPhysicalNetworkAndProvider(long physicalNetworkId, String provider_name) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = physicalNetworkServiceProviderSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", provider_name);
+        return search(sc, null);
+    }
+
+    @Override
+    public List<ExternalLoadBalancerDeviceVO> listByProviderAndDeviceAllocationState(long physicalNetworkId, String provider_name, LBDeviceAllocationState state) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = allocationStateSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", provider_name);
+        sc.setParameters("allocationState", state);
+        return search(sc, null);
+    }
+
+    @Override
+    public List<ExternalLoadBalancerDeviceVO> listByProviderAndDeviceStaus(long physicalNetworkId, String providerName, LBDeviceState state) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = deviceStatusSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", providerName);
+        sc.setParameters("deviceState", state);
+        return search(sc, null);
+    }
+
+    @Override
+    public List<ExternalLoadBalancerDeviceVO> listByProviderAndManagedType(long physicalNetworkId, String providerName, boolean managed) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = deviceManagedTypeSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", providerName);
+        sc.setParameters("managedType", managed);
+        return search(sc, null);
+    }
+
+    @Override
+    public ExternalLoadBalancerDeviceVO findGslbServiceProvider(long physicalNetworkId, String providerName) {
+        SearchCriteria<ExternalLoadBalancerDeviceVO> sc = gslbProviderSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        sc.setParameters("providerName", providerName);
+        sc.setParameters("gslbProvider", true);
+        return findOneBy(sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceVO.java b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceVO.java
new file mode 100644
index 0000000..04714a6
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/ExternalLoadBalancerDeviceVO.java
@@ -0,0 +1,227 @@
+// 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.Identity;
+import org.apache.cloudstack.api.InternalIdentity;
+import org.apache.cloudstack.network.ExternalNetworkDeviceManager;
+
+import javax.persistence.*;
+import java.util.UUID;
+
+/**
+ * ExternalLoadBalancerDeviceVO contains information on external load balancer devices (F5/Netscaler VPX,MPX,SDX) added into a deployment
+  */
+
+@Entity
+@Table(name="external_load_balancer_devices")
+public class ExternalLoadBalancerDeviceVO implements InternalIdentity, Identity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private long id;
+
+    @Column(name="uuid")
+    private String uuid;
+
+    @Column(name = "host_id")
+    private long hostId;
+
+    @Column(name = "physical_network_id")
+    private long physicalNetworkId;
+    
+    @Column(name = "provider_name")
+    private String providerName;
+
+    @Column(name = "device_name")
+    private String deviceName;
+
+    @Column(name="device_state")
+    @Enumerated(value=EnumType.STRING)
+    private LBDeviceState state;
+
+    @Column(name = "allocation_state")
+    @Enumerated(value=EnumType.STRING)
+    private LBDeviceAllocationState allocationState;
+
+    @Column(name="is_managed")
+    private boolean isManagedDevice;
+
+    @Column(name="is_dedicated")
+    private boolean isDedicatedDevice;
+
+    @Column(name="is_gslb_provider")
+    private boolean gslbProvider;
+
+    @Column(name="gslb_site_publicip")
+    private String gslbSitePublicIP;
+
+    @Column(name="gslb_site_privateip")
+    private String gslbSitePrivateIP;
+
+    @Column(name = "parent_host_id")
+    private long parentHostId;
+
+    @Column(name = "capacity")
+    private long capacity;
+
+    //keeping it enum for future possible states Maintenance, Shutdown
+    public enum LBDeviceState {
+        Enabled,
+        Disabled
+    }
+
+    public enum LBDeviceAllocationState {
+        Free,      // In this state no networks are using this device for load balancing
+        Shared,    // In this state one or more networks will be using this device for load balancing
+        Dedicated, // In this state this device is dedicated for a single network
+        Provider   // This state is set only for device that can dynamically provision LB appliances
+    }
+
+    public ExternalLoadBalancerDeviceVO(long hostId, long physicalNetworkId, String provider_name, String device_name,
+            long capacity, boolean dedicated, boolean gslbProvider) {
+        this.physicalNetworkId = physicalNetworkId;
+        this.providerName = provider_name;
+        this.deviceName = device_name;
+        this.hostId = hostId;
+        this.state = LBDeviceState.Disabled;
+        this.allocationState = LBDeviceAllocationState.Free;
+        this.capacity = capacity;
+        this.isDedicatedDevice = dedicated;
+        this.isManagedDevice = false;
+        this.state = LBDeviceState.Enabled;
+        this.uuid = UUID.randomUUID().toString();
+        this.gslbProvider = gslbProvider;
+        this.gslbSitePublicIP = null;
+        this.gslbSitePrivateIP = null;
+        if (device_name.equalsIgnoreCase(ExternalNetworkDeviceManager.NetworkDevice.NetscalerSDXLoadBalancer.getName())) {
+            this.allocationState = LBDeviceAllocationState.Provider;
+        }
+    }
+
+    public ExternalLoadBalancerDeviceVO(long hostId, long physicalNetworkId, String provider_name, String device_name,
+            long capacity, boolean dedicated, boolean managed, long parentHostId) {
+        this(hostId, physicalNetworkId, provider_name, device_name, capacity, dedicated, false);
+        this.isManagedDevice = managed;
+        this.parentHostId = parentHostId;
+    }
+
+    public ExternalLoadBalancerDeviceVO() {
+        this.uuid = UUID.randomUUID().toString();
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getPhysicalNetworkId() {
+        return physicalNetworkId;
+    }
+
+    public String getProviderName() {
+        return providerName;
+    }
+
+    public String getDeviceName() {
+        return deviceName;
+    }
+
+    public long getHostId() {
+        return hostId;
+    }
+
+    public long getParentHostId() {
+        return parentHostId;
+    }
+
+    public void setParentHostId(long parentHostId) {
+        this.parentHostId = parentHostId;
+    }
+
+    public long getCapacity() {
+        return capacity;
+    }
+
+    public void setCapacity(long capacity) {
+        this.capacity = capacity;
+    }
+
+    public LBDeviceState getState() {
+        return state;
+    }
+
+    public void setState(LBDeviceState state) {
+        this.state = state;
+    }    
+
+    public LBDeviceAllocationState getAllocationState() {
+        return allocationState;
+    }
+
+    public void setAllocationState(LBDeviceAllocationState allocationState) {
+        this.allocationState = allocationState;
+    }
+    
+    public boolean getIsManagedDevice() {
+        return isManagedDevice;
+    }
+
+    public void setIsManagedDevice(boolean managed) {
+        this.isManagedDevice = managed;
+    }
+
+    public boolean getIsDedicatedDevice() {
+        return isDedicatedDevice;
+    }
+
+    public void setIsDedicatedDevice(boolean isDedicated) {
+        isDedicatedDevice = isDedicated;
+    }
+
+    public boolean getGslbProvider() {
+        return gslbProvider;
+    }
+
+    public void setGslbProvider(boolean gslbProvider) {
+        gslbProvider = gslbProvider;
+    }
+
+    public void setGslbSitePublicIP(String gslbSitePublicIP) {
+        this.gslbSitePublicIP = gslbSitePublicIP;
+    }
+
+    public String getGslbSitePublicIP() {
+        return gslbSitePublicIP;
+    }
+
+    public void setGslbSitePrivateIP(String gslbSitePrivateIP) {
+        this.gslbSitePrivateIP = gslbSitePrivateIP;
+    }
+
+    public String getGslbSitePrivateIP() {
+        return gslbSitePrivateIP;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDao.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDao.java
new file mode 100644
index 0000000..d020ea7
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDao.java
@@ -0,0 +1,29 @@
+// 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 FirewallRulesCidrsDao extends GenericDao<FirewallRulesCidrsVO, Long> {
+    
+    void persist(long firewallRuleId, List<String> sourceCidrs);
+    
+    List<String> getSourceCidrs(long firewallRuleId);
+    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java
new file mode 100644
index 0000000..b007e19
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java
@@ -0,0 +1,70 @@
+// 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.apache.log4j.Logger;
+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.Transaction;
+
+@Component
+@Local(value=FirewallRulesCidrsDao.class)
+public class FirewallRulesCidrsDaoImpl extends GenericDaoBase<FirewallRulesCidrsVO, Long> implements FirewallRulesCidrsDao {
+    private static final Logger s_logger = Logger.getLogger(FirewallRulesCidrsDaoImpl.class);
+    protected final SearchBuilder<FirewallRulesCidrsVO> CidrsSearch;
+    
+    protected FirewallRulesCidrsDaoImpl() {
+        CidrsSearch = createSearchBuilder();
+        CidrsSearch.and("firewallRuleId", CidrsSearch.entity().getFirewallRuleId(), SearchCriteria.Op.EQ);
+        CidrsSearch.done();        
+    }
+
+    @Override @DB
+    public List<String> getSourceCidrs(long firewallRuleId) {
+        SearchCriteria<FirewallRulesCidrsVO> sc = CidrsSearch.create();
+        sc.setParameters("firewallRuleId", firewallRuleId);
+        
+        List<FirewallRulesCidrsVO> results = search(sc, null);
+        List<String> cidrs = new ArrayList<String>(results.size());
+        for (FirewallRulesCidrsVO result : results) {
+            cidrs.add(result.getCidr());
+        }
+
+        return cidrs;
+    }
+    
+    @Override @DB
+    public void persist(long firewallRuleId, List<String> sourceCidrs) {
+        Transaction txn = Transaction.currentTxn();
+
+        txn.start();
+        for (String tag : sourceCidrs) {
+            FirewallRulesCidrsVO vo = new FirewallRulesCidrsVO(firewallRuleId, tag);
+            persist(vo);
+        }
+        txn.commit();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsVO.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsVO.java
new file mode 100644
index 0000000..75b8919
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsVO.java
@@ -0,0 +1,65 @@
+// 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.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name=("firewall_rules_cidrs"))
+public class FirewallRulesCidrsVO implements InternalIdentity {
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    private Long id;
+
+    @Column(name="firewall_rule_id")
+    private long firewallRuleId;
+
+    @Column(name="source_cidr")
+    private String sourceCidrList;
+
+    public FirewallRulesCidrsVO() { }
+
+    public FirewallRulesCidrsVO(long firewallRuleId, String sourceCidrList) {
+        this.firewallRuleId = firewallRuleId;
+        this.sourceCidrList = sourceCidrList;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getFirewallRuleId() {
+        return firewallRuleId;
+    }
+
+    public String getCidr() {
+        return sourceCidrList;
+    }
+
+    public String getSourceCidrList() {
+        return sourceCidrList;
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/FirewallRulesDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesDao.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesDao.java
new file mode 100644
index 0000000..0bbaa93
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesDao.java
@@ -0,0 +1,65 @@
+// 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.host.HostVO;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.FirewallRuleVO;
+import com.cloud.utils.db.GenericDao;
+
+/*
+ * Data Access Object for user_ip_address and ip_forwarding tables
+ */
+public interface FirewallRulesDao extends GenericDao<FirewallRuleVO, Long> {
+
+    List<FirewallRuleVO> listByIpAndPurposeAndNotRevoked(long ipAddressId, FirewallRule.Purpose purpose);
+
+    List<FirewallRuleVO> listByNetworkAndPurposeAndNotRevoked(long networkId, FirewallRule.Purpose purpose);
+
+    boolean setStateToAdd(FirewallRuleVO rule);
+
+    boolean revoke(FirewallRuleVO rule);
+
+    boolean releasePorts(long ipAddressId, String protocol, FirewallRule.Purpose purpose, int[] ports);
+
+    List<FirewallRuleVO> listByIpAndPurpose(long ipAddressId, FirewallRule.Purpose purpose);
+
+    List<FirewallRuleVO> listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose);
+
+    List<FirewallRuleVO> listStaticNatByVmId(long vmId);
+
+    List<FirewallRuleVO> listByIpPurposeAndProtocolAndNotRevoked(long ipAddressId, Integer startPort, Integer endPort, String protocol, FirewallRule.Purpose purpose);
+
+    FirewallRuleVO findByRelatedId(long ruleId);
+
+    List<FirewallRuleVO> listSystemRules();
+
+    List<FirewallRuleVO> listByIp(long ipAddressId);
+
+    List<FirewallRuleVO> listByIpAndNotRevoked(long ipAddressId);
+
+    long countRulesByIpId(long sourceIpId);
+    
+    List<FirewallRuleVO> listByNetworkPurposeTrafficTypeAndNotRevoked(long networkId, FirewallRule.Purpose purpose, FirewallRule.TrafficType trafficType);
+    List<FirewallRuleVO> listByNetworkPurposeTrafficType(long networkId, FirewallRule.Purpose purpose, FirewallRule.TrafficType trafficType);
+    
+    List<FirewallRuleVO> listByIpAndPurposeWithState(Long addressId, FirewallRule.Purpose purpose, FirewallRule.State state);
+        
+    void loadSourceCidrs(FirewallRuleVO rule);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
new file mode 100644
index 0000000..45a8068
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
@@ -0,0 +1,352 @@
+// 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 javax.inject.Inject;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.FirewallRule.FirewallRuleType;
+import com.cloud.network.rules.FirewallRule.Purpose;
+import com.cloud.network.rules.FirewallRule.State;
+import com.cloud.network.rules.FirewallRule.TrafficType;
+import com.cloud.network.rules.FirewallRuleVO;
+import com.cloud.server.ResourceTag.TaggedResourceType;
+import com.cloud.tags.dao.ResourceTagDao;
+import com.cloud.tags.dao.ResourceTagsDaoImpl;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.Transaction;
+
+@Component
+@Local(value = FirewallRulesDao.class)
+@DB(txn = false)
+public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> implements FirewallRulesDao {
+
+    protected final SearchBuilder<FirewallRuleVO> AllFieldsSearch;
+    protected final SearchBuilder<FirewallRuleVO> NotRevokedSearch;
+    protected final SearchBuilder<FirewallRuleVO> ReleaseSearch;
+    protected SearchBuilder<FirewallRuleVO> VmSearch;
+    protected final SearchBuilder<FirewallRuleVO> SystemRuleSearch;
+    protected final GenericSearchBuilder<FirewallRuleVO, Long> RulesByIpCount;
+
+    @Inject protected FirewallRulesCidrsDao _firewallRulesCidrsDao;
+    @Inject ResourceTagDao _tagsDao;
+    @Inject IPAddressDao _ipDao;
+
+    protected FirewallRulesDaoImpl() {
+        super();
+
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("ipId", AllFieldsSearch.entity().getSourceIpAddressId(), Op.EQ);
+        AllFieldsSearch.and("protocol", AllFieldsSearch.entity().getProtocol(), Op.EQ);
+        AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
+        AllFieldsSearch.and("purpose", AllFieldsSearch.entity().getPurpose(), Op.EQ);
+        AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
+        AllFieldsSearch.and("domain", AllFieldsSearch.entity().getDomainId(), Op.EQ);
+        AllFieldsSearch.and("id", AllFieldsSearch.entity().getId(), Op.EQ);
+        AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
+        AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
+        AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
+        AllFieldsSearch.done();
+
+        NotRevokedSearch = createSearchBuilder();
+        NotRevokedSearch.and("ipId", NotRevokedSearch.entity().getSourceIpAddressId(), Op.EQ);
+        NotRevokedSearch.and("state", NotRevokedSearch.entity().getState(), Op.NEQ);
+        NotRevokedSearch.and("purpose", NotRevokedSearch.entity().getPurpose(), Op.EQ);
+        NotRevokedSearch.and("protocol", NotRevokedSearch.entity().getProtocol(), Op.EQ);
+        NotRevokedSearch.and("sourcePortStart", NotRevokedSearch.entity().getSourcePortStart(), Op.EQ);
+        NotRevokedSearch.and("sourcePortEnd", NotRevokedSearch.entity().getSourcePortEnd(), Op.EQ);
+        NotRevokedSearch.and("networkId", NotRevokedSearch.entity().getNetworkId(), Op.EQ);
+        NotRevokedSearch.and("trafficType", NotRevokedSearch.entity().getTrafficType(), Op.EQ);
+        NotRevokedSearch.done();
+
+        ReleaseSearch = createSearchBuilder();
+        ReleaseSearch.and("protocol", ReleaseSearch.entity().getProtocol(), Op.EQ);
+        ReleaseSearch.and("ipId", ReleaseSearch.entity().getSourceIpAddressId(), Op.EQ);
+        ReleaseSearch.and("purpose", ReleaseSearch.entity().getPurpose(), Op.EQ);
+        ReleaseSearch.and("ports", ReleaseSearch.entity().getSourcePortStart(), Op.IN);
+        ReleaseSearch.done();
+
+        SystemRuleSearch = createSearchBuilder();
+        SystemRuleSearch.and("type", SystemRuleSearch.entity().getType(), Op.EQ);
+        SystemRuleSearch.and("ipId", SystemRuleSearch.entity().getSourceIpAddressId(), Op.NULL);
+        SystemRuleSearch.done();
+
+        RulesByIpCount = createSearchBuilder(Long.class);
+        RulesByIpCount.select(null, Func.COUNT, RulesByIpCount.entity().getId());
+        RulesByIpCount.and("ipAddressId", RulesByIpCount.entity().getSourceIpAddressId(), Op.EQ);
+        RulesByIpCount.done();
+    }
+
+    @Override
+    public List<FirewallRuleVO> listSystemRules() {
+        SearchCriteria<FirewallRuleVO> sc = SystemRuleSearch.create();
+        sc.setParameters("type", FirewallRuleType.System.toString());
+        return listBy(sc);
+    }
+
+    @Override
+    public boolean releasePorts(long ipId, String protocol, FirewallRule.Purpose purpose, int[] ports) {
+        SearchCriteria<FirewallRuleVO> sc = ReleaseSearch.create();
+        sc.setParameters("protocol", protocol);
+        sc.setParameters("ipId", ipId);
+        sc.setParameters("purpose", purpose);
+        sc.setParameters("ports", ports);
+
+        int results = remove(sc);
+        return results == ports.length;
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIpAndPurpose(long ipId, FirewallRule.Purpose purpose) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("ipId", ipId);
+        sc.setParameters("purpose", purpose);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIpAndPurposeAndNotRevoked(long ipId, FirewallRule.Purpose purpose) {
+        SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
+        sc.setParameters("ipId", ipId);
+        sc.setParameters("state", State.Revoke);
+
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByNetworkAndPurposeAndNotRevoked(long networkId, FirewallRule.Purpose purpose) {
+        SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("state", State.Revoke);
+
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("purpose", purpose);
+        sc.setParameters("networkId", networkId);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByNetworkPurposeTrafficTypeAndNotRevoked(long networkId, FirewallRule.Purpose purpose, TrafficType trafficType) {
+        SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("state", State.Revoke);
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+        sc.setParameters("trafficType", trafficType);
+
+        return listBy(sc);
+    }
+
+
+    @Override
+    public boolean setStateToAdd(FirewallRuleVO rule) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("id", rule.getId());
+        sc.setParameters("state", State.Staged);
+
+        rule.setState(State.Add);
+
+        return update(rule, sc) > 0;
+    }
+
+    @Override
+    public boolean revoke(FirewallRuleVO rule) {
+        rule.setState(State.Revoke);
+        return update(rule.getId(), rule);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listStaticNatByVmId(long vmId) {
+        if (VmSearch == null) {
+            SearchBuilder<IPAddressVO> IpSearch = _ipDao.createSearchBuilder();
+            IpSearch.and("associatedWithVmId", IpSearch.entity().getAssociatedWithVmId(), SearchCriteria.Op.EQ);
+            IpSearch.and("oneToOneNat", IpSearch.entity().isOneToOneNat(), SearchCriteria.Op.NNULL);
+
+            VmSearch = createSearchBuilder();
+            VmSearch.and("purpose", VmSearch.entity().getPurpose(), Op.EQ);
+            VmSearch.join("ipSearch", IpSearch, VmSearch.entity().getSourceIpAddressId(), IpSearch.entity().getId(), JoinBuilder.JoinType.INNER);
+            VmSearch.done();
+        }
+
+        SearchCriteria<FirewallRuleVO> sc = VmSearch.create();
+        sc.setParameters("purpose", Purpose.StaticNat);
+        sc.setJoinParameters("ipSearch", "associatedWithVmId", vmId);
+
+        return listBy(sc);
+    }
+
+    @Override
+    @DB
+    public FirewallRuleVO persist(FirewallRuleVO firewallRule) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+
+        FirewallRuleVO dbfirewallRule = super.persist(firewallRule);
+        saveSourceCidrs(firewallRule, firewallRule.getSourceCidrList());
+        loadSourceCidrs(dbfirewallRule);
+
+        txn.commit();
+        return dbfirewallRule;
+    }
+
+    public void saveSourceCidrs(FirewallRuleVO firewallRule, List<String> cidrList) {
+        if (cidrList == null) {
+            return;
+        }
+        _firewallRulesCidrsDao.persist(firewallRule.getId(), cidrList);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIpPurposeAndProtocolAndNotRevoked(long ipAddressId, Integer startPort, Integer endPort, String protocol, FirewallRule.Purpose purpose) {
+        SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
+        sc.setParameters("ipId", ipAddressId);
+        sc.setParameters("state", State.Revoke);
+
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+
+        if (protocol != null) {
+            sc.setParameters("protocol", protocol);
+        }
+
+        sc.setParameters("sourcePortStart", startPort);
+
+        sc.setParameters("sourcePortEnd", endPort);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public FirewallRuleVO findByRelatedId(long ruleId) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("related", ruleId);
+        sc.setParameters("purpose", Purpose.Firewall);
+
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIp(long ipId) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("ipId", ipId);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIpAndNotRevoked(long ipId) {
+        SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
+        sc.setParameters("ipId", ipId);
+        sc.setParameters("state", State.Revoke);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public long countRulesByIpId(long sourceIpId) {
+        SearchCriteria<Long> sc = RulesByIpCount.create();
+        sc.setParameters("ipAddressId", sourceIpId);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByNetworkPurposeTrafficType(long networkId, Purpose purpose, TrafficType trafficType) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("networkId", networkId);
+
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+
+        sc.setParameters("trafficType", trafficType);
+
+        return listBy(sc);
+    }
+
+    @Override
+    @DB
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        FirewallRuleVO entry = findById(id);
+        if (entry != null) {
+            if (entry.getPurpose() == Purpose.LoadBalancing) {
+                _tagsDao.removeByIdAndType(id, TaggedResourceType.LoadBalancer);
+            } else if (entry.getPurpose() == Purpose.PortForwarding) {
+                _tagsDao.removeByIdAndType(id, TaggedResourceType.PortForwardingRule);
+            } else if (entry.getPurpose() == Purpose.Firewall) {
+                _tagsDao.removeByIdAndType(id, TaggedResourceType.FirewallRule);
+            } else if (entry.getPurpose() == Purpose.NetworkACL) {
+                _tagsDao.removeByIdAndType(id, TaggedResourceType.NetworkACL);
+            }
+        }
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+
+    @Override
+    public List<FirewallRuleVO> listByIpAndPurposeWithState(Long ipId, Purpose purpose, State state) {
+        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("ipId", ipId);
+
+        if (state != null) {
+            sc.setParameters("state", state);
+        }
+
+        if (purpose != null) {
+            sc.setParameters("purpose", purpose);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override
+    public void loadSourceCidrs(FirewallRuleVO rule) {
+        List<String> sourceCidrs = _firewallRulesCidrsDao.getSourceCidrs(rule.getId());
+        rule.setSourceCidrList(sourceCidrs);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDao.java b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
new file mode 100755
index 0000000..3d588fa
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
@@ -0,0 +1,71 @@
+// 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.dc.Vlan.VlanType;
+import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.net.Ip;
+
+public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
+	
+    IPAddressVO markAsUnavailable(long ipAddressId);
+    
+	void unassignIpAddress(long ipAddressId);	
+
+	List<IPAddressVO> listByAccount(long accountId);
+	
+	List<IPAddressVO> listByVlanId(long vlanId);
+	
+	List<IPAddressVO> listByDcIdIpAddress(long dcId, String ipAddress);
+	
+	List<IPAddressVO> listByDcId(long dcId); 
+	
+	List<IPAddressVO> listByAssociatedNetwork(long networkId, Boolean isSourceNat);
+	
+	List<IPAddressVO> listStaticNatPublicIps(long networkId);
+	
+	int countIPs(long dcId, long vlanDbId, boolean onlyCountAllocated);
+	
+	int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask);
+	
+    long countAllocatedIPsForAccount(long accountId);
+	
+	boolean mark(long dcId, Ip ip);
+
+	int countIPsForNetwork(long dcId, boolean onlyCountAllocated, VlanType vlanType);
+	
+	IPAddressVO findByAssociatedVmId(long vmId);
+	
+	IPAddressVO findByIpAndSourceNetworkId(long networkId, String ipAddress);
+
+	public IPAddressVO findByIpAndDcId(long dcId, String ipAddress);
+
+	List<IPAddressVO> listByPhysicalNetworkId(long physicalNetworkId);
+	
+	List<IPAddressVO> listByAssociatedVpc(long vpcId, Boolean isSourceNat);
+
+	long countFreePublicIPs();
+	
+    long countFreeIPsInNetwork(long networkId);
+    IPAddressVO findByVmIp(String vmIp);
+
+    IPAddressVO findByAssociatedVmIdAndVmIp(long vmId, String vmIp);
+
+    IPAddressVO findByIpAndNetworkId(long networkId, String ipAddress);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
new file mode 100755
index 0000000..73f310f
--- /dev/null
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
@@ -0,0 +1,376 @@
+// 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.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Date;
+import java.util.List;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.dc.Vlan.VlanType;
+import com.cloud.dc.VlanVO;
+import com.cloud.dc.dao.VlanDao;
+import com.cloud.dc.dao.VlanDaoImpl;
+import com.cloud.network.IpAddress.State;
+import com.cloud.server.ResourceTag.TaggedResourceType;
+import com.cloud.tags.dao.ResourceTagDao;
+import com.cloud.tags.dao.ResourceTagsDaoImpl;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.GenericSearchBuilder;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Func;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.net.Ip;
+
+@Component
+@Local(value = { IPAddressDao.class })
+@DB
+public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implements IPAddressDao {
+    private static final Logger s_logger = Logger.getLogger(IPAddressDaoImpl.class);
+
+    protected SearchBuilder<IPAddressVO> AllFieldsSearch;
+    protected SearchBuilder<IPAddressVO> VlanDbIdSearchUnallocated;
+    protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCount;
+    protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCount;
+    protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDashboard;    
+    protected GenericSearchBuilder<IPAddressVO, Long> AllocatedIpCountForAccount;    
+    @Inject protected VlanDao _vlanDao;
+    protected GenericSearchBuilder<IPAddressVO, Long> CountFreePublicIps;
+    @Inject ResourceTagDao _tagsDao;
+
+    // make it public for JUnit test
+    public IPAddressDaoImpl() {
+    }
+
+    @PostConstruct
+    public void init() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("id", AllFieldsSearch.entity().getId(), Op.EQ);
+        AllFieldsSearch.and("dataCenterId", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
+        AllFieldsSearch.and("ipAddress", AllFieldsSearch.entity().getAddress(), Op.EQ);
+        AllFieldsSearch.and("vlan", AllFieldsSearch.entity().getVlanId(), Op.EQ);
+        AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAllocatedToAccountId(), Op.EQ);
+        AllFieldsSearch.and("sourceNat", AllFieldsSearch.entity().isSourceNat(), Op.EQ);
+        AllFieldsSearch.and("network", AllFieldsSearch.entity().getAssociatedWithNetworkId(), Op.EQ);
+        AllFieldsSearch.and("associatedWithVmId", AllFieldsSearch.entity().getAssociatedWithVmId(), Op.EQ);
+        AllFieldsSearch.and("oneToOneNat", AllFieldsSearch.entity().isOneToOneNat(), Op.EQ);
+        AllFieldsSearch.and("sourcenetwork", AllFieldsSearch.entity().getSourceNetworkId(), Op.EQ);
+        AllFieldsSearch.and("physicalNetworkId", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
+        AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
+        AllFieldsSearch.and("associatedVmIp", AllFieldsSearch.entity().getVmIp(), Op.EQ);
+        AllFieldsSearch.done();
+
+        VlanDbIdSearchUnallocated = createSearchBuilder();
+        VlanDbIdSearchUnallocated.and("allocated", VlanDbIdSearchUnallocated.entity().getAllocatedTime(), Op.NULL);
+        VlanDbIdSearchUnallocated.and("vlanDbId", VlanDbIdSearchUnallocated.entity().getVlanId(), Op.EQ);
+        VlanDbIdSearchUnallocated.done();
+
+        AllIpCount = createSearchBuilder(Integer.class);
+        AllIpCount.select(null, Func.COUNT, AllIpCount.entity().getAddress());
+        AllIpCount.and("dc", AllIpCount.entity().getDataCenterId(), Op.EQ);
+        AllIpCount.and("vlan", AllIpCount.entity().getVlanId(), Op.EQ);
+        AllIpCount.done();
+
+        AllocatedIpCount = createSearchBuilder(Integer.class);
+        AllocatedIpCount.select(null, Func.COUNT, AllocatedIpCount.entity().getAddress());
+        AllocatedIpCount.and("dc", AllocatedIpCount.entity().getDataCenterId(), Op.EQ);
+        AllocatedIpCount.and("vlan", AllocatedIpCount.entity().getVlanId(), Op.EQ);
+        AllocatedIpCount.and("allocated", AllocatedIpCount.entity().getAllocatedTime(), Op.NNULL);
+        AllocatedIpCount.done();              
+
+        AllIpCountForDashboard = createSearchBuilder(Integer.class);
+        AllIpCountForDashboard.select(null, Func.COUNT, AllIpCountForDashboard.entity().getAddress());
+        AllIpCountForDashboard.and("dc", AllIpCountForDashboard.entity().getDataCenterId(), Op.EQ);        
+        AllIpCountForDashboard.and("state", AllIpCountForDashboard.entity().getState(), SearchCriteria.Op.NEQ);                
+
+        SearchBuilder<VlanVO> virtaulNetworkVlan = _vlanDao.createSearchBuilder();
+        virtaulNetworkVlan.and("vlanType", virtaulNetworkVlan.entity().getVlanType(), SearchCriteria.Op.EQ);
+
+        AllIpCountForDashboard.join("vlan", virtaulNetworkVlan, virtaulNetworkVlan.entity().getId(),
+                AllIpCountForDashboard.entity().getVlanId(), JoinBuilder.JoinType.INNER);
+        virtaulNetworkVlan.done();
+        AllIpCountForDashboard.done();
+
+        AllocatedIpCountForAccount = createSearchBuilder(Long.class);
+        AllocatedIpCountForAccount.select(null, Func.COUNT, AllocatedIpCountForAccount.entity().getAddress());
+        AllocatedIpCountForAccount.and("account", AllocatedIpCountForAccount.entity().getAllocatedToAccountId(), Op.EQ);
+        AllocatedIpCountForAccount.and("allocated", AllocatedIpCountForAccount.entity().getAllocatedTime(), Op.NNULL);
+        AllocatedIpCountForAccount.and("network", AllocatedIpCountForAccount.entity().getAssociatedWithNetworkId(), Op.NNULL);        
+        AllocatedIpCountForAccount.done();
+
+        CountFreePublicIps = createSearchBuilder(Long.class);
+        CountFreePublicIps.select(null, Func.COUNT, null);
+        CountFreePublicIps.and("state", CountFreePublicIps.entity().getState(), SearchCriteria.Op.EQ);
+        CountFreePublicIps.and("networkId", CountFreePublicIps.entity().getSourceNetworkId(), SearchCriteria.Op.EQ);
+        SearchBuilder<VlanVO> join = _vlanDao.createSearchBuilder();
+        join.and("vlanType", join.entity().getVlanType(), Op.EQ);
+        CountFreePublicIps.join("vlans", join, CountFreePublicIps.entity().getVlanId(), join.entity().getId(), JoinBuilder.JoinType.INNER);
+        CountFreePublicIps.done();
+    }
+
+    @Override
+    public boolean mark(long dcId, Ip ip) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("dataCenterId", dcId);
+        sc.setParameters("ipAddress", ip);
+
+        IPAddressVO vo = createForUpdate();
+        vo.setAllocatedTime(new Date());
+        vo.setState(State.Allocated);
+
+        return update(vo, sc) >= 1;
+    }
+
+    @Override
+    public void unassignIpAddress(long ipAddressId) {
+        IPAddressVO address = createForUpdate();
+        address.setAllocatedToAccountId(null);
+        address.setAllocatedInDomainId(null);
+        address.setAllocatedTime(null);
+        address.setSourceNat(false);
+        address.setOneToOneNat(false);
+        address.setAssociatedWithVmId(null);
+        address.setState(State.Free);
+        address.setAssociatedWithNetworkId(null);
+        address.setVpcId(null);
+        address.setSystem(false);
+        update(ipAddressId, address);
+    }
+
+    @Override
+    public List<IPAddressVO> listByAccount(long accountId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("accountId", accountId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<IPAddressVO> listByVlanId(long vlanId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("vlan", vlanId);
+        return listBy(sc);
+    }
+
+    @Override
+    public IPAddressVO findByIpAndSourceNetworkId(long networkId, String ipAddress) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("sourcenetwork", networkId);
+        sc.setParameters("ipAddress", ipAddress);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public IPAddressVO findByIpAndNetworkId(long networkId, String ipAddress) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("ipAddress", ipAddress);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public IPAddressVO findByIpAndDcId(long dcId, String ipAddress) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("dataCenterId", dcId);
+        sc.setParameters("ipAddress", ipAddress);
+        return findOneBy(sc);
+    }
+
+    @Override
+    public List<IPAddressVO> listByDcId(long dcId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("dataCenterId", dcId);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<IPAddressVO> listByDcIdIpAddress(long dcId, String ipAddress) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("dataCenterId", dcId);
+        sc.setParameters("ipAddress", ipAddress);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<IPAddressVO> listByAssociatedNetwork(long networkId, Boolean isSourceNat) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+
+        if (isSourceNat != null) {
+            sc.setParameters("sourceNat", isSourceNat);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override 
+    public List<IPAddressVO> listStaticNatPublicIps(long networkId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("network", networkId);
+        sc.setParameters("oneToOneNat", true);
+        return listBy(sc);        
+    }
+
+    @Override
+    public IPAddressVO findByAssociatedVmId(long vmId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("associatedWithVmId", vmId);
+
+        return findOneBy(sc);
+    }
+
+    @Override
+    public IPAddressVO findByVmIp(String vmIp) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("associatedVmIp", vmIp);
+        return findOneBy(sc);
+    }
+    @Override
+    public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) {
+        SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create();
+        sc.setParameters("dc", dcId);
+        sc.setParameters("vlan", vlanId);
+
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public int countIPsForNetwork(long dcId, boolean onlyCountAllocated, VlanType vlanType) {
+        SearchCriteria<Integer> sc = AllIpCountForDashboard.create();
+        sc.setParameters("dc", dcId);
+        if (onlyCountAllocated){
+            sc.setParameters("state", State.Free);
+        }
+        sc.setJoinParameters("vlan", "vlanType", vlanType.toString());
+        return customSearch(sc, null).get(0);
+    }
+
+
+    @Override
+    @DB
+    public int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask) {
+        Transaction txn = Transaction.currentTxn();
+        int ipCount = 0;
+        try {
+            String sql = "SELECT count(*) FROM user_ip_address u INNER JOIN vlan v on (u.vlan_db_id = v.id AND v.data_center_id = ? AND v.vlan_id = ? AND v.vlan_gateway = ? AND v.vlan_netmask = ? AND u.account_id = ?)";
+
+            PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql);
+            pstmt.setLong(1, dcId);
+            pstmt.setString(2, vlanId);
+            pstmt.setString(3, vlanGateway);
+            pstmt.setString(4, vlanNetmask);
+            pstmt.setLong(5, accountId);
+            ResultSet rs = pstmt.executeQuery();
+
+            if (rs.next()) {
+                ipCount = rs.getInt(1);
+            }
+        } catch (Exception e) {
+            s_logger.warn("Exception counting IP addresses", e);
+        }
+
+        return ipCount;
+    }
+
+    @Override @DB
+    public IPAddressVO markAsUnavailable(long ipAddressId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("id", ipAddressId);
+
+        IPAddressVO ip = createForUpdate();
+        ip.setState(State.Releasing);
+        if (update(ip, sc) != 1) {
+            return null;
+        }
+
+        return findOneBy(sc);
+    }
+
+    @Override
+    public long countAllocatedIPsForAccount(long accountId) {
+        SearchCriteria<Long> sc = AllocatedIpCountForAccount.create();
+        sc.setParameters("account", accountId);
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
+    public List<IPAddressVO> listByPhysicalNetworkId(long physicalNetworkId) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("physicalNetworkId", physicalNetworkId);
+        return listBy(sc);
+    }
+
+    @Override
+    public long countFreePublicIPs() {
+        SearchCriteria<Long> sc = CountFreePublicIps.create();
+        sc.setParameters("state", State.Free);
+        sc.setJoinParameters("vlans", "vlanType", VlanType.VirtualNetwork);
+        return customSearch(sc, null).get(0);       
+    }
+
+    @Override
+    public List<IPAddressVO> listByAssociatedVpc(long vpcId, Boolean isSourceNat) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("vpcId", vpcId);
+
+        if (isSourceNat != null) {
+            sc.setParameters("sourceNat", isSourceNat);
+        }
+
+        return listBy(sc);
+    }
+
+    @Override
+    public long countFreeIPsInNetwork(long networkId) {
+        SearchCriteria<Long> sc = CountFreePublicIps.create();
+        sc.setParameters("state", State.Free);
+        sc.setParameters("networkId", networkId);
+        return customSearch(sc, null).get(0);       
+    }
+
+    @Override
+    @DB
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        IPAddressVO entry = findById(id);
+        if (entry != null) {
+            _tagsDao.removeByIdAndType(id, TaggedResourceType.SecurityGroup);
+        }
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+
+    @Override
+    public IPAddressVO findByAssociatedVmIdAndVmIp(long vmId, String vmIp) {
+        SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
+        sc.setParameters("associatedWithVmId", vmId);
+        sc.setParameters("associatedVmIp", vmIp);
+        return findOneBy(sc);
+    }
+}


Mime
View raw message