/** * 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 . * */ package com.cloud.vm.dao; import java.util.List; import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.network.router.VirtualRouter.Role; 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 com.cloud.utils.db.Transaction; import com.cloud.utils.db.UpdateBuilder; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.VirtualMachine.State; @Local(value = { DomainRouterDao.class }) public class DomainRouterDaoImpl extends GenericDaoBase implements DomainRouterDao { private static final Logger s_logger = Logger.getLogger(DomainRouterDaoImpl.class); protected final SearchBuilder AllFieldsSearch; protected final SearchBuilder IdStatesSearch; protected final SearchBuilder HostUpSearch; protected DomainRouterDaoImpl() { AllFieldsSearch = createSearchBuilder(); AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), Op.EQ); AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ); AllFieldsSearch.and("role", AllFieldsSearch.entity().getRole(), Op.EQ); AllFieldsSearch.and("domainId", AllFieldsSearch.entity().getDomainId(), Op.EQ); AllFieldsSearch.and("host", AllFieldsSearch.entity().getHostId(), Op.EQ); AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ); AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ); AllFieldsSearch.and("podId", AllFieldsSearch.entity().getPodId(), Op.EQ); AllFieldsSearch.done(); IdStatesSearch = createSearchBuilder(); IdStatesSearch.and("id", IdStatesSearch.entity().getId(), Op.EQ); IdStatesSearch.and("states", IdStatesSearch.entity().getState(), Op.IN); IdStatesSearch.done(); HostUpSearch = createSearchBuilder(); HostUpSearch.and("host", HostUpSearch.entity().getHostId(), Op.EQ); HostUpSearch.and("states", HostUpSearch.entity().getState(), Op.NIN); HostUpSearch.done(); } @Override public boolean remove(Long id) { Transaction txn = Transaction.currentTxn(); txn.start(); DomainRouterVO router = createForUpdate(); router.setPublicIpAddress(null); UpdateBuilder ub = getUpdateBuilder(router); ub.set(router, "state", State.Destroyed); update(id, ub); boolean result = super.remove(id); txn.commit(); return result; } @Override public List listByDataCenter(long dcId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("dc", dcId); return listBy(sc); } @Override public DomainRouterVO findBy(long accountId, long dcId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("role", Role.DHCP_FIREWALL_LB_PASSWD_USERDATA); return findOneBy(sc); } @Override public DomainRouterVO findBy(long accountId, long dcId, Role role) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); sc.setParameters("dc", dcId); sc.setParameters("role", role); return findOneBy(sc); } @Override public List listBy(long accountId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("account", accountId); return listBy(sc); } @Override public List listByHostId(Long hostId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("host", hostId); return listBy(sc); } @Override public List listUpByHostId(Long hostId) { SearchCriteria sc = HostUpSearch.create(); if(hostId != null){ sc.setParameters("host", hostId); } sc.setParameters("states", State.Destroyed, State.Stopped, State.Expunging); return listBy(sc); } @Override public List listByDomain(Long domainId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("domainId", domainId); return listBy(sc); } @Override public DomainRouterVO findByNetwork(long networkId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("network", networkId); return findOneBy(sc); } @Override public List listByLastHostId(Long hostId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("lastHost", hostId); sc.setParameters("state", State.Stopped); return listBy(sc); } @Override public DomainRouterVO findByNetworkAndPod(long networkId, long podId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("network", networkId); sc.setParameters("podId", podId); return findOneBy(sc); } }