Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions engine/schema/src/main/java/com/cloud/dc/dao/VlanDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public interface VlanDao extends GenericDao<VlanVO, Long> {

List<VlanVO> listVlansByNetworkId(long networkId);

List<VlanVO> listVlansByNetworkIds(List<Long> networkIds);

List<VlanVO> listVlansByNetworkIdIncludingRemoved(long networkId);

List<VlanVO> listVlansByPhysicalNetworkId(long physicalNetworkId);
Expand Down
14 changes: 13 additions & 1 deletion engine/schema/src/main/java/com/cloud/dc/dao/VlanDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.dc.VlanDetailsVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -134,7 +136,7 @@ public VlanDaoImpl() {
ZoneTypeSearch.done();

NetworkVlanSearch = createSearchBuilder();
NetworkVlanSearch.and("networkId", NetworkVlanSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
NetworkVlanSearch.and("networkId", NetworkVlanSearch.entity().getNetworkId(), SearchCriteria.Op.IN);
NetworkVlanSearch.done();

PhysicalNetworkVlanSearch = createSearchBuilder();
Expand Down Expand Up @@ -392,6 +394,16 @@ public List<VlanVO> listVlansByNetworkId(long networkId) {
return listBy(sc);
}

@Override
public List<VlanVO> listVlansByNetworkIds(List<Long> networkIds) {
if (CollectionUtils.isEmpty(networkIds)) {
return Collections.emptyList();
}
SearchCriteria<VlanVO> sc = NetworkVlanSearch.create();
sc.setParameters("networkId", networkIds.toArray());
return listBy(sc);
}

@Override public List<VlanVO> listVlansByNetworkIdIncludingRemoved(long networkId) {
SearchCriteria<VlanVO> sc = NetworkVlanSearch.create();
sc.setParameters("networkId", networkId);
Expand Down
2 changes: 2 additions & 0 deletions engine/schema/src/main/java/com/cloud/vm/dao/NicDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public interface NicDao extends GenericDao<NicVO, Long> {

NicVO findByMacAddress(String macAddress, long networkId);

List<NicVO> listByMacAddresses(List<String> macAddresses);

NicVO findByNetworkIdAndMacAddressIncludingRemoved(long networkId, String mac);

List<NicVO> findNicsByIpv6GatewayIpv6CidrAndReserver(String ipv6Gateway, String ipv6Cidr, String reserverName);
Expand Down
15 changes: 13 additions & 2 deletions engine/schema/src/main/java/com/cloud/vm/dao/NicDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import com.cloud.utils.db.Filter;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected void init() {
AllFieldsSearch.and("strategy", AllFieldsSearch.entity().getReservationStrategy(), Op.EQ);
AllFieldsSearch.and("strategyNEQ", AllFieldsSearch.entity().getReservationStrategy(), Op.NEQ);
AllFieldsSearch.and("reserverName",AllFieldsSearch.entity().getReserver(),Op.EQ);
AllFieldsSearch.and("macAddress", AllFieldsSearch.entity().getMacAddress(), Op.EQ);
AllFieldsSearch.and("macAddress", AllFieldsSearch.entity().getMacAddress(), Op.IN);
AllFieldsSearch.and("deviceid", AllFieldsSearch.entity().getDeviceId(), Op.EQ);
AllFieldsSearch.and("ipv6Gateway", AllFieldsSearch.entity().getIPv6Gateway(), Op.EQ);
AllFieldsSearch.and("ipv6Cidr", AllFieldsSearch.entity().getIPv6Cidr(), Op.EQ);
Expand Down Expand Up @@ -427,6 +428,16 @@ public NicVO findByMacAddress(String macAddress, long networkId) {
return findOneBy(sc);
}

@Override
public List<NicVO> listByMacAddresses(List<String> macAddresses) {
if (CollectionUtils.isEmpty(macAddresses)) {
return Collections.emptyList();
}
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("macAddress", macAddresses.toArray());
return listBy(sc);
}

@Override
public List<NicVO> findNicsByIpv6GatewayIpv6CidrAndReserver(String ipv6Gateway, String ipv6Cidr, String reserverName) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
Expand Down
28 changes: 24 additions & 4 deletions server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5195,11 +5195,31 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
return;
}

List<String> macAddresses = new ArrayList<>(vmNetworkStats.size());
for (VmNetworkStatsEntry entry : vmNetworkStats) {
macAddresses.add(entry.getMacAddress());
}
Map<String, NicVO> nicsByMac = new HashMap<>();
for (NicVO nic : _nicDao.listByMacAddresses(macAddresses)) {
nicsByMac.put(nic.getMacAddress(), nic);
}

Set<Long> networkIds = new HashSet<>();
for (NicVO nic : nicsByMac.values()) {
networkIds.add(nic.getNetworkId());
}
Map<Long, List<VlanVO>> vlansByNetwork = new HashMap<>();
for (VlanVO vlan : _vlanDao.listVlansByNetworkIds(new ArrayList<>(networkIds))) {
vlansByNetwork.computeIfAbsent(vlan.getNetworkId(), k -> new ArrayList<>()).add(vlan);
}

for (VmNetworkStatsEntry vmNetworkStat:vmNetworkStats) {
SearchCriteria<NicVO> sc_nic = _nicDao.createSearchCriteria();
sc_nic.addAnd("macAddress", SearchCriteria.Op.EQ, vmNetworkStat.getMacAddress());
NicVO nic = _nicDao.search(sc_nic, null).get(0);
List<VlanVO> vlan = _vlanDao.listVlansByNetworkId(nic.getNetworkId());
NicVO nic = nicsByMac.get(vmNetworkStat.getMacAddress());
if (nic == null) {
logger.warn("Unable to find nic for mac " + vmNetworkStat.getMacAddress());
continue;
}
List<VlanVO> vlan = vlansByNetwork.get(nic.getNetworkId());
if (vlan == null || vlan.size() == 0 || vlan.get(0).getVlanType() != VlanType.DirectAttached)
{
break; // only get network statistics for DirectAttached network (shared networks in Basic zone and Advanced zone with/without SG)
Expand Down
Loading