Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DD9F1106FD for ; Mon, 6 May 2013 23:34:56 +0000 (UTC) Received: (qmail 29604 invoked by uid 500); 6 May 2013 23:34:44 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 29532 invoked by uid 500); 6 May 2013 23:34:44 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 28579 invoked by uid 99); 6 May 2013 23:34:43 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 May 2013 23:34:43 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 7569E888046; Mon, 6 May 2013 23:34:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ahuang@apache.org To: commits@cloudstack.apache.org Date: Mon, 06 May 2013 23:35:23 -0000 Message-Id: <66fda5224b8841928cc00649d3b92a1a@git.apache.org> In-Reply-To: <6e057524610d405099989be2bef2d4fc@git.apache.org> References: <6e057524610d405099989be2bef2d4fc@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [43/56] [abbrv] [partial] Moved most of the VOs and DAOs from server package into engine-schema as well 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 { + public List 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 implements CounterDao { + final SearchBuilder 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 listCounters(Long id, String name, String source, String keyword, Filter filter) { + SearchCriteria sc = AllFieldsSearch.create(); + + if (keyword != null) { + SearchCriteria 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 { + + /** + * 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 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 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 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 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 implements ExternalFirewallDeviceDao { + final SearchBuilder physicalNetworkServiceProviderSearch; + final SearchBuilder physicalNetworkIdSearch; + final SearchBuilder allocationStateSearch; + final SearchBuilder 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 listByPhysicalNetwork(long physicalNetworkId) { + SearchCriteria sc = physicalNetworkIdSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + return search(sc, null); + } + + @Override + public List listByPhysicalNetworkAndProvider(long physicalNetworkId, String providerName) { + SearchCriteria sc = physicalNetworkServiceProviderSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("networkServiceProviderName", providerName); + return search(sc, null); + } + + @Override + public List listByProviderAndDeviceAllocationState(long physicalNetworkId, String providerName, FirewallDeviceAllocationState allocationState) { + SearchCriteria sc = allocationStateSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("providerName", providerName); + sc.setParameters("allocationState", allocationState); + return search(sc, null); + } + + @Override + public List listByProviderAndDeviceStaus(long physicalNetworkId, String providerName, FirewallDeviceState state) { + SearchCriteria 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 { + + /** + * 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 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 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 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 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 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 implements ExternalLoadBalancerDeviceDao { + final SearchBuilder physicalNetworkIdSearch; + final SearchBuilder physicalNetworkServiceProviderSearch; + final SearchBuilder allocationStateSearch; + final SearchBuilder deviceStatusSearch; + final SearchBuilder deviceManagedTypeSearch; + final SearchBuilder 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 listByPhysicalNetwork(long physicalNetworkId) { + SearchCriteria sc = physicalNetworkIdSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + return search(sc, null); + } + + @Override + public List listByPhysicalNetworkAndProvider(long physicalNetworkId, String provider_name) { + SearchCriteria sc = physicalNetworkServiceProviderSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("providerName", provider_name); + return search(sc, null); + } + + @Override + public List listByProviderAndDeviceAllocationState(long physicalNetworkId, String provider_name, LBDeviceAllocationState state) { + SearchCriteria sc = allocationStateSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("providerName", provider_name); + sc.setParameters("allocationState", state); + return search(sc, null); + } + + @Override + public List listByProviderAndDeviceStaus(long physicalNetworkId, String providerName, LBDeviceState state) { + SearchCriteria sc = deviceStatusSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("providerName", providerName); + sc.setParameters("deviceState", state); + return search(sc, null); + } + + @Override + public List listByProviderAndManagedType(long physicalNetworkId, String providerName, boolean managed) { + SearchCriteria 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 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 { + + void persist(long firewallRuleId, List sourceCidrs); + + List 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 implements FirewallRulesCidrsDao { + private static final Logger s_logger = Logger.getLogger(FirewallRulesCidrsDaoImpl.class); + protected final SearchBuilder CidrsSearch; + + protected FirewallRulesCidrsDaoImpl() { + CidrsSearch = createSearchBuilder(); + CidrsSearch.and("firewallRuleId", CidrsSearch.entity().getFirewallRuleId(), SearchCriteria.Op.EQ); + CidrsSearch.done(); + } + + @Override @DB + public List getSourceCidrs(long firewallRuleId) { + SearchCriteria sc = CidrsSearch.create(); + sc.setParameters("firewallRuleId", firewallRuleId); + + List results = search(sc, null); + List cidrs = new ArrayList(results.size()); + for (FirewallRulesCidrsVO result : results) { + cidrs.add(result.getCidr()); + } + + return cidrs; + } + + @Override @DB + public void persist(long firewallRuleId, List 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 { + + List listByIpAndPurposeAndNotRevoked(long ipAddressId, FirewallRule.Purpose purpose); + + List 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 listByIpAndPurpose(long ipAddressId, FirewallRule.Purpose purpose); + + List listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose); + + List listStaticNatByVmId(long vmId); + + List listByIpPurposeAndProtocolAndNotRevoked(long ipAddressId, Integer startPort, Integer endPort, String protocol, FirewallRule.Purpose purpose); + + FirewallRuleVO findByRelatedId(long ruleId); + + List listSystemRules(); + + List listByIp(long ipAddressId); + + List listByIpAndNotRevoked(long ipAddressId); + + long countRulesByIpId(long sourceIpId); + + List listByNetworkPurposeTrafficTypeAndNotRevoked(long networkId, FirewallRule.Purpose purpose, FirewallRule.TrafficType trafficType); + List listByNetworkPurposeTrafficType(long networkId, FirewallRule.Purpose purpose, FirewallRule.TrafficType trafficType); + + List 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 implements FirewallRulesDao { + + protected final SearchBuilder AllFieldsSearch; + protected final SearchBuilder NotRevokedSearch; + protected final SearchBuilder ReleaseSearch; + protected SearchBuilder VmSearch; + protected final SearchBuilder SystemRuleSearch; + protected final GenericSearchBuilder 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 listSystemRules() { + SearchCriteria 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 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 listByIpAndPurpose(long ipId, FirewallRule.Purpose purpose) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("ipId", ipId); + sc.setParameters("purpose", purpose); + + return listBy(sc); + } + + @Override + public List listByIpAndPurposeAndNotRevoked(long ipId, FirewallRule.Purpose purpose) { + SearchCriteria 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 listByNetworkAndPurposeAndNotRevoked(long networkId, FirewallRule.Purpose purpose) { + SearchCriteria 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 listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("purpose", purpose); + sc.setParameters("networkId", networkId); + + return listBy(sc); + } + + @Override + public List listByNetworkPurposeTrafficTypeAndNotRevoked(long networkId, FirewallRule.Purpose purpose, TrafficType trafficType) { + SearchCriteria 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 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 listStaticNatByVmId(long vmId) { + if (VmSearch == null) { + SearchBuilder 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 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 cidrList) { + if (cidrList == null) { + return; + } + _firewallRulesCidrsDao.persist(firewallRule.getId(), cidrList); + } + + @Override + public List listByIpPurposeAndProtocolAndNotRevoked(long ipAddressId, Integer startPort, Integer endPort, String protocol, FirewallRule.Purpose purpose) { + SearchCriteria 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 sc = AllFieldsSearch.create(); + sc.setParameters("related", ruleId); + sc.setParameters("purpose", Purpose.Firewall); + + return findOneBy(sc); + } + + @Override + public List listByIp(long ipId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("ipId", ipId); + + return listBy(sc); + } + + @Override + public List listByIpAndNotRevoked(long ipId) { + SearchCriteria sc = NotRevokedSearch.create(); + sc.setParameters("ipId", ipId); + sc.setParameters("state", State.Revoke); + + return listBy(sc); + } + + @Override + public long countRulesByIpId(long sourceIpId) { + SearchCriteria sc = RulesByIpCount.create(); + sc.setParameters("ipAddressId", sourceIpId); + return customSearch(sc, null).get(0); + } + + @Override + public List listByNetworkPurposeTrafficType(long networkId, Purpose purpose, TrafficType trafficType) { + SearchCriteria 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 listByIpAndPurposeWithState(Long ipId, Purpose purpose, State state) { + SearchCriteria 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 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 markAsUnavailable(long ipAddressId); + + void unassignIpAddress(long ipAddressId); + + List listByAccount(long accountId); + + List listByVlanId(long vlanId); + + List listByDcIdIpAddress(long dcId, String ipAddress); + + List listByDcId(long dcId); + + List listByAssociatedNetwork(long networkId, Boolean isSourceNat); + + List 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 listByPhysicalNetworkId(long physicalNetworkId); + + List 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 implements IPAddressDao { + private static final Logger s_logger = Logger.getLogger(IPAddressDaoImpl.class); + + protected SearchBuilder AllFieldsSearch; + protected SearchBuilder VlanDbIdSearchUnallocated; + protected GenericSearchBuilder AllIpCount; + protected GenericSearchBuilder AllocatedIpCount; + protected GenericSearchBuilder AllIpCountForDashboard; + protected GenericSearchBuilder AllocatedIpCountForAccount; + @Inject protected VlanDao _vlanDao; + protected GenericSearchBuilder 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 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 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 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 listByAccount(long accountId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("accountId", accountId); + return listBy(sc); + } + + @Override + public List listByVlanId(long vlanId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("vlan", vlanId); + return listBy(sc); + } + + @Override + public IPAddressVO findByIpAndSourceNetworkId(long networkId, String ipAddress) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("sourcenetwork", networkId); + sc.setParameters("ipAddress", ipAddress); + return findOneBy(sc); + } + + @Override + public IPAddressVO findByIpAndNetworkId(long networkId, String ipAddress) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); + sc.setParameters("ipAddress", ipAddress); + return findOneBy(sc); + } + + @Override + public IPAddressVO findByIpAndDcId(long dcId, String ipAddress) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dataCenterId", dcId); + sc.setParameters("ipAddress", ipAddress); + return findOneBy(sc); + } + + @Override + public List listByDcId(long dcId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dataCenterId", dcId); + return listBy(sc); + } + + @Override + public List listByDcIdIpAddress(long dcId, String ipAddress) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dataCenterId", dcId); + sc.setParameters("ipAddress", ipAddress); + return listBy(sc); + } + + @Override + public List listByAssociatedNetwork(long networkId, Boolean isSourceNat) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); + + if (isSourceNat != null) { + sc.setParameters("sourceNat", isSourceNat); + } + + return listBy(sc); + } + + @Override + public List listStaticNatPublicIps(long networkId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("network", networkId); + sc.setParameters("oneToOneNat", true); + return listBy(sc); + } + + @Override + public IPAddressVO findByAssociatedVmId(long vmId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("associatedWithVmId", vmId); + + return findOneBy(sc); + } + + @Override + public IPAddressVO findByVmIp(String vmIp) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("associatedVmIp", vmIp); + return findOneBy(sc); + } + @Override + public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) { + SearchCriteria 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 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 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 sc = AllocatedIpCountForAccount.create(); + sc.setParameters("account", accountId); + return customSearch(sc, null).get(0); + } + + @Override + public List listByPhysicalNetworkId(long physicalNetworkId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + return listBy(sc); + } + + @Override + public long countFreePublicIPs() { + SearchCriteria sc = CountFreePublicIps.create(); + sc.setParameters("state", State.Free); + sc.setJoinParameters("vlans", "vlanType", VlanType.VirtualNetwork); + return customSearch(sc, null).get(0); + } + + @Override + public List listByAssociatedVpc(long vpcId, Boolean isSourceNat) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("vpcId", vpcId); + + if (isSourceNat != null) { + sc.setParameters("sourceNat", isSourceNat); + } + + return listBy(sc); + } + + @Override + public long countFreeIPsInNetwork(long networkId) { + SearchCriteria 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 sc = AllFieldsSearch.create(); + sc.setParameters("associatedWithVmId", vmId); + sc.setParameters("associatedVmIp", vmIp); + return findOneBy(sc); + } +}