@Override
  @DB
  public boolean prepare(
      Network network,
      NicProfile nic,
      VirtualMachineProfile<? extends VirtualMachine> vm,
      DeployDestination dest,
      ReservationContext context)
      throws ConcurrentOperationException, ResourceUnavailableException,
          InsufficientCapacityException {
    Host host = dest.getHost();
    if (host == null || host.getHypervisorType() != HypervisorType.BareMetal) {
      return true;
    }

    Transaction txn = Transaction.currentTxn();
    txn.start();
    nic.setMacAddress(host.getPrivateMacAddress());
    NicVO vo = _nicDao.findById(nic.getId());
    assert vo != null : "Where ths nic " + nic.getId() + " going???";
    vo.setMacAddress(nic.getMacAddress());
    _nicDao.update(vo.getId(), vo);
    txn.commit();
    s_logger.debug(
        "Bare Metal changes mac address of nic " + nic.getId() + " to " + nic.getMacAddress());

    return _dhcpMgr.addVirtualMachineIntoNetwork(network, nic, vm, dest, context);
  }
  @Override
  public boolean finalizeDeployment(
      Commands cmds,
      VirtualMachineProfile<DomainRouterVO> profile,
      DeployDestination dest,
      ReservationContext context)
      throws ResourceUnavailableException {
    DomainRouterVO elbVm = profile.getVirtualMachine();

    List<NicProfile> nics = profile.getNics();
    for (NicProfile nic : nics) {
      if (nic.getTrafficType() == TrafficType.Public) {
        elbVm.setPublicIpAddress(nic.getIp4Address());
        elbVm.setPublicNetmask(nic.getNetmask());
        elbVm.setPublicMacAddress(nic.getMacAddress());
      } else if (nic.getTrafficType() == TrafficType.Control) {
        elbVm.setPrivateIpAddress(nic.getIp4Address());
        elbVm.setPrivateMacAddress(nic.getMacAddress());
      }
    }
    _routerDao.update(elbVm.getId(), elbVm);

    finalizeCommandsOnStart(cmds, profile);
    return true;
  }
  @Override
  public boolean finalizeDeployment(
      Commands cmds,
      VirtualMachineProfile<SecondaryStorageVmVO> profile,
      DeployDestination dest,
      ReservationContext context) {

    finalizeCommandsOnStart(cmds, profile);

    SecondaryStorageVmVO secVm = profile.getVirtualMachine();
    DataCenter dc = dest.getDataCenter();
    List<NicProfile> nics = profile.getNics();
    for (NicProfile nic : nics) {
      if ((nic.getTrafficType() == TrafficType.Public
              && dc.getNetworkType() == NetworkType.Advanced)
          || (nic.getTrafficType() == TrafficType.Guest
              && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) {
        secVm.setPublicIpAddress(nic.getIp4Address());
        secVm.setPublicNetmask(nic.getNetmask());
        secVm.setPublicMacAddress(nic.getMacAddress());
      } else if (nic.getTrafficType() == TrafficType.Management) {
        secVm.setPrivateIpAddress(nic.getIp4Address());
        secVm.setPrivateMacAddress(nic.getMacAddress());
      }
    }
    _secStorageVmDao.update(secVm.getId(), secVm);
    return true;
  }
  private boolean preparePxeInBasicZone(
      VirtualMachineProfile profile,
      NicProfile nic,
      DeployDestination dest,
      ReservationContext context)
      throws AgentUnavailableException, OperationTimedoutException {
    NetworkVO nwVO = _nwDao.findById(nic.getNetworkId());
    QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class);
    sc.and(sc.entity().getDeviceType(), Op.EQ, BaremetalPxeType.KICK_START.toString());
    sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, nwVO.getPhysicalNetworkId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
      throw new CloudRuntimeException(
          "No kickstart PXE server found in pod: "
              + dest.getPod().getId()
              + ", you need to add it before starting VM");
    }
    VMTemplateVO template = _tmpDao.findById(profile.getTemplateId());
    List<String> tuple = parseKickstartUrl(profile);

    String ks = tuple.get(0);
    String kernel = tuple.get(1);
    String initrd = tuple.get(2);

    PrepareKickstartPxeServerCommand cmd = new PrepareKickstartPxeServerCommand();
    cmd.setKsFile(ks);
    cmd.setInitrd(initrd);
    cmd.setKernel(kernel);
    cmd.setMac(nic.getMacAddress());
    cmd.setTemplateUuid(template.getUuid());
    Answer aws = _agentMgr.send(pxeVo.getHostId(), cmd);
    if (!aws.getResult()) {
      s_logger.warn(
          "Unable to set host: "
              + dest.getHost().getId()
              + " to PXE boot because "
              + aws.getDetails());
      return false;
    }

    return true;
  }
  /**
   * @param router
   * @param add
   * @param privateNic
   * @return
   * @throws ResourceUnavailableException
   */
  protected boolean setupVpcPrivateNetwork(
      final VirtualRouter router, final boolean add, final NicProfile privateNic)
      throws ResourceUnavailableException {

    if (router.getState() == State.Running) {
      final PrivateIpVO ipVO =
          _privateIpDao.findByIpAndSourceNetworkId(
              privateNic.getNetworkId(), privateNic.getIPv4Address());
      final Network network = _networkDao.findById(privateNic.getNetworkId());
      final String netmask = NetUtils.getCidrNetmask(network.getCidr());
      final PrivateIpAddress ip =
          new PrivateIpAddress(
              ipVO,
              network.getBroadcastUri().toString(),
              network.getGateway(),
              netmask,
              privateNic.getMacAddress());

      final List<PrivateIpAddress> privateIps = new ArrayList<PrivateIpAddress>(1);
      privateIps.add(ip);
      final Commands cmds = new Commands(Command.OnError.Stop);
      _commandSetupHelper.createVpcAssociatePrivateIPCommands(router, privateIps, cmds, add);

      try {
        if (_nwHelper.sendCommandsToRouter(router, cmds)) {
          s_logger.debug(
              "Successfully applied ip association for ip " + ip + " in vpc network " + network);
          return true;
        } else {
          s_logger.warn("Failed to associate ip address " + ip + " in vpc network " + network);
          return false;
        }
      } catch (final Exception ex) {
        s_logger.warn(
            "Failed to send  "
                + (add ? "add " : "delete ")
                + " private network "
                + network
                + " commands to rotuer ");
        return false;
      }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
      s_logger.debug(
          "Router "
              + router.getInstanceName()
              + " is in "
              + router.getState()
              + ", so not sending setup private network command to the backend");
    } else {
      s_logger.warn(
          "Unable to setup private gateway, virtual router "
              + router
              + " is not in the right state "
              + router.getState());

      throw new ResourceUnavailableException(
          "Unable to setup Private gateway on the backend,"
              + " virtual router "
              + router
              + " is not in the right state",
          DataCenter.class,
          router.getDataCenterId());
    }
    return true;
  }
  private boolean preparePxeInAdvancedZone(
      VirtualMachineProfile profile,
      NicProfile nic,
      Network network,
      DeployDestination dest,
      ReservationContext context)
      throws Exception {
    DomainRouterVO vr = getVirtualRouter(network);
    List<NicVO> nics = _nicDao.listByVmId(vr.getId());
    NicVO mgmtNic = null;
    for (NicVO nicvo : nics) {
      if (ControlNetworkGuru.class.getSimpleName().equals(nicvo.getReserver())) {
        mgmtNic = nicvo;
        break;
      }
    }

    if (mgmtNic == null) {
      throw new CloudRuntimeException(
          String.format("cannot find management nic on virtual router[id:%s]", vr.getId()));
    }

    String internalServerIp = _configDao.getValue(Config.BaremetalInternalStorageServer.key());
    if (internalServerIp == null) {
      throw new CloudRuntimeException(
          String.format(
              "please specify 'baremetal.internal.storage.server.ip', which is the http server/nfs server storing kickstart files and ISO files, in global setting"));
    }

    List<String> tuple = parseKickstartUrl(profile);
    String cmd =
        String.format(
            "/usr/bin/prepare_pxe.sh %s %s %s %s %s %s",
            tuple.get(1),
            tuple.get(2),
            profile.getTemplate().getUuid(),
            String.format("01-%s", nic.getMacAddress().replaceAll(":", "-")).toLowerCase(),
            tuple.get(0),
            nic.getMacAddress().toLowerCase());
    s_logger.debug(
        String.format(
            "prepare pxe on virtual router[ip:%s], cmd: %s", mgmtNic.getIp4Address(), cmd));
    Pair<Boolean, String> ret =
        SshHelper.sshExecute(
            mgmtNic.getIp4Address(), 3922, "root", getSystemVMKeyFile(), null, cmd);
    if (!ret.first()) {
      throw new CloudRuntimeException(
          String.format(
              "failed preparing PXE in virtual router[id:%s], because %s",
              vr.getId(), ret.second()));
    }

    // String internalServerIp = "10.223.110.231";
    cmd =
        String.format(
            "/usr/bin/baremetal_snat.sh %s %s %s",
            mgmtNic.getIp4Address(), internalServerIp, mgmtNic.getGateway());
    s_logger.debug(
        String.format(
            "prepare SNAT on virtual router[ip:%s], cmd: %s", mgmtNic.getIp4Address(), cmd));
    ret =
        SshHelper.sshExecute(
            mgmtNic.getIp4Address(), 3922, "root", getSystemVMKeyFile(), null, cmd);
    if (!ret.first()) {
      throw new CloudRuntimeException(
          String.format(
              "failed preparing PXE in virtual router[id:%s], because %s",
              vr.getId(), ret.second()));
    }

    return true;
  }
  @Override
  public boolean prepare(
      VirtualMachineProfile<UserVmVO> profile,
      NicProfile pxeNic,
      DeployDestination dest,
      ReservationContext context) {
    SearchCriteriaService<BaremetalPxeVO, BaremetalPxeVO> sc =
        SearchCriteria2.create(BaremetalPxeVO.class);
    sc.addAnd(sc.getEntity().getDeviceType(), Op.EQ, BaremetalPxeType.PING.toString());
    sc.addAnd(sc.getEntity().getPodId(), Op.EQ, dest.getPod().getId());
    BaremetalPxeVO pxeVo = sc.find();
    if (pxeVo == null) {
      throw new CloudRuntimeException(
          "No PING PXE server found in pod: "
              + dest.getPod().getId()
              + ", you need to add it before starting VM");
    }
    long pxeServerId = pxeVo.getHostId();

    String mac = pxeNic.getMacAddress();
    String ip = pxeNic.getIp4Address();
    String gateway = pxeNic.getGateway();
    String mask = pxeNic.getNetmask();
    String dns = pxeNic.getDns1();
    if (dns == null) {
      dns = pxeNic.getDns2();
    }

    try {
      String tpl = profile.getTemplate().getUrl();
      assert tpl != null : "How can a null template get here!!!";
      PreparePxeServerCommand cmd =
          new PreparePxeServerCommand(
              ip,
              mac,
              mask,
              gateway,
              dns,
              tpl,
              profile.getVirtualMachine().getInstanceName(),
              dest.getHost().getName());
      PreparePxeServerAnswer ans = (PreparePxeServerAnswer) _agentMgr.send(pxeServerId, cmd);
      if (!ans.getResult()) {
        s_logger.warn(
            "Unable tot program PXE server: " + pxeVo.getId() + " because " + ans.getDetails());
        return false;
      }

      IpmISetBootDevCommand bootCmd = new IpmISetBootDevCommand(BootDev.pxe);
      Answer anw = _agentMgr.send(dest.getHost().getId(), bootCmd);
      if (!anw.getResult()) {
        s_logger.warn(
            "Unable to set host: "
                + dest.getHost().getId()
                + " to PXE boot because "
                + anw.getDetails());
      }

      return anw.getResult();
    } catch (Exception e) {
      s_logger.warn("Cannot prepare PXE server", e);
      return false;
    }
  }