mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
233 lines
9.8 KiB
Java
233 lines
9.8 KiB
Java
/**
|
|
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
|
|
*
|
|
* This software is licensed under the GNU General Public License v3 or later.
|
|
*
|
|
* It is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or any later version.
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
package com.cloud.network.dao;
|
|
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
import javax.ejb.Local;
|
|
import javax.persistence.TableGenerator;
|
|
|
|
import com.cloud.network.NetworkAccountDaoImpl;
|
|
import com.cloud.network.NetworkAccountVO;
|
|
import com.cloud.network.NetworkVO;
|
|
import com.cloud.network.Networks.BroadcastDomainType;
|
|
import com.cloud.network.Networks.Mode;
|
|
import com.cloud.network.Networks.TrafficType;
|
|
import com.cloud.offering.NetworkOffering.GuestIpType;
|
|
import com.cloud.utils.component.ComponentLocator;
|
|
import com.cloud.utils.db.DB;
|
|
import com.cloud.utils.db.GenericDaoBase;
|
|
import com.cloud.utils.db.JoinBuilder;
|
|
import com.cloud.utils.db.JoinBuilder.JoinType;
|
|
import com.cloud.utils.db.SearchBuilder;
|
|
import com.cloud.utils.db.SearchCriteria;
|
|
import com.cloud.utils.db.SearchCriteria.Op;
|
|
import com.cloud.utils.db.SequenceFetcher;
|
|
import com.cloud.utils.db.Transaction;
|
|
import com.cloud.utils.net.NetUtils;
|
|
|
|
@Local(value=NetworkDao.class) @DB(txn=false)
|
|
public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements NetworkDao {
|
|
final SearchBuilder<NetworkVO> AllFieldsSearch;
|
|
final SearchBuilder<NetworkVO> AccountSearch;
|
|
final SearchBuilder<NetworkVO> RelatedConfigSearch;
|
|
final SearchBuilder<NetworkVO> AccountNetworkSearch;
|
|
final SearchBuilder<NetworkVO> ZoneBroadcastUriSearch;
|
|
|
|
NetworkAccountDaoImpl _accountsDao = ComponentLocator.inject(NetworkAccountDaoImpl.class);
|
|
NetworkOpDaoImpl _opDao = ComponentLocator.inject(NetworkOpDaoImpl.class);
|
|
final TableGenerator _tgMacAddress;
|
|
Random _rand = new Random(System.currentTimeMillis());
|
|
long _prefix = 0x2;
|
|
|
|
protected NetworkDaoImpl() {
|
|
super();
|
|
|
|
AllFieldsSearch = createSearchBuilder();
|
|
AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
|
|
AllFieldsSearch.and("cidr", AllFieldsSearch.entity().getCidr(), Op.EQ);
|
|
AllFieldsSearch.and("broadcastType", AllFieldsSearch.entity().getBroadcastDomainType(), Op.EQ);
|
|
AllFieldsSearch.and("offering", AllFieldsSearch.entity().getNetworkOfferingId(), Op.EQ);
|
|
AllFieldsSearch.and("datacenter", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
|
|
AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
|
|
AllFieldsSearch.and("guesttype", AllFieldsSearch.entity().getGuestType(), Op.EQ);
|
|
AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
|
|
AllFieldsSearch.done();
|
|
|
|
AccountSearch = createSearchBuilder();
|
|
AccountSearch.and("offering", AccountSearch.entity().getNetworkOfferingId(), Op.EQ);
|
|
SearchBuilder<NetworkAccountVO> join = _accountsDao.createSearchBuilder();
|
|
join.and("account", join.entity().getAccountId(), Op.EQ);
|
|
AccountSearch.join("accounts", join, AccountSearch.entity().getId(), join.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
|
|
AccountSearch.and("datacenter", AccountSearch.entity().getDataCenterId(), Op.EQ);
|
|
AccountSearch.done();
|
|
|
|
RelatedConfigSearch = createSearchBuilder();
|
|
RelatedConfigSearch.and("offering", RelatedConfigSearch.entity().getNetworkOfferingId(), Op.EQ);
|
|
RelatedConfigSearch.and("datacenter", RelatedConfigSearch.entity().getDataCenterId(), Op.EQ);
|
|
SearchBuilder<NetworkAccountVO> join2 = _accountsDao.createSearchBuilder();
|
|
join2.and("account", join2.entity().getAccountId(), Op.EQ);
|
|
RelatedConfigSearch.join("account", join2, join2.entity().getNetworkId(), RelatedConfigSearch.entity().getId(), JoinType.INNER);
|
|
RelatedConfigSearch.done();
|
|
|
|
AccountNetworkSearch = createSearchBuilder();
|
|
AccountNetworkSearch.and("networkId", AccountNetworkSearch.entity().getId(), Op.EQ);
|
|
SearchBuilder<NetworkAccountVO> mapJoin = _accountsDao.createSearchBuilder();
|
|
mapJoin.and("accountId", mapJoin.entity().getAccountId(), Op.EQ);
|
|
AccountNetworkSearch.join("networkSearch", mapJoin, AccountNetworkSearch.entity().getId(), mapJoin.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
|
|
AccountNetworkSearch.done();
|
|
|
|
|
|
ZoneBroadcastUriSearch = createSearchBuilder();
|
|
ZoneBroadcastUriSearch.and("dataCenterId", ZoneBroadcastUriSearch.entity().getDataCenterId(), Op.EQ);
|
|
ZoneBroadcastUriSearch.and("broadcastUri", ZoneBroadcastUriSearch.entity().getBroadcastUri(), Op.EQ);
|
|
ZoneBroadcastUriSearch.done();
|
|
|
|
_tgMacAddress = _tgs.get("macAddress");
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> listBy(long accountId, long dataCenterId, GuestIpType type) {
|
|
SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
|
|
sc.setParameters("datacenter", dataCenterId);
|
|
sc.setParameters("account", accountId);
|
|
if (type != null) {
|
|
sc.setParameters("guesttype", type);
|
|
}
|
|
return listBy(sc, null);
|
|
}
|
|
|
|
public List<NetworkVO> findBy(TrafficType trafficType, Mode mode, BroadcastDomainType broadcastType, long networkOfferingId, long dataCenterId) {
|
|
SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
|
|
sc.setParameters("trafficType", trafficType);
|
|
sc.setParameters("broadcastType", broadcastType);
|
|
sc.setParameters("offering", networkOfferingId);
|
|
sc.setParameters("datacenter", dataCenterId);
|
|
|
|
return search(sc, null);
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> listBy(long accountId) {
|
|
SearchCriteria<NetworkVO> sc = AccountSearch.create();
|
|
sc.setParameters("account", accountId);
|
|
sc.setJoinParameters("accounts", "account", accountId);
|
|
|
|
return listBy(sc);
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> listBy(long accountId, long offeringId, long dataCenterId) {
|
|
SearchCriteria<NetworkVO> sc = AccountSearch.create();
|
|
sc.setParameters("offering", offeringId);
|
|
sc.setJoinParameters("accounts", "account", accountId);
|
|
sc.setParameters("datacenter", dataCenterId);
|
|
|
|
return listBy(sc);
|
|
}
|
|
|
|
@Override @DB
|
|
public NetworkVO persist(NetworkVO config, boolean gc) {
|
|
Transaction txn = Transaction.currentTxn();
|
|
txn.start();
|
|
config = super.persist(config);
|
|
addAccountToNetworkConfiguration(config.getId(), config.getAccountId(), true);
|
|
NetworkOpVO op = new NetworkOpVO(config.getId(), gc);
|
|
_opDao.persist(op);
|
|
txn.commit();
|
|
return config;
|
|
}
|
|
|
|
@Override
|
|
public void addAccountToNetwork(long configurationId, long accountId) {
|
|
addAccountToNetworkConfiguration(configurationId, accountId, false);
|
|
}
|
|
|
|
protected void addAccountToNetworkConfiguration(long configurationId, long accountId, boolean isOwner) {
|
|
NetworkAccountVO account = new NetworkAccountVO(configurationId, accountId, isOwner);
|
|
_accountsDao.persist(account);
|
|
}
|
|
|
|
@Override
|
|
public SearchBuilder<NetworkAccountVO> createSearchBuilderForAccount() {
|
|
return _accountsDao.createSearchBuilder();
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> getNetworksForOffering(long offeringId, long dataCenterId, long accountId) {
|
|
SearchCriteria<NetworkVO> sc = RelatedConfigSearch.create();
|
|
sc.setParameters("offering", offeringId);
|
|
sc.setParameters("dc", dataCenterId);
|
|
sc.setJoinParameters("account", "account", accountId);
|
|
return search(sc, null);
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> getRelatedNetworks(long related) {
|
|
SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
|
|
sc.setParameters("related", related);
|
|
return search(sc, null);
|
|
}
|
|
|
|
@Override
|
|
public String getNextAvailableMacAddress(long networkConfigId) {
|
|
SequenceFetcher fetch = SequenceFetcher.getInstance();
|
|
|
|
long seq = fetch.getNextSequence(Long.class, _tgMacAddress, networkConfigId);
|
|
seq = seq | _prefix | ((_rand.nextInt(Short.MAX_VALUE) << 16) & 0x00000000ffff0000l);
|
|
return NetUtils.long2Mac(seq);
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> listBy(long accountId, long networkId) {
|
|
SearchCriteria<NetworkVO> sc = AccountNetworkSearch.create();
|
|
sc.setParameters("networkId", networkId);
|
|
sc.setJoinParameters("networkSearch", "accountId", accountId);
|
|
return listBy(sc);
|
|
}
|
|
|
|
@Override
|
|
public List<NetworkVO> listBy(long zoneId, String broadcastUri) {
|
|
SearchCriteria<NetworkVO> sc = ZoneBroadcastUriSearch.create();
|
|
sc.setParameters("dataCenterId", zoneId);
|
|
sc.setParameters("broadcastUri", broadcastUri);
|
|
return search(sc, null);
|
|
}
|
|
|
|
@Override
|
|
public void changeActiveNicsBy(long networkId, int count) {
|
|
_opDao.changeActiveNicsBy(networkId, count);
|
|
}
|
|
|
|
@Override
|
|
public int getActiveNicsIn(long networkId) {
|
|
return _opDao.getActiveNics(networkId);
|
|
}
|
|
|
|
@Override
|
|
public List<Long> findNetworksToGarbageCollect() {
|
|
return _opDao.getNetworksToGarbageCollect();
|
|
}
|
|
|
|
@Override
|
|
public void clearCheckForGc(long networkId) {
|
|
_opDao.clearCheckForGc(networkId);
|
|
}
|
|
}
|