private void removeNetworks() {
    final List<Network> networks = getNetworkDao().getAllForDataCenter(getStoragePoolId());
    for (Network network : networks) {
      if (network.isExternal()) {
        for (VmNic nic : getVmNicDao().getAllForNetwork(network.getId())) {
          new ExternalNetworkManager(nic, network).deallocateIfExternal();
        }
      }
    }

    TransactionSupport.executeInNewTransaction(
        new TransactionMethod<Void>() {

          @Override
          public Void runInTransaction() {
            for (final Network net : networks) {
              List<VnicProfile> profiles =
                  getDbFacade().getVnicProfileDao().getAllForNetwork(net.getId());
              for (VnicProfile vnicProfile : profiles) {
                getCompensationContext().snapshotEntity(vnicProfile);
                getDbFacade().getVnicProfileDao().remove(vnicProfile.getId());
              }
              getCompensationContext().snapshotEntity(net);
              getNetworkDao().remove(net.getId());
            }
            getCompensationContext().stateChanged();
            return null;
          }
        });
  }
Example #2
0
 /**
  * Determine if a given network interface should be configured on hosts
  *
  * @param network the network to check.
  * @return <code>true</code> iff the network is labeled and not an external network.
  */
 public static boolean isConfiguredByLabel(Network network) {
   return isLabeled(network) && !network.isExternal();
 }
  @Override
  protected boolean canDoAction() {
    String ifaceGateway = null;
    interfaces = getDbFacade().getInterfaceDao().getAllInterfacesForVds(getParameters().getVdsId());

    // check that interface exists
    for (final VdsNetworkInterface i : getParameters().getInterfaces()) {
      VdsNetworkInterface iface =
          interfaces.stream().filter(x -> x.getName().equals(i.getName())).findFirst().orElse(null);
      if (iface == null) {
        addCanDoActionMessage(EngineMessage.NETWORK_INTERFACE_NOT_EXISTS);
        return false;
      }
      ifaceGateway = iface.getGateway();
    }

    // check that the old network name is not null
    if (StringUtils.isEmpty(getParameters().getOldNetworkName())) {
      addCanDoActionMessage(EngineMessage.NETWORK_OLD_NETWORK_NOT_SPECIFIED);
      return false;
    }

    VDS vds = getVdsDao().get(getParameters().getVdsId());
    if (vds.getStatus() != VDSStatus.Maintenance) {
      // check that the old network exists in host
      boolean ifaceFound =
          interfaces
              .stream()
              .anyMatch(
                  i ->
                      i.getNetworkName() != null
                          && i.getNetworkName().equals(getParameters().getNetwork().getName()));
      if (ifaceFound) {
        addCanDoActionMessage(EngineMessage.NETWORK_HOST_IS_BUSY);
        return false;
      }
    }

    // check that the old network exists in host
    boolean ifacenetNotFound =
        interfaces
            .stream()
            .noneMatch(
                i ->
                    i.getNetworkName() != null
                        && i.getNetworkName().equals(getParameters().getOldNetworkName()));
    if (ifacenetNotFound) {
      addCanDoActionMessage(EngineMessage.NETWORK_NOT_EXISTS);
      return false;
    }

    final Guid clusterId = getVdsGroupId();
    if (!managementNetworkUtil.isManagementNetwork(
        getParameters().getNetwork().getName(), clusterId)) {
      if (managementNetworkUtil.isManagementNetwork(
          getParameters().getOldNetworkName(), clusterId)) {
        getReturnValue()
            .getCanDoActionMessages()
            .add(EngineMessage.NETWORK_DEFAULT_UPDATE_NAME_INVALID.toString());
        getReturnValue()
            .getCanDoActionMessages()
            .add(String.format("$NetworkName %1$s", getParameters().getOldNetworkName()));
        return false;
      }

      if (StringUtils.isNotEmpty(getParameters().getGateway())) {
        if (!getParameters().getGateway().equals(ifaceGateway)) {
          addCanDoActionMessage(EngineMessage.NETWORK_ATTACH_ILLEGAL_GATEWAY);
          return false;
        }
        // if the gateway didn't change we don't want the vdsm to set it.
        else {
          getParameters().setGateway(null);
        }
      }

      // check connectivity
      if (getParameters().getCheckConnectivity()) {
        addCanDoActionMessage(EngineMessage.NETWORK_CHECK_CONNECTIVITY);
        return false;
      }
    }

    // check address exists in static ip
    if (getParameters().getBootProtocol() == NetworkBootProtocol.STATIC_IP) {
      if (StringUtils.isEmpty(getParameters().getAddress())) {
        addCanDoActionMessage(EngineMessage.NETWORK_ADDR_MANDATORY_IN_STATIC_IP);
        return false;
      }
    }

    Network network = getNetworkDao().getByNameAndCluster(getNetworkName(), vds.getVdsGroupId());
    if (network != null && network.isExternal()) {
      return failCanDoAction(EngineMessage.EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED);
    }

    return true;
  }