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 056EE112AD for ; Mon, 13 May 2013 18:01:50 +0000 (UTC) Received: (qmail 29882 invoked by uid 500); 13 May 2013 12:25:45 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 28418 invoked by uid 500); 13 May 2013 12:25:40 -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 26574 invoked by uid 99); 13 May 2013 12:25:32 -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, 13 May 2013 12:25:32 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A2BFE88F459; Mon, 13 May 2013 12:25:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: nitin@apache.org To: commits@cloudstack.apache.org Date: Mon, 13 May 2013 12:25:46 -0000 Message-Id: <7e7a667d0c894c90b8885d596a17f845@git.apache.org> In-Reply-To: <2570cc0d573e44ba8ecf113450d2c1a8@git.apache.org> References: <2570cc0d573e44ba8ecf113450d2c1a8@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [16/57] [abbrv] [partial] merge master http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/DataCenterLinkLocalIpAddressDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterLinkLocalIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterLinkLocalIpAddressDaoImpl.java new file mode 100644 index 0000000..b52e373 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterLinkLocalIpAddressDaoImpl.java @@ -0,0 +1,188 @@ +// 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.dc.dao; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import com.cloud.dc.DataCenterLinkLocalIpAddressVO; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDao; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Func; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.net.NetUtils; + +@Component +@Local(value={DataCenterLinkLocalIpAddressDaoImpl.class}) @DB(txn=false) +public class DataCenterLinkLocalIpAddressDaoImpl extends GenericDaoBase implements DataCenterLinkLocalIpAddressDao { + private static final Logger s_logger = Logger.getLogger(DataCenterLinkLocalIpAddressDaoImpl.class); + + private final SearchBuilder AllFieldsSearch; + private final GenericSearchBuilder AllIpCount; + private final GenericSearchBuilder AllAllocatedIpCount; + + @DB + public DataCenterLinkLocalIpAddressVO takeIpAddress(long dcId, long podId, long instanceId, String reservationId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("pod", podId); + sc.setParameters("taken", (Date)null); + + Transaction txn = Transaction.currentTxn(); + txn.start(); + + DataCenterLinkLocalIpAddressVO vo = lockOneRandomRow(sc, true); + if (vo == null) { + return null; + } + + vo.setTakenAt(new Date()); + vo.setInstanceId(instanceId); + vo.setReservationId(reservationId); + update(vo.getId(), vo); + txn.commit(); + return vo; + } + + public boolean deleteIpAddressByPod(long podId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("pod", podId); + return remove(sc) > 0; + } + + @DB + public void addIpRange(long dcId, long podId, String start, String end) { + String insertSql = "INSERT INTO `cloud`.`op_dc_link_local_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)"; + PreparedStatement stmt = null; + + long startIP = NetUtils.ip2Long(start); + long endIP = NetUtils.ip2Long(end); + + Transaction txn = Transaction.currentTxn(); + try { + txn.start(); + stmt = txn.prepareAutoCloseStatement(insertSql); + while (startIP <= endIP) { + stmt.setString(1, NetUtils.long2Ip(startIP++)); + stmt.setLong(2, dcId); + stmt.setLong(3, podId); + stmt.addBatch(); + } + stmt.executeBatch(); + txn.commit(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to insert", e); + } + } + + public void releaseIpAddress(String ipAddress, long dcId, long instanceId) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Releasing ip address: " + ipAddress + " data center " + dcId); + } + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("ip", ipAddress); + sc.setParameters("dc", dcId); + sc.setParameters("instance", instanceId); + + DataCenterLinkLocalIpAddressVO vo = createForUpdate(); + + vo.setTakenAt(null); + vo.setInstanceId(null); + vo.setReservationId(null); + update(vo, sc); + } + + public void releaseIpAddress(long nicId, String reservationId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("instance", nicId); + sc.setParameters("reservation", reservationId); + + DataCenterLinkLocalIpAddressVO vo = createForUpdate(); + + vo.setTakenAt(null); + vo.setInstanceId(null); + vo.setReservationId(null); + update(vo, sc); + } + + public List listByPodIdDcId(long podId, long dcId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("pod", podId); + return listBy(sc); + } + + public int countIPs(long podId, long dcId, boolean onlyCountAllocated) { + SearchCriteria sc; + if (onlyCountAllocated) { + sc = AllAllocatedIpCount.create(); + } else { + sc = AllIpCount.create(); + } + + sc.setParameters("pod", podId); + List count = customSearch(sc, null); + return count.get(0); + } + + public DataCenterLinkLocalIpAddressDaoImpl() { + super(); + AllFieldsSearch = createSearchBuilder(); + AllFieldsSearch.and("ip", AllFieldsSearch.entity().getIpAddress(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("reservation", AllFieldsSearch.entity().getReservationId(), SearchCriteria.Op.EQ); + AllFieldsSearch.and("taken", AllFieldsSearch.entity().getTakenAt(), SearchCriteria.Op.EQ); + AllFieldsSearch.done(); + + AllIpCount = createSearchBuilder(Integer.class); + AllIpCount.select(null, Func.COUNT, AllIpCount.entity().getId()); + AllIpCount.and("pod", AllIpCount.entity().getPodId(), SearchCriteria.Op.EQ); + AllIpCount.done(); + + AllAllocatedIpCount = createSearchBuilder(Integer.class); + AllAllocatedIpCount.select(null, Func.COUNT, AllAllocatedIpCount.entity().getId()); + AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ); + AllAllocatedIpCount.and("removed", AllAllocatedIpCount.entity().getTakenAt(), SearchCriteria.Op.NNULL); + AllAllocatedIpCount.done(); + + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + super.configure(name, params); + + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("ip", NetUtils.getLinkLocalGateway()); + remove(sc); + + return true; + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDao.java b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDao.java new file mode 100644 index 0000000..778498d --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDao.java @@ -0,0 +1,49 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.DataCenterVnetVO; +import com.cloud.utils.db.GenericDao; +import com.cloud.utils.db.Transaction; + +public interface DataCenterVnetDao extends GenericDao { + public List listAllocatedVnets(long physicalNetworkId); + public List listAllocatedVnetsInRange(long dcId, long physicalNetworkId, Integer start, Integer end); + public List findVnet(long dcId, String vnet); + public int countZoneVlans(long dcId, boolean onlyCountAllocated); + public List findVnet(long dcId, long physicalNetworkId, String vnet); + + public void add(long dcId, long physicalNetworkId, int start, int end); + + public void delete(long physicalNetworkId); + + public void deleteRange(Transaction txn, long dcId, long physicalNetworkId, int start, int end); + + public void lockRange(long dcId, long physicalNetworkId, Integer start, Integer end); + + public DataCenterVnetVO take(long physicalNetworkId, long accountId, String reservationId, List vlanDbIds); + + public void release(String vnet, long physicalNetworkId, long accountId, String reservationId); + + public void releaseDedicatedGuestVlans(Long dedicatedGuestVlanRangeId); + + public int countVnetsAllocatedToAccount(long dcId, long accountId); + + public int countVnetsDedicatedToAccount(long dcId, long accountId); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java new file mode 100755 index 0000000..e97f2c6 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java @@ -0,0 +1,318 @@ +// 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.dc.dao; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; +import javax.naming.ConfigurationException; + +import com.cloud.exception.InvalidParameterValueException; +import org.springframework.stereotype.Component; + +import com.cloud.dc.DataCenterVnetVO; +import com.cloud.network.dao.AccountGuestVlanMapDao; +import com.cloud.network.dao.AccountGuestVlanMapVO; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDao; +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.exception.CloudRuntimeException; + +/** + * DataCenterVnetDaoImpl maintains the one-to-many relationship between + * data center/physical_network and the vnet that appears within the physical network. + */ +@Component +@DB(txn=false) +public class DataCenterVnetDaoImpl extends GenericDaoBase implements DataCenterVnetDao { + + private final SearchBuilder FreeVnetSearch; + private final SearchBuilder FreeDedicatedVnetSearch; + private final SearchBuilder VnetDcSearch; + private final SearchBuilder VnetDcSearchAllocated; + private final SearchBuilder DcSearchAllocated; + private final SearchBuilder DcSearchAllocatedInRange; + private final GenericSearchBuilder countZoneVlans; + private final GenericSearchBuilder countAllocatedZoneVlans; + private final SearchBuilder SearchRange; + private final SearchBuilder DedicatedGuestVlanRangeSearch; + private final GenericSearchBuilder countVnetsAllocatedToAccount; + protected GenericSearchBuilder countVnetsDedicatedToAccount; + protected SearchBuilder AccountGuestVlanMapSearch; + + @Inject protected AccountGuestVlanMapDao _accountGuestVlanMapDao; + + public List listAllocatedVnets(long physicalNetworkId) { + SearchCriteria sc = DcSearchAllocated.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + return listBy(sc); + } + + public List listAllocatedVnetsInRange(long dcId, long physicalNetworkId, Integer start, Integer end) { + SearchCriteria sc = DcSearchAllocatedInRange.create(); + sc.setParameters("dc",dcId); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("vnetRange", start.toString(), end.toString()); + return listBy(sc); + } + + public void lockRange(long dcId, long physicalNetworkId, Integer start, Integer end) { + SearchCriteria sc = SearchRange.create(); + sc.setParameters("dc",dcId); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("vnetRange", start.toString(), end.toString()); + lockRows(sc,null,true); + } + + public List findVnet(long dcId, String vnet) { + SearchCriteria sc = VnetDcSearch.create();; + sc.setParameters("dc", dcId); + sc.setParameters("vnet", vnet); + return listBy(sc); + } + + public int countZoneVlans(long dcId, boolean onlyCountAllocated){ + SearchCriteria sc = onlyCountAllocated ? countAllocatedZoneVlans.create() : countZoneVlans.create(); + sc.setParameters("dc", dcId); + return customSearch(sc, null).get(0); + } + + public List findVnet(long dcId, long physicalNetworkId, String vnet) { + SearchCriteria sc = VnetDcSearch.create(); + sc.setParameters("dc", dcId); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("vnet", vnet); + + return listBy(sc); + } + + @DB + public void add(long dcId, long physicalNetworkId, int start, int end) { + String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)"; + + Transaction txn = Transaction.currentTxn(); + try { + txn.start(); + PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet); + for (int i = start; i <= end; i++) { + stmt.setString(1, String.valueOf(i)); + stmt.setLong(2, dcId); + stmt.setLong(3, physicalNetworkId); + stmt.addBatch(); + } + stmt.executeBatch(); + txn.commit(); + } catch (SQLException e) { + if (!e.getMessage().contains("Duplicate")){ + txn.rollback(); + txn.close(); + throw new CloudRuntimeException("Exception caught adding vnet ", e); + } + } + } + + public void deleteRange(Transaction txn, long dcId, long physicalNetworkId, int start, int end) { + String deleteVnet = "DELETE FROM `cloud`.`op_dc_vnet_alloc` WHERE data_center_id=? AND physical_network_id=? AND taken IS NULL AND vnet BETWEEN ? AND ?"; + try { + PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet); + stmt.setLong(1,dcId); + stmt.setLong(2,physicalNetworkId); + stmt.setString(3,((Integer)start).toString()); + stmt.setString(4,((Integer)end).toString()); + stmt.execute(); + } catch (SQLException e) { + throw new CloudRuntimeException("Exception caught adding vnet ", e); + } + } + public void delete(long physicalNetworkId) { + SearchCriteria sc = VnetDcSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + remove(sc); + } + + @DB + public DataCenterVnetVO take(long physicalNetworkId, long accountId, String reservationId, List vlanDbIds) { + SearchCriteria sc; + if (vlanDbIds != null) { + sc = FreeDedicatedVnetSearch.create(); + sc.setParameters("accountGuestVlanMapId", vlanDbIds.toArray()); + } else { + sc = FreeVnetSearch.create(); + } + sc.setParameters("physicalNetworkId", physicalNetworkId); + Date now = new Date(); + Transaction txn = Transaction.currentTxn(); + txn.start(); + DataCenterVnetVO vo = lockOneRandomRow(sc, true); + if (vo == null) { + return null; + } + + vo.setTakenAt(now); + vo.setAccountId(accountId); + vo.setReservationId(reservationId); + update(vo.getId(), vo); + txn.commit(); + return vo; + } + + + public void release(String vnet, long physicalNetworkId, long accountId, String reservationId) { + SearchCriteria sc = VnetDcSearchAllocated.create(); + sc.setParameters("vnet", vnet); + sc.setParameters("physicalNetworkId", physicalNetworkId); + sc.setParameters("account", accountId); + sc.setParameters("reservation", reservationId); + + DataCenterVnetVO vo = findOneIncludingRemovedBy(sc); + if (vo == null) { + return; + } + + vo.setTakenAt(null); + vo.setAccountId(null); + vo.setReservationId(null); + update(vo.getId(), vo); + } + + @Override + public void releaseDedicatedGuestVlans(Long dedicatedGuestVlanRangeId) { + SearchCriteria sc = DedicatedGuestVlanRangeSearch.create(); + sc.setParameters("dedicatedGuestVlanRangeId", dedicatedGuestVlanRangeId); + List vnets = listBy(sc); + for(DataCenterVnetVO vnet : vnets) { + vnet.setAccountGuestVlanMapId(null); + update(vnet.getId(), vnet); + } + } + + @Override + public int countVnetsAllocatedToAccount(long dcId, long accountId) { + SearchCriteria sc = countVnetsAllocatedToAccount.create(); + sc.setParameters("dc", dcId); + sc.setParameters("accountId", accountId); + return customSearch(sc, null).get(0); + } + + @Override + public int countVnetsDedicatedToAccount(long dcId, long accountId) { + SearchCriteria sc = countVnetsDedicatedToAccount.create(); + sc.setParameters("dc", dcId); + sc.setParameters("accountId", accountId); + return customSearch(sc, null).get(0); + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + boolean result = super.configure(name, params); + + countVnetsDedicatedToAccount = createSearchBuilder(Integer.class); + countVnetsDedicatedToAccount.and("dc", countVnetsDedicatedToAccount.entity().getDataCenterId(), SearchCriteria.Op.EQ); + countVnetsDedicatedToAccount.and("accountGuestVlanMapId", countVnetsDedicatedToAccount.entity().getAccountGuestVlanMapId(), Op.NNULL); + AccountGuestVlanMapSearch = _accountGuestVlanMapDao.createSearchBuilder(); + AccountGuestVlanMapSearch.and("accountId", AccountGuestVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + countVnetsDedicatedToAccount.join("AccountGuestVlanMapSearch", AccountGuestVlanMapSearch, countVnetsDedicatedToAccount.entity().getAccountGuestVlanMapId(), + AccountGuestVlanMapSearch.entity().getId(), JoinBuilder.JoinType.INNER); + countVnetsDedicatedToAccount.select(null, Func.COUNT, countVnetsDedicatedToAccount.entity().getId()); + countVnetsDedicatedToAccount.done(); + AccountGuestVlanMapSearch.done(); + + return result; + } + + public DataCenterVnetDaoImpl() { + super(); + DcSearchAllocated = createSearchBuilder(); + DcSearchAllocated.and("dc", DcSearchAllocated.entity().getDataCenterId(), SearchCriteria.Op.EQ); + DcSearchAllocated.and("physicalNetworkId", DcSearchAllocated.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + DcSearchAllocated.and("allocated", DcSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL); + DcSearchAllocated.done(); + + DcSearchAllocatedInRange = createSearchBuilder(); + DcSearchAllocatedInRange.and("dc",DcSearchAllocatedInRange.entity().getDataCenterId(), Op.EQ); + DcSearchAllocatedInRange.and("physicalNetworkId", DcSearchAllocatedInRange.entity().getPhysicalNetworkId(), Op.EQ); + DcSearchAllocatedInRange.and("allocated", DcSearchAllocatedInRange.entity().getTakenAt(), Op.NNULL); + DcSearchAllocatedInRange.and("vnetRange", DcSearchAllocatedInRange.entity().getVnet(), Op.BETWEEN); + DcSearchAllocatedInRange.done(); + + SearchRange = createSearchBuilder(); + SearchRange.and("dc", SearchRange.entity().getDataCenterId(), Op.EQ); + SearchRange.and("physicalNetworkId", SearchRange.entity().getPhysicalNetworkId(), Op.EQ); + SearchRange.and("vnetRange", SearchRange.entity().getVnet(), Op.BETWEEN); + + FreeVnetSearch = createSearchBuilder(); + FreeVnetSearch.and("dc", FreeVnetSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + FreeVnetSearch.and("physicalNetworkId", FreeVnetSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + FreeVnetSearch.and("taken", FreeVnetSearch.entity().getTakenAt(), SearchCriteria.Op.NULL); + FreeVnetSearch.and("accountGuestVlanMapId", FreeVnetSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.NULL); + FreeVnetSearch.done(); + + FreeDedicatedVnetSearch = createSearchBuilder(); + FreeDedicatedVnetSearch.and("dc", FreeDedicatedVnetSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + FreeDedicatedVnetSearch.and("physicalNetworkId", FreeDedicatedVnetSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + FreeDedicatedVnetSearch.and("taken", FreeDedicatedVnetSearch.entity().getTakenAt(), SearchCriteria.Op.NULL); + FreeDedicatedVnetSearch.and("accountGuestVlanMapId", FreeDedicatedVnetSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.IN); + FreeDedicatedVnetSearch.done(); + + VnetDcSearch = createSearchBuilder(); + VnetDcSearch.and("vnet", VnetDcSearch.entity().getVnet(), SearchCriteria.Op.EQ); + VnetDcSearch.and("dc", VnetDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + VnetDcSearch.and("physicalNetworkId", VnetDcSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + VnetDcSearch.done(); + + countZoneVlans = createSearchBuilder(Integer.class); + countZoneVlans.select(null, Func.COUNT, countZoneVlans.entity().getId()); + countZoneVlans.and("dc", countZoneVlans.entity().getDataCenterId(), Op.EQ); + countZoneVlans.done(); + + countAllocatedZoneVlans = createSearchBuilder(Integer.class); + countAllocatedZoneVlans.select(null, Func.COUNT, countAllocatedZoneVlans.entity().getId()); + countAllocatedZoneVlans.and("dc", countAllocatedZoneVlans.entity().getDataCenterId(), Op.EQ); + countAllocatedZoneVlans.and("allocated", countAllocatedZoneVlans.entity().getTakenAt(), SearchCriteria.Op.NNULL); + countAllocatedZoneVlans.done(); + + VnetDcSearchAllocated = createSearchBuilder(); + VnetDcSearchAllocated.and("vnet", VnetDcSearchAllocated.entity().getVnet(), SearchCriteria.Op.EQ); + VnetDcSearchAllocated.and("dc", VnetDcSearchAllocated.entity().getDataCenterId(), SearchCriteria.Op.EQ); + VnetDcSearchAllocated.and("physicalNetworkId", VnetDcSearchAllocated.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + VnetDcSearchAllocated.and("taken", VnetDcSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL); + VnetDcSearchAllocated.and("account", VnetDcSearchAllocated.entity().getAccountId(), SearchCriteria.Op.EQ); + VnetDcSearchAllocated.and("reservation", VnetDcSearchAllocated.entity().getReservationId(), SearchCriteria.Op.EQ); + VnetDcSearchAllocated.done(); + + DedicatedGuestVlanRangeSearch = createSearchBuilder(); + DedicatedGuestVlanRangeSearch.and("dedicatedGuestVlanRangeId", DedicatedGuestVlanRangeSearch.entity().getAccountGuestVlanMapId(), SearchCriteria.Op.EQ); + DedicatedGuestVlanRangeSearch.done(); + + countVnetsAllocatedToAccount = createSearchBuilder(Integer.class); + countVnetsAllocatedToAccount.and("dc", countVnetsAllocatedToAccount.entity().getDataCenterId(), SearchCriteria.Op.EQ); + countVnetsAllocatedToAccount.and("accountId", countVnetsAllocatedToAccount.entity().getAccountId(), SearchCriteria.Op.EQ); + countVnetsAllocatedToAccount.select(null, Func.COUNT, countVnetsAllocatedToAccount.entity().getId()); + countVnetsAllocatedToAccount.done(); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java b/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java new file mode 100644 index 0000000..a3b72a8 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/DcDetailsDao.java @@ -0,0 +1,32 @@ +// 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.dc.dao; + +import java.util.Map; + +import com.cloud.dc.DcDetailVO; +import com.cloud.utils.db.GenericDao; + +public interface DcDetailsDao extends GenericDao { + Map findDetails(long dcId); + + void persist(long dcId, Map details); + + DcDetailVO findDetail(long dcId, String name); + + void deleteDetails(long dcId); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java new file mode 100644 index 0000000..278fdb4 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/DcDetailsDaoImpl.java @@ -0,0 +1,97 @@ +// 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.dc.dao; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.DcDetailVO; +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=DcDetailsDao.class) +public class DcDetailsDaoImpl extends GenericDaoBase implements DcDetailsDao { + protected final SearchBuilder DcSearch; + protected final SearchBuilder DetailSearch; + + public DcDetailsDaoImpl() { + DcSearch = createSearchBuilder(); + DcSearch.and("dcId", DcSearch.entity().getDcId(), SearchCriteria.Op.EQ); + DcSearch.done(); + + DetailSearch = createSearchBuilder(); + DetailSearch.and("dcId", DetailSearch.entity().getDcId(), SearchCriteria.Op.EQ); + DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ); + DetailSearch.done(); + } + + @Override + public DcDetailVO findDetail(long dcId, String name) { + SearchCriteria sc = DetailSearch.create(); + sc.setParameters("dcId", dcId); + sc.setParameters("name", name); + + return findOneIncludingRemovedBy(sc); + } + + @Override + public Map findDetails(long dcId) { + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + + List results = search(sc, null); + Map details = new HashMap(results.size()); + for (DcDetailVO result : results) { + details.put(result.getName(), result.getValue()); + } + return details; + } + + @Override + public void deleteDetails(long dcId) { + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + + List results = search(sc, null); + for (DcDetailVO result : results) { + remove(result.getId()); + } + } + + @Override + public void persist(long dcId, Map details) { + Transaction txn = Transaction.currentTxn(); + txn.start(); + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + expunge(sc); + + for (Map.Entry detail : details.entrySet()) { + DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue()); + persist(vo); + } + txn.commit(); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/HostPodDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDao.java b/engine/schema/src/com/cloud/dc/dao/HostPodDao.java new file mode 100644 index 0000000..03f7155 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/HostPodDao.java @@ -0,0 +1,35 @@ +// 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.dc.dao; + +import java.util.HashMap; +import java.util.List; + +import com.cloud.dc.HostPodVO; +import com.cloud.utils.db.GenericDao; +import com.cloud.vm.VirtualMachine; + +public interface HostPodDao extends GenericDao { + public List listByDataCenterId(long id); + + public HostPodVO findByName(String name, long dcId); + + public HashMap> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip); + + public List listDisabledPods(long zoneId); + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java new file mode 100644 index 0000000..07b4ad1 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java @@ -0,0 +1,134 @@ +// 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.dc.dao; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import com.cloud.dc.HostPodVO; +import com.cloud.org.Grouping; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; +import com.cloud.utils.db.Transaction; + +@Component +@Local(value={HostPodDao.class}) +public class HostPodDaoImpl extends GenericDaoBase implements HostPodDao { + private static final Logger s_logger = Logger.getLogger(HostPodDaoImpl.class); + + protected SearchBuilder DataCenterAndNameSearch; + protected SearchBuilder DataCenterIdSearch; + + public HostPodDaoImpl() { + DataCenterAndNameSearch = createSearchBuilder(); + DataCenterAndNameSearch.and("dc", DataCenterAndNameSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + DataCenterAndNameSearch.and("name", DataCenterAndNameSearch.entity().getName(), SearchCriteria.Op.EQ); + DataCenterAndNameSearch.done(); + + DataCenterIdSearch = createSearchBuilder(); + DataCenterIdSearch.and("dcId", DataCenterIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + DataCenterIdSearch.done(); + } + + @Override + public List listByDataCenterId(long id) { + SearchCriteria sc = DataCenterIdSearch.create(); + sc.setParameters("dcId", id); + + return listBy(sc); + } + + @Override + public HostPodVO findByName(String name, long dcId) { + SearchCriteria sc = DataCenterAndNameSearch.create(); + sc.setParameters("dc", dcId); + sc.setParameters("name", name); + + return findOneBy(sc); + } + + @Override + public HashMap> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip) { + HashMap> currentPodCidrSubnets = new HashMap>(); + + String selectSql = "SELECT id, cidr_address, cidr_size FROM host_pod_ref WHERE data_center_id=" + zoneId +" and removed IS NULL"; + Transaction txn = Transaction.currentTxn(); + try { + PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + Long podId = rs.getLong("id"); + if (podId.longValue() == podIdToSkip) { + continue; + } + String cidrAddress = rs.getString("cidr_address"); + long cidrSize = rs.getLong("cidr_size"); + List cidrPair = new ArrayList(); + cidrPair.add(0, cidrAddress); + cidrPair.add(1, new Long(cidrSize)); + currentPodCidrSubnets.put(podId, cidrPair); + } + } catch (SQLException ex) { + s_logger.warn("DB exception " + ex.getMessage(), ex); + return null; + } + + return currentPodCidrSubnets; + } + + @Override + public boolean remove(Long id) { + Transaction txn = Transaction.currentTxn(); + txn.start(); + HostPodVO pod = createForUpdate(); + pod.setName(null); + + update(id, pod); + + boolean result = super.remove(id); + txn.commit(); + return result; + } + + @Override + public List listDisabledPods(long zoneId) { + GenericSearchBuilder podIdSearch = createSearchBuilder(Long.class); + podIdSearch.selectField(podIdSearch.entity().getId()); + podIdSearch.and("dataCenterId", podIdSearch.entity().getDataCenterId(), Op.EQ); + podIdSearch.and("allocationState", podIdSearch.entity().getAllocationState(), Op.EQ); + podIdSearch.done(); + + + SearchCriteria sc = podIdSearch.create(); + sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId); + sc.addAnd("allocationState", SearchCriteria.Op.EQ, Grouping.AllocationState.Disabled); + return customSearch(sc, null); + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java b/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java new file mode 100644 index 0000000..6359dfe --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/PodVlanDao.java @@ -0,0 +1,30 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.PodVlanVO; +import com.cloud.utils.db.GenericDao; + +public interface PodVlanDao extends GenericDao { + public List listAllocatedVnets(long podId); + public void add(long podId, int start, int end); + public void delete(long podId); + public PodVlanVO take(long podId, long accountId); + public void release(String vlan, long podId, long accountId); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java new file mode 100755 index 0000000..413f9ed --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java @@ -0,0 +1,139 @@ +// 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.dc.dao; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.PodVlanVO; +import com.cloud.utils.db.GenericDao; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; + +/** + * PodVlanDaoImpl maintains the one-to-many relationship between + */ +@Component +public class PodVlanDaoImpl extends GenericDaoBase implements PodVlanDao { + private final SearchBuilder FreeVlanSearch; + private final SearchBuilder VlanPodSearch; + private final SearchBuilder PodSearchAllocated; + + public List listAllocatedVnets(long podId) { + SearchCriteria sc = PodSearchAllocated.create(); + sc.setParameters("podId", podId); + return listBy(sc); + } + + public void add(long podId, int start, int end) { + String insertVnet = "INSERT INTO `cloud`.`op_pod_vlan_alloc` (vlan, pod_id) VALUES ( ?, ?)"; + + Transaction txn = Transaction.currentTxn(); + try { + txn.start(); + PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet); + for (int i = start; i < end; i++) { + stmt.setString(1, String.valueOf(i)); + stmt.setLong(2, podId); + stmt.addBatch(); + } + stmt.executeBatch(); + txn.commit(); + } catch (SQLException e) { + throw new CloudRuntimeException("Exception caught adding vnet ", e); + } + } + + public void delete(long podId) { + String deleteVnet = "DELETE FROM `cloud`.`op_pod_vlan_alloc` WHERE pod_id = ?"; + + Transaction txn = Transaction.currentTxn(); + try { + PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet); + stmt.setLong(1, podId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new CloudRuntimeException("Exception caught deleting vnet ", e); + } + } + + public PodVlanVO take(long podId, long accountId) { + SearchCriteria sc = FreeVlanSearch.create(); + sc.setParameters("podId", podId); + Date now = new Date(); + Transaction txn = Transaction.currentTxn(); + try { + txn.start(); + PodVlanVO vo = lockOneRandomRow(sc, true); + if (vo == null) { + return null; + } + + vo.setTakenAt(now); + vo.setAccountId(accountId); + update(vo.getId(), vo); + txn.commit(); + return vo; + + } catch (Exception e) { + throw new CloudRuntimeException("Caught Exception ", e); + } + } + + public void release(String vlan, long podId, long accountId) { + SearchCriteria sc = VlanPodSearch.create(); + sc.setParameters("vlan", vlan); + sc.setParameters("podId", podId); + sc.setParameters("account", accountId); + + PodVlanVO vo = findOneIncludingRemovedBy(sc); + if (vo == null) { + return; + } + + vo.setTakenAt(null); + vo.setAccountId(null); + update(vo.getId(), vo); + } + + public PodVlanDaoImpl() { + super(); + PodSearchAllocated = createSearchBuilder(); + PodSearchAllocated.and("podId", PodSearchAllocated.entity().getPodId(), SearchCriteria.Op.EQ); + PodSearchAllocated.and("allocated", PodSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL); + PodSearchAllocated.done(); + + FreeVlanSearch = createSearchBuilder(); + FreeVlanSearch.and("podId", FreeVlanSearch.entity().getPodId(), SearchCriteria.Op.EQ); + FreeVlanSearch.and("taken", FreeVlanSearch.entity().getTakenAt(), SearchCriteria.Op.NULL); + FreeVlanSearch.done(); + + VlanPodSearch = createSearchBuilder(); + VlanPodSearch.and("vlan", VlanPodSearch.entity().getVlan(), SearchCriteria.Op.EQ); + VlanPodSearch.and("podId", VlanPodSearch.entity().getPodId(), SearchCriteria.Op.EQ); + VlanPodSearch.and("taken", VlanPodSearch.entity().getTakenAt(), SearchCriteria.Op.NNULL); + VlanPodSearch.and("account", VlanPodSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + VlanPodSearch.done(); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java new file mode 100644 index 0000000..841ed1b --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDao.java @@ -0,0 +1,30 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.PodVlanMapVO; +import com.cloud.utils.db.GenericDao; + +public interface PodVlanMapDao extends GenericDao { + + public List listPodVlanMapsByPod(long podId); + public PodVlanMapVO listPodVlanMapsByVlan(long vlanDbId); + public PodVlanMapVO findPodVlanMap(long podId, long vlanDbId); + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java new file mode 100644 index 0000000..df7a5d9 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/PodVlanMapDaoImpl.java @@ -0,0 +1,75 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.dc.dao; + +import java.util.List; + +import javax.ejb.Local; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.PodVlanMapVO; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; + +@Component +@Local(value={PodVlanMapDao.class}) +public class PodVlanMapDaoImpl extends GenericDaoBase implements PodVlanMapDao { + + protected SearchBuilder PodSearch; + protected SearchBuilder VlanSearch; + protected SearchBuilder PodVlanSearch; + + @Override + public List listPodVlanMapsByPod(long podId) { + SearchCriteria sc = PodSearch.create(); + sc.setParameters("podId", podId); + return listIncludingRemovedBy(sc); + } + + @Override + public PodVlanMapVO listPodVlanMapsByVlan(long vlanDbId) { + SearchCriteria sc = VlanSearch.create(); + sc.setParameters("vlanDbId", vlanDbId); + return findOneBy(sc); + } + + @Override + public PodVlanMapVO findPodVlanMap(long podId, long vlanDbId) { + SearchCriteria sc = PodVlanSearch.create(); + sc.setParameters("podId", podId); + sc.setParameters("vlanDbId", vlanDbId); + return findOneIncludingRemovedBy(sc); + } + + public PodVlanMapDaoImpl() { + PodSearch = createSearchBuilder(); + PodSearch.and("podId", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ); + PodSearch.done(); + + VlanSearch = createSearchBuilder(); + VlanSearch.and("vlanDbId", VlanSearch.entity().getVlanDbId(), SearchCriteria.Op.EQ); + VlanSearch.done(); + + PodVlanSearch = createSearchBuilder(); + PodVlanSearch.and("podId", PodVlanSearch.entity().getPodId(), SearchCriteria.Op.EQ); + PodVlanSearch.and("vlanDbId", PodVlanSearch.entity().getVlanDbId(), SearchCriteria.Op.EQ); + PodVlanSearch.done(); + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java new file mode 100755 index 0000000..f1fd2bd --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDao.java @@ -0,0 +1,32 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.StorageNetworkIpAddressVO; +import com.cloud.utils.db.GenericDao; + +public interface StorageNetworkIpAddressDao extends GenericDao { + long countInUseIpByRangeId(long rangeId); + + List listInUseIpByRangeId(long rangeId); + + StorageNetworkIpAddressVO takeIpAddress(long rangeId); + + void releaseIpAddress(String ip); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java new file mode 100755 index 0000000..782ee0d --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java @@ -0,0 +1,114 @@ +// 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.dc.dao; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.DataCenterIpAddressVO; +import com.cloud.dc.StorageNetworkIpAddressVO; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria2; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.SearchCriteria.Func; +import com.cloud.utils.db.SearchCriteria.Op; + +@Component +@Local(value={StorageNetworkIpAddressDao.class}) +@DB(txn=false) +public class StorageNetworkIpAddressDaoImpl extends GenericDaoBase implements StorageNetworkIpAddressDao { + protected final GenericSearchBuilder countInUserIp; + protected final GenericSearchBuilder listInUseIp; + protected final SearchBuilder untakenIp; + protected final SearchBuilder ipSearch; + + + protected StorageNetworkIpAddressDaoImpl() { + countInUserIp = createSearchBuilder(Long.class); + countInUserIp.select(null, Func.COUNT, null); + countInUserIp.and("rangeId", countInUserIp.entity().getRangeId(), Op.EQ); + countInUserIp.and("taken", countInUserIp.entity().getTakenAt(), Op.NNULL); + countInUserIp.done(); + + listInUseIp = createSearchBuilder(String.class); + listInUseIp.selectField(listInUseIp.entity().getIpAddress()); + listInUseIp.and("rangeId", listInUseIp.entity().getRangeId(), Op.EQ); + listInUseIp.and("taken", listInUseIp.entity().getTakenAt(), Op.NNULL); + listInUseIp.done(); + + untakenIp = createSearchBuilder(); + untakenIp.and("rangeId", untakenIp.entity().getRangeId(), Op.EQ); + untakenIp.and("taken", untakenIp.entity().getTakenAt(), Op.NULL); + untakenIp.done(); + + ipSearch = createSearchBuilder(); + ipSearch.and("ipAddress", ipSearch.entity().getIpAddress(), Op.EQ); + ipSearch.done(); + } + + @Override + public long countInUseIpByRangeId(long rangeId) { + SearchCriteria sc = countInUserIp.create(); + sc.setParameters("rangeId", rangeId); + return customSearch(sc, null).get(0); + } + + @Override + public List listInUseIpByRangeId(long rangeId) { + SearchCriteria sc = listInUseIp.create(); + sc.setParameters("rangeId", rangeId); + return customSearch(sc, null); + } + + @Override + @DB + public StorageNetworkIpAddressVO takeIpAddress(long rangeId) { + SearchCriteria sc = untakenIp.create(); + sc.setParameters("rangeId", rangeId); + Transaction txn = Transaction.currentTxn(); + txn.start(); + StorageNetworkIpAddressVO ip = lockOneRandomRow(sc, true); + if (ip == null) { + txn.rollback(); + return null; + } + ip.setTakenAt(new Date()); + update(ip.getId(), ip); + txn.commit(); + return ip; + } + + @Override + public void releaseIpAddress(String ip) { + SearchCriteria sc = ipSearch.create(); + sc.setParameters("ipAddress", ip); + StorageNetworkIpAddressVO vo = createForUpdate(); + vo.setTakenAt(null); + update(vo, sc); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java new file mode 100755 index 0000000..2edd568 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDao.java @@ -0,0 +1,32 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.StorageNetworkIpRangeVO; +import com.cloud.utils.db.GenericDao; + +public interface StorageNetworkIpRangeDao extends GenericDao { + List listByRangeId(long rangeId); + + List listByPodId(long podId); + + List listByDataCenterId(long dcId); + + long countRanges(); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java new file mode 100755 index 0000000..d732e6f --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpRangeDaoImpl.java @@ -0,0 +1,78 @@ +// 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.dc.dao; + +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.naming.ConfigurationException; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.StorageNetworkIpRangeVO; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria2; +import com.cloud.utils.db.SearchCriteriaService; +import com.cloud.utils.db.SearchCriteria.Func; +import com.cloud.utils.db.SearchCriteria.Op; + +@Component +@Local(value={StorageNetworkIpRangeDao.class}) +@DB(txn=false) +public class StorageNetworkIpRangeDaoImpl extends GenericDaoBase implements StorageNetworkIpRangeDao { + protected final GenericSearchBuilder countRanges; + + protected StorageNetworkIpRangeDaoImpl() { + countRanges = createSearchBuilder(Long.class); + countRanges.select(null, Func.COUNT, null); + countRanges.done(); + } + + @Override + public List listByPodId(long podId) { + SearchCriteriaService sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class); + sc.addAnd(sc.getEntity().getPodId(), Op.EQ, podId); + return sc.list(); + } + + @Override + public List listByRangeId(long rangeId) { + SearchCriteriaService sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class); + sc.addAnd(sc.getEntity().getId(), Op.EQ, rangeId); + return sc.list(); + } + + @Override + public List listByDataCenterId(long dcId) { + SearchCriteriaService sc = SearchCriteria2.create(StorageNetworkIpRangeVO.class); + sc.addAnd(sc.getEntity().getDataCenterId(), Op.EQ, dcId); + return sc.list(); + } + + @Override + public long countRanges() { + SearchCriteria sc = countRanges.create(); + return customSearch(sc, null).get(0); + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/VlanDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDao.java b/engine/schema/src/com/cloud/dc/dao/VlanDao.java new file mode 100755 index 0000000..cc82632 --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/VlanDao.java @@ -0,0 +1,55 @@ +// 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.dc.dao; + +import java.util.List; + +import com.cloud.dc.Vlan; +import com.cloud.dc.Vlan.VlanType; +import com.cloud.dc.VlanVO; +import com.cloud.utils.db.GenericDao; + +public interface VlanDao extends GenericDao { + + VlanVO findByZoneAndVlanId(long zoneId, String vlanId); + + List listByZone(long zoneId); + + List listByType(Vlan.VlanType vlanType); + + List listByZoneAndType(long zoneId, Vlan.VlanType vlanType); + + List listVlansForPod(long podId); + + List listVlansForPodByType(long podId, Vlan.VlanType vlanType); + + void addToPod(long podId, long vlanDbId); + + List listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType); + + boolean zoneHasDirectAttachUntaggedVlans(long zoneId); + + List listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId); + + List searchForZoneWideVlans(long dcId, String vlanType,String vlanId); + + List listVlansByNetworkId(long networkId); + + List listVlansByPhysicalNetworkId(long physicalNetworkId); + + List listZoneWideNonDedicatedVlans(long zoneId); +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java new file mode 100755 index 0000000..100295b --- /dev/null +++ b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java @@ -0,0 +1,334 @@ +// 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.dc.dao; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; +import javax.inject.Inject; +import javax.naming.ConfigurationException; + +import org.springframework.stereotype.Component; + +import com.cloud.dc.AccountVlanMapVO; +import com.cloud.dc.PodVlanMapVO; +import com.cloud.dc.Vlan; +import com.cloud.dc.Vlan.VlanType; +import com.cloud.dc.VlanVO; +import com.cloud.network.dao.IPAddressDao; +import com.cloud.utils.Pair; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.JoinBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; + +@Component +@Local(value={VlanDao.class}) +public class VlanDaoImpl extends GenericDaoBase implements VlanDao { + + private final String FindZoneWideVlans = "SELECT * FROM vlan WHERE data_center_id=? and vlan_type=? and vlan_id!=? and id not in (select vlan_db_id from account_vlan_map)"; + + protected SearchBuilder ZoneVlanIdSearch; + protected SearchBuilder ZoneSearch; + protected SearchBuilder ZoneTypeSearch; + protected SearchBuilder ZoneTypeAllPodsSearch; + protected SearchBuilder ZoneTypePodSearch; + protected SearchBuilder ZoneVlanSearch; + protected SearchBuilder NetworkVlanSearch; + protected SearchBuilder PhysicalNetworkVlanSearch; + protected SearchBuilder ZoneWideNonDedicatedVlanSearch; + + protected SearchBuilder AccountVlanMapSearch; + + @Inject protected PodVlanMapDao _podVlanMapDao; + @Inject protected AccountVlanMapDao _accountVlanMapDao; + @Inject protected IPAddressDao _ipAddressDao; + + @Override + public VlanVO findByZoneAndVlanId(long zoneId, String vlanId) { + SearchCriteria sc = ZoneVlanIdSearch.create(); + sc.setParameters("zoneId", zoneId); + sc.setParameters("vlanId", vlanId); + return findOneBy(sc); + } + + @Override + public List listByZone(long zoneId) { + SearchCriteria sc = ZoneSearch.create(); + sc.setParameters("zoneId", zoneId); + return listBy(sc); + } + + public VlanDaoImpl() { + ZoneVlanIdSearch = createSearchBuilder(); + ZoneVlanIdSearch.and("zoneId", ZoneVlanIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneVlanIdSearch.and("vlanId", ZoneVlanIdSearch.entity().getVlanTag(), SearchCriteria.Op.EQ); + ZoneVlanIdSearch.done(); + + ZoneSearch = createSearchBuilder(); + ZoneSearch.and("zoneId", ZoneSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneSearch.done(); + + ZoneTypeSearch = createSearchBuilder(); + ZoneTypeSearch.and("zoneId", ZoneTypeSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneTypeSearch.and("vlanType", ZoneTypeSearch.entity().getVlanType(), SearchCriteria.Op.EQ); + ZoneTypeSearch.done(); + + NetworkVlanSearch = createSearchBuilder(); + NetworkVlanSearch.and("networkOfferingId", NetworkVlanSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); + NetworkVlanSearch.done(); + + PhysicalNetworkVlanSearch = createSearchBuilder(); + PhysicalNetworkVlanSearch.and("physicalNetworkId", PhysicalNetworkVlanSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ); + PhysicalNetworkVlanSearch.done(); + } + + @Override + public List listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId){ + SearchCriteria sc = ZoneVlanSearch.create(); + sc.setParameters("zoneId", zoneId); + sc.setParameters("vlanId", vlanId); + sc.setParameters("vlanType", vlanType); + return listBy(sc); + } + + @Override + public List listByZoneAndType(long zoneId, VlanType vlanType) { + SearchCriteria sc = ZoneTypeSearch.create(); + sc.setParameters("zoneId", zoneId); + sc.setParameters("vlanType", vlanType); + return listBy(sc); + } + + + @Override + public List listByType(VlanType vlanType) { + SearchCriteria sc = ZoneTypeSearch.create(); + sc.setParameters("vlanType", vlanType); + return listBy(sc); + } + + @Override + public List listVlansForPod(long podId) { + //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two + List vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId); + List result = new ArrayList(); + for (PodVlanMapVO pvmvo: vlanMaps) { + result.add(findById(pvmvo.getVlanDbId())); + } + return result; + } + + @Override + public List listVlansForPodByType(long podId, VlanType vlanType) { + //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two) + List vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId); + List result = new ArrayList(); + for (PodVlanMapVO pvmvo: vlanMaps) { + VlanVO vlan =findById(pvmvo.getVlanDbId()); + if (vlan.getVlanType() == vlanType) { + result.add(vlan); + } + } + return result; + } + + @Override + public List listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType) { + //FIXME: use a join statement to improve the performance (should be minor since we expect only one or two) + List vlanMaps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId); + List result = new ArrayList(); + for (AccountVlanMapVO acvmvo: vlanMaps) { + VlanVO vlan =findById(acvmvo.getVlanDbId()); + if (vlan.getVlanType() == vlanType && (zoneId == null || vlan.getDataCenterId() == zoneId)) { + result.add(vlan); + } + } + return result; + } + + @Override + public void addToPod(long podId, long vlanDbId) { + PodVlanMapVO pvmvo = new PodVlanMapVO(podId, vlanDbId); + _podVlanMapDao.persist(pvmvo); + + } + + @Override + public boolean configure(String name, Map params) + throws ConfigurationException { + boolean result = super.configure(name, params); + ZoneTypeAllPodsSearch = createSearchBuilder(); + ZoneTypeAllPodsSearch.and("zoneId", ZoneTypeAllPodsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneTypeAllPodsSearch.and("vlanType", ZoneTypeAllPodsSearch.entity().getVlanType(), SearchCriteria.Op.EQ); + + SearchBuilder PodVlanSearch = _podVlanMapDao.createSearchBuilder(); + PodVlanSearch.and("podId", PodVlanSearch.entity().getPodId(), SearchCriteria.Op.NNULL); + ZoneTypeAllPodsSearch.join("vlan", PodVlanSearch, PodVlanSearch.entity().getVlanDbId(), ZoneTypeAllPodsSearch.entity().getId(), JoinBuilder.JoinType.INNER); + + ZoneTypeAllPodsSearch.done(); + PodVlanSearch.done(); + + ZoneTypePodSearch = createSearchBuilder(); + ZoneTypePodSearch.and("zoneId", ZoneTypePodSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneTypePodSearch.and("vlanType", ZoneTypePodSearch.entity().getVlanType(), SearchCriteria.Op.EQ); + + SearchBuilder PodVlanSearch2 = _podVlanMapDao.createSearchBuilder(); + PodVlanSearch2.and("podId", PodVlanSearch2.entity().getPodId(), SearchCriteria.Op.EQ); + ZoneTypePodSearch.join("vlan", PodVlanSearch2, PodVlanSearch2.entity().getVlanDbId(), ZoneTypePodSearch.entity().getId(), JoinBuilder.JoinType.INNER); + PodVlanSearch2.done(); + ZoneTypePodSearch.done(); + + ZoneWideNonDedicatedVlanSearch = createSearchBuilder(); + ZoneWideNonDedicatedVlanSearch.and("zoneId", ZoneWideNonDedicatedVlanSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + AccountVlanMapSearch = _accountVlanMapDao.createSearchBuilder(); + AccountVlanMapSearch.and("accountId", AccountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.NULL); + ZoneWideNonDedicatedVlanSearch.join("AccountVlanMapSearch", AccountVlanMapSearch, ZoneWideNonDedicatedVlanSearch.entity().getId(), AccountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.LEFTOUTER); + ZoneWideNonDedicatedVlanSearch.done(); + AccountVlanMapSearch.done(); + + return result; + } + + private VlanVO findNextVlan(long zoneId, Vlan.VlanType vlanType) { + List allVlans = listByZoneAndType(zoneId, vlanType); + List emptyVlans = new ArrayList(); + List fullVlans = new ArrayList(); + + // Try to find a VLAN that is partially allocated + for (VlanVO vlan : allVlans) { + long vlanDbId = vlan.getId(); + + int countOfAllocatedIps = _ipAddressDao.countIPs(zoneId, vlanDbId, true); + int countOfAllIps = _ipAddressDao.countIPs(zoneId, vlanDbId, false); + + if ((countOfAllocatedIps > 0) && (countOfAllocatedIps < countOfAllIps)) { + return vlan; + } else if (countOfAllocatedIps == 0) { + emptyVlans.add(vlan); + } else if (countOfAllocatedIps == countOfAllIps) { + fullVlans.add(vlan); + } + } + + if (emptyVlans.isEmpty()) { + return null; + } + + // Try to find an empty VLAN with the same tag/subnet as a VLAN that is full + for (VlanVO fullVlan : fullVlans) { + for (VlanVO emptyVlan : emptyVlans) { + if (fullVlan.getVlanTag().equals(emptyVlan.getVlanTag()) && + fullVlan.getVlanGateway().equals(emptyVlan.getVlanGateway()) && + fullVlan.getVlanNetmask().equals(emptyVlan.getVlanNetmask())) { + return emptyVlan; + } + } + } + + // Return a random empty VLAN + return emptyVlans.get(0); + } + + @Override + public boolean zoneHasDirectAttachUntaggedVlans(long zoneId) { + SearchCriteria sc = ZoneTypeAllPodsSearch.create(); + sc.setParameters("zoneId", zoneId); + sc.setParameters("vlanType", VlanType.DirectAttached); + + return listIncludingRemovedBy(sc).size() > 0; + } + + + public Pair assignPodDirectAttachIpAddress(long zoneId, + long podId, long accountId, long domainId) { + SearchCriteria sc = ZoneTypePodSearch.create(); + sc.setParameters("zoneId", zoneId); + sc.setParameters("vlanType", VlanType.DirectAttached); + sc.setJoinParameters("vlan", "podId", podId); + + VlanVO vlan = findOneIncludingRemovedBy(sc); + if (vlan == null) { + return null; + } + + return null; +// String ipAddress = _ipAddressDao.assignIpAddress(accountId, domainId, vlan.getId(), false).getAddress(); +// if (ipAddress == null) { +// return null; +// } +// return new Pair(ipAddress, vlan); + + } + + @Override + @DB + public List searchForZoneWideVlans(long dcId, String vlanType, String vlanId){ + + StringBuilder sql = new StringBuilder(FindZoneWideVlans); + + Transaction txn = Transaction.currentTxn(); + PreparedStatement pstmt = null; + try { + pstmt = txn.prepareAutoCloseStatement(sql.toString()); + pstmt.setLong(1, dcId); + pstmt.setString(2, vlanType); + pstmt.setString(3, vlanId); + + ResultSet rs = pstmt.executeQuery(); + List zoneWideVlans = new ArrayList(); + + while (rs.next()) { + zoneWideVlans.add(toEntityBean(rs, false)); + } + + return zoneWideVlans; + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e); + } + } + + @Override + public List listVlansByNetworkId(long networkOfferingId) { + SearchCriteria sc = NetworkVlanSearch.create(); + sc.setParameters("networkOfferingId", networkOfferingId); + return listBy(sc); + } + + @Override + public List listVlansByPhysicalNetworkId(long physicalNetworkId) { + SearchCriteria sc = PhysicalNetworkVlanSearch.create(); + sc.setParameters("physicalNetworkId", physicalNetworkId); + return listBy(sc); + } + + @Override + public List listZoneWideNonDedicatedVlans(long zoneId) { + SearchCriteria sc = ZoneWideNonDedicatedVlanSearch.create(); + sc.setParameters("ZoneWideNonDedicatedVlanSearch", "zoneId", zoneId); + return listBy(sc); + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/domain/DomainVO.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/domain/DomainVO.java b/engine/schema/src/com/cloud/domain/DomainVO.java new file mode 100644 index 0000000..a87bedc --- /dev/null +++ b/engine/schema/src/com/cloud/domain/DomainVO.java @@ -0,0 +1,215 @@ +// 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.domain; + +import java.util.Date; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.cloudstack.api.InternalIdentity; +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.Identity; +import com.cloud.utils.db.GenericDao; + +@Entity +@Table(name="domain") +public class DomainVO implements Domain { + public static final Logger s_logger = Logger.getLogger(DomainVO.class.getName()); + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name="id") + private long id; + + @Column(name="parent") + private Long parent = null; + + @Column(name="name") + private String name = null; + + @Column(name="owner") + private long accountId; + + @Column(name="path") + private String path = null; + + @Column(name="level") + private int level; + + @Column(name=GenericDao.REMOVED_COLUMN) + private Date removed; + + @Column(name="child_count") + private int childCount = 0; + + @Column(name="next_child_seq") + private long nextChildSeq = 1L; + + @Column(name="state") + private Domain.State state; + + @Column(name="network_domain") + private String networkDomain; + + @Column(name="uuid") + private String uuid; + + public DomainVO() {} + + public DomainVO(String name, long owner, Long parentId, String networkDomain) { + this.parent = parentId; + this.name = name; + this.accountId = owner; + this.path =""; + this.level = 0; + this.state = Domain.State.Active; + this.networkDomain = networkDomain; + this.uuid = UUID.randomUUID().toString(); + } + + public DomainVO(String name, long owner, Long parentId, String networkDomain, String uuid) { + this.parent = parentId; + this.name = name; + this.accountId = owner; + this.path =""; + this.level = 0; + this.state = Domain.State.Active; + this.networkDomain = networkDomain; + this.uuid = uuid; + } + + @Override + public long getId() { + return id; + } + + @Override + public Long getParent() { + return parent; + } + + @Override + public void setParent(Long parent) { + if(parent == null) { + this.parent = DomainVO.ROOT_DOMAIN; + } else { + if(parent.longValue() <= DomainVO.ROOT_DOMAIN) + this.parent = DomainVO.ROOT_DOMAIN; + else + this.parent = parent; + } + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public long getAccountId() { + return accountId; + } + + @Override + public Date getRemoved() { + return removed; + } + + @Override + public String getPath() { + return path; + } + + @Override + public void setPath(String path) { + this.path = path; + } + + @Override + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + @Override + public int getChildCount() { + return childCount; + } + + public void setChildCount(int count) { + childCount = count; + } + + @Override + public long getNextChildSeq() { + return nextChildSeq; + } + + public void setNextChildSeq(long seq) { + nextChildSeq = seq; + } + + @Override + public Domain.State getState() { + return state; + } + + @Override + public void setState(Domain.State state) { + this.state = state; + } + + @Override + public String toString() { + return new StringBuilder("Domain:").append(id).append(path).toString(); + } + + @Override + public String getNetworkDomain() { + return networkDomain; + } + + public void setNetworkDomain(String domainSuffix) { + this.networkDomain = domainSuffix; + } + + @Override + public String getUuid() { + return this.uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + +} + http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c11dbad9/engine/schema/src/com/cloud/domain/dao/DomainDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/domain/dao/DomainDao.java b/engine/schema/src/com/cloud/domain/dao/DomainDao.java new file mode 100644 index 0000000..afeb0f4 --- /dev/null +++ b/engine/schema/src/com/cloud/domain/dao/DomainDao.java @@ -0,0 +1,34 @@ +// 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.domain.dao; + +import java.util.List; +import java.util.Set; + +import com.cloud.domain.DomainVO; +import com.cloud.utils.db.GenericDao; + +public interface DomainDao extends GenericDao { + public DomainVO create(DomainVO domain); + public DomainVO findDomainByPath(String domainPath); + public boolean isChildDomain(Long parentId, Long childId); + DomainVO findImmediateChildForParent(Long parentId); + List findImmediateChildrenForParent(Long parentId); + List findAllChildren(String path, Long parentId); + List findInactiveDomains(); + Set getDomainParentIds(long domainId); +}