package org.ovirt.engine.ui.uicommonweb.models.vms;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import org.ovirt.engine.core.common.VdcActionUtils;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.VMStatus;
import org.ovirt.engine.core.common.businessentities.VmGuestAgentInterface;
import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.VdcQueryType;
import org.ovirt.engine.ui.uicommonweb.UICommand;
import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
import org.ovirt.engine.ui.uicommonweb.help.HelpTag;
import org.ovirt.engine.ui.uicommonweb.models.SearchableListModel;
import org.ovirt.engine.ui.uicompat.ConstantsManager;
import org.ovirt.engine.ui.uicompat.PropertyChangedEventArgs;
public class VmInterfaceListModel extends SearchableListModel<VM, VmNetworkInterface> {
private UICommand privateNewCommand;
private UICommand privateEditCommand;
private UICommand privateRemoveCommand;
public VmInterfaceListModel() {
setTitle(ConstantsManager.getInstance().getConstants().networkInterfacesTitle());
setHelpTag(HelpTag.network_interfaces);
setHashName("network_interfaces"); //$NON-NLS-1$
setNewCommand(new UICommand("New", this)); //$NON-NLS-1$
setEditCommand(new UICommand("Edit", this)); //$NON-NLS-1$
setRemoveCommand(new UICommand("Remove", this)); //$NON-NLS-1$
initSelectionGeustAgentData(getSelectedItem());
updateActionAvailability();
}
private List<VmGuestAgentInterface> guestAgentData;
private List<VmGuestAgentInterface> selectionGuestAgentData;
public UICommand getNewCommand() {
return privateNewCommand;
}
private void setNewCommand(UICommand value) {
privateNewCommand = value;
}
@Override
public UICommand getEditCommand() {
return privateEditCommand;
}
private void setEditCommand(UICommand value) {
privateEditCommand = value;
}
public UICommand getRemoveCommand() {
return privateRemoveCommand;
}
private void setRemoveCommand(UICommand value) {
privateRemoveCommand = value;
}
public List<VmGuestAgentInterface> getGuestAgentData() {
return guestAgentData;
}
public void setGuestAgentData(List<VmGuestAgentInterface> guestAgentData) {
this.guestAgentData = guestAgentData;
}
public List<VmGuestAgentInterface> getSelectionGuestAgentData() {
return selectionGuestAgentData;
}
public void setSelectionGuestAgentData(List<VmGuestAgentInterface> selectedGuestAgentData) {
this.selectionGuestAgentData = selectedGuestAgentData;
}
@Override
protected void onEntityChanged() {
super.onEntityChanged();
if (getEntity() != null) {
getSearchCommand().execute();
}
updateActionAvailability();
}
@Override
protected void syncSearch() {
if (getEntity() == null) {
return;
}
final VM vm = getEntity();
// Initialize guest agent data
AsyncDataProvider.getInstance().getVmGuestAgentInterfacesByVmId(new AsyncQuery<>(result -> {
setGuestAgentData(result);
VmInterfaceListModel.super.syncSearch(VdcQueryType.GetVmInterfacesByVmId, new IdQueryParameters(vm.getId()));
}), vm.getId());
}
private void newEntity() {
if (getWindow() != null) {
return;
}
VmInterfaceModel model =
NewVmInterfaceModel.createInstance(getEntity().getStaticData(),
getEntity().getStatus(),
getEntity().getStoragePoolId(),
getEntity().getClusterCompatibilityVersion(),
(ArrayList<VmNetworkInterface>) getItems(),
this);
setWindow(model);
}
private void edit() {
if (getWindow() != null) {
return;
}
VmInterfaceModel model =
EditVmInterfaceModel.createInstance(getEntity().getStaticData(), getEntity(),
getEntity().getClusterCompatibilityVersion(),
(ArrayList<VmNetworkInterface>) getItems(),
getSelectedItem(), this);
setWindow(model);
}
private void remove() {
if (getWindow() != null) {
return;
}
RemoveVmInterfaceModel model = new RemoveVmInterfaceModel(this, getSelectedItems(), false);
setWindow(model);
}
@Override
protected void selectedItemsChanged() {
super.selectedItemsChanged();
updateActionAvailability();
}
@Override
protected void entityPropertyChanged(Object sender, PropertyChangedEventArgs e) {
super.entityPropertyChanged(sender, e);
if (e.propertyName.equals("status")) { //$NON-NLS-1$
updateActionAvailability();
}
}
private void updateActionAvailability() {
VM vm = getEntity();
ArrayList<VM> items = new ArrayList<>();
if (vm != null) {
items.add(vm);
}
getNewCommand().setIsExecutionAllowed(vm != null
&& VdcActionUtils.canExecute(items, VM.class, VdcActionType.AddVmInterface));
getEditCommand().setIsExecutionAllowed(vm != null
&& VdcActionUtils.canExecute(items, VM.class, VdcActionType.UpdateVmInterface)
&& (getSelectedItems() != null && getSelectedItems().size() == 1));
getRemoveCommand().setIsExecutionAllowed(vm != null
&& VdcActionUtils.canExecute(items, VM.class, VdcActionType.RemoveVmInterface) && canRemoveNics()
&& (getSelectedItems() != null && getSelectedItems().size() > 0));
}
private boolean canRemoveNics() {
VM vm = getEntity();
if (VMStatus.Down.equals(vm.getStatus())) {
return true;
}
List<VmNetworkInterface> nics =
getSelectedItems() != null ? getSelectedItems() : new ArrayList<VmNetworkInterface>();
for (VmNetworkInterface nic : nics) {
if (nic.isPlugged()) {
return false;
}
}
return true;
}
@Override
public void executeCommand(UICommand command) {
super.executeCommand(command);
if (command == getNewCommand()) {
newEntity();
}
else if (command == getEditCommand()) {
edit();
}
else if (command == getRemoveCommand()) {
remove();
}
}
@Override
protected String getListName() {
return "VmInterfaceListModel"; //$NON-NLS-1$
}
@Override
protected void onSelectedItemChanging(VmNetworkInterface newValue, VmNetworkInterface oldValue) {
initSelectionGeustAgentData(newValue);
super.onSelectedItemChanging(newValue, oldValue);
}
private void initSelectionGeustAgentData(VmNetworkInterface selectedItem) {
if (selectedItem == null || getGuestAgentData() == null){
setSelectionGuestAgentData(null);
return;
}
List<VmGuestAgentInterface> selectionInterfaces = new ArrayList<>();
for (VmGuestAgentInterface guestInterface : getGuestAgentData()) {
if (Objects.equals(guestInterface.getMacAddress(), selectedItem.getMacAddress())) {
selectionInterfaces.add(guestInterface);
}
}
setSelectionGuestAgentData(selectionInterfaces);
}
@Override
protected void onSelectedItemChanged() {
super.onSelectedItemChanged();
updateActionAvailability();
}
@Override
public void setItems(Collection<VmNetworkInterface> value) {
super.setItems(value);
if (getSelectedItem() == null && (getSelectedItems() == null || getSelectedItems().size() == 0)) {
if (value != null && value.iterator().hasNext()) {
setSelectedItem(value.iterator().next());
}
}
}
}