@Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    _configDao = locator.getDao(ConfigurationDao.class);
    _setupAgentPath = Script.findScript(getPatchPath(), "setup_agent.sh");
    _kvmPrivateNic = _configDao.getValue(Config.KvmPrivateNetwork.key());
    if (_kvmPrivateNic == null) {
      _kvmPrivateNic = "cloudbr0";
    }

    _kvmPublicNic = _configDao.getValue(Config.KvmPublicNetwork.key());
    if (_kvmPublicNic == null) {
      _kvmPublicNic = _kvmPrivateNic;
    }

    _kvmGuestNic = _configDao.getValue(Config.KvmGuestNetwork.key());
    if (_kvmGuestNic == null) {
      _kvmGuestNic = _kvmPrivateNic;
    }

    if (_setupAgentPath == null) {
      throw new ConfigurationException("Can't find setup_agent.sh");
    }
    _hostIp = _configDao.getValue("host");
    if (_hostIp == null) {
      throw new ConfigurationException("Can't get host IP");
    }
    _resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
    return true;
  }
Example #2
0
 public ApiServlet() {
   super();
   _apiServer = ApiServer.getInstance();
   if (_apiServer == null) {
     throw new CloudRuntimeException("ApiServer not initialized");
   }
   ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
   _accountMgr = locator.getManager(AccountService.class);
 }
  public void test217to22Upgrade() throws SQLException {
    s_logger.debug("Finding sample data from 2.2.14");
    //        DbTestUtils.executeScript("PreviousDatabaseSchema/2.2.14/dave-sample.sql", false,
    // true);

    Connection conn;
    PreparedStatement pstmt;

    VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class);
    DatabaseUpgradeChecker checker = ComponentLocator.inject(DatabaseUpgradeChecker.class);

    String version = dao.getCurrentVersion();
    assert version.equals("2.2.14") : "Version returned is not 2.2.14 but " + version;

    checker.upgrade("2.2.14", "3.0.3");
  }
 public static void initApiServer() {
   if (s_instance == null) {
     // Injection will create ApiServer and all its fields which have @Inject
     s_instance = ComponentLocator.inject(ApiServer.class);
     s_instance.init();
   }
 }
  @Override
  public List<UserVmVO> listVirtualNetworkInstancesByAcctAndZone(long accountId, long dcId) {
    if (AccountDataCenterVirtualSearch == null) {
      ServiceOfferingDao offeringDao =
          ComponentLocator.getLocator("management-server").getDao(ServiceOfferingDao.class);
      SearchBuilder<ServiceOfferingVO> offeringSearch = offeringDao.createSearchBuilder();
      offeringSearch.and(
          "guestIpType", offeringSearch.entity().getGuestIpType(), SearchCriteria.Op.EQ);

      AccountDataCenterVirtualSearch = createSearchBuilder();
      AccountDataCenterVirtualSearch.and(
          "account", AccountDataCenterVirtualSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
      AccountDataCenterVirtualSearch.and(
          "dc", AccountDataCenterVirtualSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
      AccountDataCenterVirtualSearch.join(
          "offeringSearch",
          offeringSearch,
          AccountDataCenterVirtualSearch.entity().getServiceOfferingId(),
          offeringSearch.entity().getId());
      AccountDataCenterVirtualSearch.done();
    }

    SearchCriteria sc = AccountDataCenterVirtualSearch.create();
    sc.setParameters("account", accountId);
    sc.setParameters("dc", dcId);
    sc.setJoinParameters("offeringSearch", "guestIpType", ServiceOffering.GuestIpType.Virtualized);

    return listActiveBy(sc);
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {

    Map<String, String> configs = _configDao.getConfiguration("Network", params);
    _numWorkerThreads =
        NumbersUtil.parseInt(
            configs.get(Config.SecurityGroupWorkerThreads.key()), WORKER_THREAD_COUNT);
    _timeBetweenCleanups =
        NumbersUtil.parseInt(
            configs.get(Config.SecurityGroupWorkCleanupInterval.key()), TIME_BETWEEN_CLEANUPS);
    _globalWorkLockTimeout =
        NumbersUtil.parseInt(configs.get(Config.SecurityGroupWorkGlobalLockTimeout.key()), 300);
    /* register state listener, no matter security group is enabled or not */
    VirtualMachine.State.getStateMachine().registerListener(this);

    _answerListener = new SecurityGroupListener(this, _agentMgr, _workDao);
    _agentMgr.registerForHostEvents(_answerListener, true, true, true);

    _serverId = ((ManagementServer) ComponentLocator.getComponent(ManagementServer.Name)).getId();

    s_logger.info(
        "SecurityGroupManager: num worker threads="
            + _numWorkerThreads
            + ", time between cleanups="
            + _timeBetweenCleanups
            + " global lock timeout="
            + _globalWorkLockTimeout);
    createThreadPools();

    return true;
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {

    if (s_logger.isInfoEnabled()) {
      s_logger.info("Start configuring AgentBasedConsoleProxyManager");
    }

    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      throw new ConfigurationException("Unable to get the configuration dao.");
    }

    Map<String, String> configs = configDao.getConfiguration("management-server", params);
    String value = configs.get("consoleproxy.url.port");
    if (value != null) {
      _consoleProxyUrlPort =
          NumbersUtil.parseInt(value, ConsoleProxyManager.DEFAULT_PROXY_URL_PORT);
    }

    value = configs.get("consoleproxy.port");
    if (value != null) {
      _consoleProxyPort = NumbersUtil.parseInt(value, ConsoleProxyManager.DEFAULT_PROXY_VNC_PORT);
    }

    value = configs.get("consoleproxy.sslEnabled");
    if (value != null && value.equalsIgnoreCase("true")) {
      _sslEnabled = true;
    }

    _instance = configs.get("instance.name");

    _consoleProxyUrlDomain = configs.get("consoleproxy.url.domain");

    _listener = new ConsoleProxyListener(this);
    _agentMgr.registerForHostEvents(_listener, true, true, false);

    _itMgr.registerGuru(VirtualMachine.Type.ConsoleProxy, this);

    if (s_logger.isInfoEnabled()) {
      s_logger.info(
          "AgentBasedConsoleProxyManager has been configured. SSL enabled: " + _sslEnabled);
    }
    return true;
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    ConfigurationDao dao = ComponentLocator.getCurrentLocator().getDao(ConfigurationDao.class);
    _params = dao.getConfiguration(params);
    _name = name;

    return true;
  }
  public void init() {
    BaseCmd.setComponents(new ApiResponseHelper());
    BaseListCmd.configure();

    _systemAccount = _accountMgr.getSystemAccount();
    _systemUser = _accountMgr.getSystemUser();
    _dispatcher = ApiDispatcher.getInstance();

    Integer apiPort = null; // api port, null by default
    ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    SearchCriteria<ConfigurationVO> sc = configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, "integration.api.port");
    List<ConfigurationVO> values = configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Set<Class<?>> cmdClasses =
        ReflectUtil.getClassesWithAnnotation(
            APICommand.class, new String[] {"org.apache.cloudstack.api", "com.cloud.api"});

    for (Class<?> cmdClass : cmdClasses) {
      String apiName = cmdClass.getAnnotation(APICommand.class).name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }
  }
 @Override
 public void execute()
     throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
         ConcurrentOperationException, ResourceAllocationException {
   try {
     ExternalNetworkDeviceManager nwDeviceMgr;
     ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name);
     nwDeviceMgr = locator.getManager(ExternalNetworkDeviceManager.class);
     Host device = nwDeviceMgr.addNetworkDevice(this);
     NetworkDeviceResponse response = nwDeviceMgr.getApiResponse(device);
     response.setObjectName("networkdevice");
     response.setResponseName(getCommandName());
     this.setResponseObject(response);
   } catch (InvalidParameterValueException ipve) {
     throw new ServerApiException(BaseCmd.PARAM_ERROR, ipve.getMessage());
   } catch (CloudRuntimeException cre) {
     throw new ServerApiException(BaseCmd.INTERNAL_ERROR, cre.getMessage());
   }
 }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();

    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      s_logger.error("Unable to get the configuration dao. " + ConfigurationDao.class.getName());
      return false;
    }
    _snapshotPollInterval = NumbersUtil.parseInt(configDao.getValue("snapshot.poll.interval"), 300);
    boolean snapshotsRecurringTest =
        Boolean.parseBoolean(configDao.getValue("snapshot.recurring.test"));
    if (snapshotsRecurringTest) {
      // look for some test values in the configuration table so that snapshots can be taken more
      // frequently (QA test code)
      int minutesPerHour =
          NumbersUtil.parseInt(configDao.getValue("snapshot.test.minutes.per.hour"), 60);
      int hoursPerDay = NumbersUtil.parseInt(configDao.getValue("snapshot.test.hours.per.day"), 24);
      int daysPerWeek = NumbersUtil.parseInt(configDao.getValue("snapshot.test.days.per.week"), 7);
      int daysPerMonth =
          NumbersUtil.parseInt(configDao.getValue("snapshot.test.days.per.month"), 30);
      int weeksPerMonth =
          NumbersUtil.parseInt(configDao.getValue("snapshot.test.weeks.per.month"), 4);
      int monthsPerYear =
          NumbersUtil.parseInt(configDao.getValue("snapshot.test.months.per.year"), 12);

      _testTimerTask =
          new TestClock(
              this,
              minutesPerHour,
              hoursPerDay,
              daysPerWeek,
              daysPerMonth,
              weeksPerMonth,
              monthsPerYear);
    }
    _currentTimestamp = new Date();
    s_logger.info("Snapshot Scheduler is configured.");

    return true;
  }
 @Override
 public void init() throws ServletException {
   // Save Configuration Values
   // ComponentLocator loc = ComponentLocator.getLocator(ConfigurationServer.Name);
   ConfigurationServer c =
       (ConfigurationServer) ComponentLocator.getComponent(ConfigurationServer.Name);
   // ConfigurationServer c = new ConfigurationServerImpl();
   try {
     c.persistDefaultValues();
     s_locator = ComponentLocator.getLocator(ManagementServer.Name);
     ManagementServer ms = (ManagementServer) ComponentLocator.getComponent(ManagementServer.Name);
     ms.enableAdminUser("password");
     ApiServer.initApiServer(ms.getApiConfig());
   } catch (InvalidParameterValueException ipve) {
     s_logger.error("Exception starting management server ", ipve);
     throw new ServletException(ipve.getMessage());
   } catch (Exception e) {
     s_logger.error("Exception starting management server ", e);
     throw new ServletException(e.getMessage());
   }
 }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);

    ComponentLocator locator = ComponentLocator.getCurrentLocator();

    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    Map<String, String> dbParams = configDao.getConfiguration(params);

    _cidr = dbParams.get(Config.ControlCidr);
    if (_cidr == null) {
      _cidr = "169.254.0.0/16";
    }

    _gateway = dbParams.get(Config.ControlGateway);
    if (_gateway == null) {
      _gateway = NetUtils.getLinkLocalGateway();
    }

    s_logger.info("Control network setup: cidr=" + _cidr + "; gateway = " + _gateway);

    return true;
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;

    String value = null;

    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
      value = (String) params.get(StorageLayer.ClassConfigKey);
      if (value == null) {
        throw new ConfigurationException("Unable to find the storage layer");
      }

      Class<StorageLayer> clazz;
      try {
        clazz = (Class<StorageLayer>) Class.forName(value);
      } catch (ClassNotFoundException e) {
        throw new ConfigurationException("Unable to instantiate " + value);
      }
      _storage = ComponentLocator.inject(clazz);
    }
    String useSsl = (String) params.get("sslcopy");
    if (useSsl != null) {
      _sslCopy = Boolean.parseBoolean(useSsl);
    }
    configureFolders(name, params);
    String inSystemVM = (String) params.get("secondary.storage.vm");
    if (inSystemVM != null && "true".equalsIgnoreCase(inSystemVM)) {
      s_logger.info("UploadManager: starting additional services since we are inside system vm");
      startAdditionalServices();
      // blockOutgoingOnPrivate();
    }

    value = (String) params.get("install.timeout.pergig");
    this.installTimeoutPerGig = NumbersUtil.parseInt(value, 15 * 60) * 1000;

    value = (String) params.get("install.numthreads");
    final int numInstallThreads = NumbersUtil.parseInt(value, 10);

    String scriptsDir = (String) params.get("template.scripts.dir");
    if (scriptsDir == null) {
      scriptsDir = "scripts/storage/secondary";
    }

    // Add more processors here.
    threadPool = Executors.newFixedThreadPool(numInstallThreads);

    return true;
  }
  @Override
  public void execute()
      throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
          ConcurrentOperationException, ResourceAllocationException {
    // param checks
    if (snapshotReservation != null && (snapshotReservation < 0 || snapshotReservation > 100))
      throw new InvalidParameterValueException("Invalid snapshot reservation");

    ComponentLocator locator = ComponentLocator.getLocator(ManagementService.Name);
    NetappManager netappMgr = locator.getManager(NetappManager.class);

    StringBuilder s = new StringBuilder(getVolSize().toString());
    s.append("g");

    try {
      netappMgr.createVolumeOnFiler(
          ipAddress,
          aggrName,
          poolName,
          volName,
          s.toString(),
          snapshotPolicy,
          snapshotReservation,
          userName,
          password);
      CreateVolumeOnFilerCmdResponse response = new CreateVolumeOnFilerCmdResponse();
      response.setResponseName(getCommandName());
      this.setResponseObject(response);
    } catch (ServerException e) {
      throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.toString());
    } catch (InvalidParameterValueException e) {
      throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.toString());
    } catch (UnknownHostException e) {
      throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.toString());
    }
  }
Example #16
0
  protected StoragePoolDaoImpl() {
    AllFieldSearch = createSearchBuilder();
    AllFieldSearch.and("name", AllFieldSearch.entity().getName(), SearchCriteria.Op.EQ);
    AllFieldSearch.and("uuid", AllFieldSearch.entity().getUuid(), SearchCriteria.Op.EQ);
    AllFieldSearch.and(
        "datacenterId", AllFieldSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    AllFieldSearch.and(
        "hostAddress", AllFieldSearch.entity().getHostAddress(), SearchCriteria.Op.EQ);
    AllFieldSearch.and("status", AllFieldSearch.entity().getStatus(), SearchCriteria.Op.EQ);
    AllFieldSearch.and("path", AllFieldSearch.entity().getPath(), SearchCriteria.Op.EQ);
    AllFieldSearch.and("podId", AllFieldSearch.entity().getPodId(), Op.EQ);
    AllFieldSearch.done();

    DcPodSearch = createSearchBuilder();
    DcPodSearch.and("datacenterId", DcPodSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    DcPodSearch.and().op("nullpod", DcPodSearch.entity().getPodId(), SearchCriteria.Op.NULL);
    DcPodSearch.or("podId", DcPodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
    DcPodSearch.cp();
    DcPodSearch.and()
        .op("nullcluster", DcPodSearch.entity().getClusterId(), SearchCriteria.Op.NULL);
    DcPodSearch.or("cluster", DcPodSearch.entity().getClusterId(), SearchCriteria.Op.EQ);
    DcPodSearch.cp();
    DcPodSearch.done();

    DcPodAnyClusterSearch = createSearchBuilder();
    DcPodAnyClusterSearch.and(
        "datacenterId", DcPodAnyClusterSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    DcPodAnyClusterSearch.and()
        .op("nullpod", DcPodAnyClusterSearch.entity().getPodId(), SearchCriteria.Op.NULL);
    DcPodAnyClusterSearch.or(
        "podId", DcPodAnyClusterSearch.entity().getPodId(), SearchCriteria.Op.EQ);
    DcPodAnyClusterSearch.cp();
    DcPodAnyClusterSearch.done();

    DeleteLvmSearch = createSearchBuilder();
    DeleteLvmSearch.and("ids", DeleteLvmSearch.entity().getId(), SearchCriteria.Op.IN);
    DeleteLvmSearch.and().op("LVM", DeleteLvmSearch.entity().getPoolType(), SearchCriteria.Op.EQ);
    DeleteLvmSearch.or("Filesystem", DeleteLvmSearch.entity().getPoolType(), SearchCriteria.Op.EQ);
    DeleteLvmSearch.cp();
    DeleteLvmSearch.done();

    StatusCountSearch = createSearchBuilder(Long.class);
    StatusCountSearch.and("status", StatusCountSearch.entity().getStatus(), SearchCriteria.Op.IN);
    StatusCountSearch.select(null, Func.COUNT, null);
    StatusCountSearch.done();

    _detailsDao = ComponentLocator.inject(StoragePoolDetailsDaoImpl.class);
  }
  public void test2214to30Upgrade() throws SQLException {
    s_logger.debug("Finding sample data from 2.2.14");
    DbTestUtils.executeScript("fake.sql", false, true);

    DatabaseUpgradeChecker checker = ComponentLocator.inject(DatabaseUpgradeChecker.class);

    checker.upgrade("2.2.14", "3.0.0");

    Connection conn = Transaction.getStandaloneConnection();

    try {
      checkSystemVm(conn);
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
      }
    }
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    if (s_logger.isInfoEnabled()) {
      s_logger.info("Start configuring secondary storage vm manager : " + name);
    }

    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      throw new ConfigurationException("Unable to get the configuration dao.");
    }

    Map<String, String> configs = configDao.getConfiguration("management-server", params);

    _secStorageVmRamSize =
        NumbersUtil.parseInt(configs.get("secstorage.vm.ram.size"), DEFAULT_SS_VM_RAMSIZE);
    _secStorageVmCpuMHz =
        NumbersUtil.parseInt(configs.get("secstorage.vm.cpu.mhz"), DEFAULT_SS_VM_CPUMHZ);
    String useServiceVM = configDao.getValue("secondary.storage.vm");
    boolean _useServiceVM = false;
    if ("true".equalsIgnoreCase(useServiceVM)) {
      _useServiceVM = true;
    }

    String sslcopy = configDao.getValue("secstorage.encrypt.copy");
    if ("true".equalsIgnoreCase(sslcopy)) {
      _useSSlCopy = true;
    }

    _allowedInternalSites = configDao.getValue("secstorage.allowed.internal.sites");

    String value = configs.get("secstorage.capacityscan.interval");
    _capacityScanInterval = NumbersUtil.parseLong(value, DEFAULT_CAPACITY_SCAN_INTERVAL);

    _instance = configs.get("instance.name");
    if (_instance == null) {
      _instance = "DEFAULT";
    }

    Map<String, String> agentMgrConfigs = configDao.getConfiguration("AgentManager", params);
    _mgmt_host = agentMgrConfigs.get("host");
    if (_mgmt_host == null) {
      s_logger.warn(
          "Critical warning! Please configure your management server host address right after you have started your management server and then restart it, otherwise you won't have access to secondary storage");
    }

    value = agentMgrConfigs.get("port");
    _mgmt_port = NumbersUtil.parseInt(value, 8250);

    _listener = new SecondaryStorageListener(this, _agentMgr);
    _agentMgr.registerForHostEvents(_listener, true, false, true);

    _itMgr.registerGuru(VirtualMachine.Type.SecondaryStorageVm, this);

    _useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));
    _serviceOffering =
        new ServiceOfferingVO(
            "System Offering For Secondary Storage VM",
            1,
            _secStorageVmRamSize,
            _secStorageVmCpuMHz,
            null,
            null,
            false,
            null,
            _useLocalStorage,
            true,
            null,
            true,
            VirtualMachine.Type.SecondaryStorageVm,
            true);
    _serviceOffering.setUniqueName("Cloud.com-SecondaryStorage");
    _serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering);

    // this can sometimes happen, if DB is manually or programmatically manipulated
    if (_serviceOffering == null) {
      String msg =
          "Data integrity problem : System Offering For Secondary Storage VM has been removed?";
      s_logger.error(msg);
      throw new ConfigurationException(msg);
    }

    if (_useServiceVM) {
      _loadScanner = new SystemVmLoadScanner<Long>(this);
      _loadScanner.initScan(STARTUP_DELAY, _capacityScanInterval);
    }
    if (s_logger.isInfoEnabled()) {
      s_logger.info("Secondary storage vm Manager is configured.");
    }
    return true;
  }
Example #19
0
@Local(value = NetworkDao.class)
@DB(txn = false)
public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements NetworkDao {
  final SearchBuilder<NetworkVO> AllFieldsSearch;
  final SearchBuilder<NetworkVO> AccountSearch;
  final SearchBuilder<NetworkVO> RelatedConfigSearch;
  final SearchBuilder<NetworkVO> AccountNetworkSearch;
  final SearchBuilder<NetworkVO> ZoneBroadcastUriSearch;
  final SearchBuilder<NetworkVO> ZoneSecurityGroupSearch;
  final GenericSearchBuilder<NetworkVO, Long> CountByOfferingId;
  final SearchBuilder<NetworkVO> PhysicalNetworkSearch;
  final SearchBuilder<NetworkVO> securityGroupSearch;

  NetworkAccountDaoImpl _accountsDao = ComponentLocator.inject(NetworkAccountDaoImpl.class);
  NetworkDomainDaoImpl _domainsDao = ComponentLocator.inject(NetworkDomainDaoImpl.class);
  NetworkOpDaoImpl _opDao = ComponentLocator.inject(NetworkOpDaoImpl.class);
  NetworkServiceMapDaoImpl _ntwkSvcMap = ComponentLocator.inject(NetworkServiceMapDaoImpl.class);

  final TableGenerator _tgMacAddress;
  Random _rand = new Random(System.currentTimeMillis());
  long _prefix = 0x2;

  protected NetworkDaoImpl() {
    super();

    AllFieldsSearch = createSearchBuilder();
    AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
    AllFieldsSearch.and("cidr", AllFieldsSearch.entity().getCidr(), Op.EQ);
    AllFieldsSearch.and("broadcastType", AllFieldsSearch.entity().getBroadcastDomainType(), Op.EQ);
    AllFieldsSearch.and("offering", AllFieldsSearch.entity().getNetworkOfferingId(), Op.EQ);
    AllFieldsSearch.and("datacenter", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
    AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
    AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
    AllFieldsSearch.and("guestType", AllFieldsSearch.entity().getGuestType(), Op.EQ);
    AllFieldsSearch.and("physicalNetwork", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
    AllFieldsSearch.done();

    AccountSearch = createSearchBuilder();
    AccountSearch.and("offering", AccountSearch.entity().getNetworkOfferingId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> join = _accountsDao.createSearchBuilder();
    join.and("account", join.entity().getAccountId(), Op.EQ);
    AccountSearch.join(
        "accounts",
        join,
        AccountSearch.entity().getId(),
        join.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    AccountSearch.and("datacenter", AccountSearch.entity().getDataCenterId(), Op.EQ);
    AccountSearch.and("cidr", AccountSearch.entity().getCidr(), Op.EQ);
    AccountSearch.done();

    RelatedConfigSearch = createSearchBuilder();
    RelatedConfigSearch.and("offering", RelatedConfigSearch.entity().getNetworkOfferingId(), Op.EQ);
    RelatedConfigSearch.and("datacenter", RelatedConfigSearch.entity().getDataCenterId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> join2 = _accountsDao.createSearchBuilder();
    join2.and("account", join2.entity().getAccountId(), Op.EQ);
    RelatedConfigSearch.join(
        "account",
        join2,
        join2.entity().getNetworkId(),
        RelatedConfigSearch.entity().getId(),
        JoinType.INNER);
    RelatedConfigSearch.done();

    AccountNetworkSearch = createSearchBuilder();
    AccountNetworkSearch.and("networkId", AccountNetworkSearch.entity().getId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> mapJoin = _accountsDao.createSearchBuilder();
    mapJoin.and("accountId", mapJoin.entity().getAccountId(), Op.EQ);
    AccountNetworkSearch.join(
        "networkSearch",
        mapJoin,
        AccountNetworkSearch.entity().getId(),
        mapJoin.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    AccountNetworkSearch.done();

    ZoneBroadcastUriSearch = createSearchBuilder();
    ZoneBroadcastUriSearch.and(
        "dataCenterId", ZoneBroadcastUriSearch.entity().getDataCenterId(), Op.EQ);
    ZoneBroadcastUriSearch.and(
        "broadcastUri", ZoneBroadcastUriSearch.entity().getBroadcastUri(), Op.EQ);
    ZoneBroadcastUriSearch.done();

    ZoneSecurityGroupSearch = createSearchBuilder();
    ZoneSecurityGroupSearch.and(
        "dataCenterId", ZoneSecurityGroupSearch.entity().getDataCenterId(), Op.EQ);
    SearchBuilder<NetworkServiceMapVO> join1 = _ntwkSvcMap.createSearchBuilder();
    join1.and("service", join1.entity().getService(), Op.EQ);
    ZoneSecurityGroupSearch.join(
        "services",
        join1,
        ZoneSecurityGroupSearch.entity().getId(),
        join1.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    ZoneSecurityGroupSearch.done();

    CountByOfferingId = createSearchBuilder(Long.class);
    CountByOfferingId.select(null, Func.COUNT, CountByOfferingId.entity().getId());
    CountByOfferingId.and("offeringId", CountByOfferingId.entity().getNetworkOfferingId(), Op.EQ);
    CountByOfferingId.and("removed", CountByOfferingId.entity().getRemoved(), Op.NULL);
    CountByOfferingId.done();

    PhysicalNetworkSearch = createSearchBuilder();
    PhysicalNetworkSearch.and(
        "physicalNetworkId", PhysicalNetworkSearch.entity().getPhysicalNetworkId(), Op.EQ);
    PhysicalNetworkSearch.done();

    securityGroupSearch = createSearchBuilder();
    SearchBuilder<NetworkServiceMapVO> join3 = _ntwkSvcMap.createSearchBuilder();
    join3.and("service", join3.entity().getService(), Op.EQ);
    securityGroupSearch.join(
        "services",
        join3,
        securityGroupSearch.entity().getId(),
        join3.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    securityGroupSearch.done();

    _tgMacAddress = _tgs.get("macAddress");
  }

  @Override
  public List<NetworkVO> listBy(long accountId, long dataCenterId, Network.GuestType type) {
    SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
    sc.setParameters("datacenter", dataCenterId);
    sc.setParameters("account", accountId);
    if (type != null) {
      sc.setParameters("guestType", type);
    }
    return listBy(sc, null);
  }

  public List<NetworkVO> findBy(
      TrafficType trafficType,
      Mode mode,
      BroadcastDomainType broadcastType,
      long networkOfferingId,
      long dataCenterId) {
    SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
    sc.setParameters("trafficType", trafficType);
    sc.setParameters("broadcastType", broadcastType);
    sc.setParameters("offering", networkOfferingId);
    sc.setParameters("datacenter", dataCenterId);

    return search(sc, null);
  }

  @Override
  public List<NetworkVO> listBy(long accountId, long offeringId, long dataCenterId) {
    SearchCriteria<NetworkVO> sc = AccountSearch.create();
    sc.setParameters("offering", offeringId);
    sc.setJoinParameters("accounts", "account", accountId);
    sc.setParameters("datacenter", dataCenterId);

    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listBy(long accountId, long offeringId, long dataCenterId, String cidr) {
    SearchCriteria<NetworkVO> sc = AccountSearch.create();
    sc.setParameters("offering", offeringId);
    sc.setJoinParameters("accounts", "account", accountId);
    sc.setParameters("datacenter", dataCenterId);
    sc.setParameters("cidr", cidr);

    return listBy(sc);
  }

  @Override
  @DB
  public NetworkVO persist(NetworkVO network, boolean gc, Map<String, String> serviceProviderMap) {
    Transaction txn = Transaction.currentTxn();
    txn.start();

    // 1) create network
    NetworkVO newNetwork = super.persist(network);
    // 2) add account to the network
    addAccountToNetwork(network.getId(), network.getAccountId(), true);
    // 3) add network to gc monitor table
    NetworkOpVO op = new NetworkOpVO(network.getId(), gc);
    _opDao.persist(op);
    // 4) add services/providers for the network
    persistNetworkServiceProviders(newNetwork.getId(), serviceProviderMap);

    txn.commit();
    return newNetwork;
  }

  @Override
  @DB
  public boolean update(Long networkId, NetworkVO network, Map<String, String> serviceProviderMap) {
    Transaction txn = Transaction.currentTxn();
    txn.start();

    super.update(networkId, network);
    if (serviceProviderMap != null) {
      _ntwkSvcMap.deleteByNetworkId(networkId);
      persistNetworkServiceProviders(networkId, serviceProviderMap);
    }

    txn.commit();
    return true;
  }

  @Override
  @DB
  public void persistNetworkServiceProviders(
      long networkId, Map<String, String> serviceProviderMap) {
    Transaction txn = Transaction.currentTxn();
    txn.start();
    for (String service : serviceProviderMap.keySet()) {
      NetworkServiceMapVO serviceMap =
          new NetworkServiceMapVO(
              networkId,
              Service.getService(service),
              Provider.getProvider(serviceProviderMap.get(service)));
      _ntwkSvcMap.persist(serviceMap);
    }
    txn.commit();
  }

  protected void addAccountToNetwork(long networkId, long accountId, boolean isOwner) {
    NetworkAccountVO account = new NetworkAccountVO(networkId, accountId, isOwner);
    _accountsDao.persist(account);
  }

  @Override
  public SearchBuilder<NetworkAccountVO> createSearchBuilderForAccount() {
    return _accountsDao.createSearchBuilder();
  }

  @Override
  public List<NetworkVO> getNetworksForOffering(
      long offeringId, long dataCenterId, long accountId) {
    SearchCriteria<NetworkVO> sc = RelatedConfigSearch.create();
    sc.setParameters("offering", offeringId);
    sc.setParameters("dc", dataCenterId);
    sc.setJoinParameters("account", "account", accountId);
    return search(sc, null);
  }

  @Override
  public String getNextAvailableMacAddress(long networkConfigId) {
    SequenceFetcher fetch = SequenceFetcher.getInstance();

    long seq = fetch.getNextSequence(Long.class, _tgMacAddress, networkConfigId);
    seq = seq | _prefix << 40 | ((_rand.nextInt(Short.MAX_VALUE) << 16) & 0x00000000ffff0000l);
    return NetUtils.long2Mac(seq);
  }

  @Override
  public List<NetworkVO> listBy(long accountId, long networkId) {
    SearchCriteria<NetworkVO> sc = AccountNetworkSearch.create();
    sc.setParameters("networkId", networkId);
    sc.setJoinParameters("networkSearch", "accountId", accountId);
    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listBy(long zoneId, String broadcastUri) {
    SearchCriteria<NetworkVO> sc = ZoneBroadcastUriSearch.create();
    sc.setParameters("dataCenterId", zoneId);
    sc.setParameters("broadcastUri", broadcastUri);
    return search(sc, null);
  }

  @Override
  public List<NetworkVO> listByZone(long zoneId) {
    SearchCriteria<NetworkVO> sc = ZoneBroadcastUriSearch.create();
    sc.setParameters("dataCenterId", zoneId);
    return search(sc, null);
  }

  @Override
  public List<NetworkVO> listByZoneSecurityGroup(Long zoneId) {
    SearchCriteria<NetworkVO> sc = ZoneSecurityGroupSearch.create();
    if (zoneId != null) {
      sc.setParameters("dataCenterId", zoneId);
    }
    sc.setJoinParameters("services", "service", Service.SecurityGroup.getName());
    return search(sc, null);
  }

  @Override
  public void changeActiveNicsBy(long networkId, int count) {
    _opDao.changeActiveNicsBy(networkId, count);
  }

  @Override
  public int getActiveNicsIn(long networkId) {
    return _opDao.getActiveNics(networkId);
  }

  @Override
  public List<Long> findNetworksToGarbageCollect() {
    return _opDao.getNetworksToGarbageCollect();
  }

  @Override
  public void clearCheckForGc(long networkId) {
    _opDao.clearCheckForGc(networkId);
  }

  @Override
  public List<NetworkVO> listByOwner(long ownerId) {
    SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
    sc.setParameters("account", ownerId);
    return listBy(sc);
  }

  @Override
  public void addDomainToNetwork(long networkId, long domainId) {
    addDomainToNetworknetwork(networkId, domainId);
  }

  protected void addDomainToNetworknetwork(long networkId, long domainId) {
    NetworkDomainVO domain = new NetworkDomainVO(networkId, domainId);
    _domainsDao.persist(domain);
  }

  @Override
  public Long getNetworkCountByOfferingId(long offeringId) {
    SearchCriteria<Long> sc = CountByOfferingId.create();
    sc.setParameters("offeringId", offeringId);
    List<Long> results = customSearch(sc, null);
    return results.get(0);
  }

  @Override
  public List<NetworkVO> listSecurityGroupEnabledNetworks() {
    SearchCriteria<NetworkVO> sc = securityGroupSearch.create();
    sc.setJoinParameters("services", "service", Service.SecurityGroup.getName());
    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listByPhysicalNetwork(long physicalNetworkId) {
    SearchCriteria<NetworkVO> sc = PhysicalNetworkSearch.create();
    sc.setParameters("physicalNetworkId", physicalNetworkId);
    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listByPhysicalNetworkTrafficType(
      long physicalNetworkId, TrafficType trafficType) {
    SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
    sc.setParameters("trafficType", trafficType);
    sc.setParameters("physicalNetworkId", physicalNetworkId);
    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listByPhysicalNetworkAndProvider(
      long physicalNetworkId, String providerName) {
    SearchBuilder<NetworkServiceMapVO> svcProviderMapSearch = _ntwkSvcMap.createSearchBuilder();
    NetworkServiceMapVO svcProviderEntry = svcProviderMapSearch.entity();
    svcProviderMapSearch.and(
        "Provider", svcProviderMapSearch.entity().getProvider(), SearchCriteria.Op.EQ);

    SearchBuilder<NetworkVO> networksSearch = createSearchBuilder();
    networksSearch.and("physicalNetworkId", networksSearch.entity().getPhysicalNetworkId(), Op.EQ);
    networksSearch.join(
        "svcProviderMapSearch",
        svcProviderMapSearch,
        networksSearch.entity().getId(),
        svcProviderEntry.getNetworkId(),
        JoinBuilder.JoinType.INNER);

    SearchCriteria<NetworkVO> sc = networksSearch.create();
    sc.setJoinParameters("svcProviderMapSearch", "Provider", providerName);
    sc.setParameters("physicalNetworkId", physicalNetworkId);

    return listBy(sc);
  }

  @Override
  public List<NetworkVO> listBy(
      long accountId, long dataCenterId, Network.GuestType type, TrafficType trafficType) {
    SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
    sc.setParameters("datacenter", dataCenterId);
    sc.setParameters("account", accountId);
    sc.setParameters("guestType", type);
    sc.setParameters("trafficType", trafficType);

    return listBy(sc, null);
  }
}
  private StatsCollector(Map<String, String> configs) {
    ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
    _agentMgr = locator.getManager(AgentManager.class);
    _userVmMgr = locator.getManager(UserVmManager.class);
    _hostDao = locator.getDao(HostDao.class);
    _userVmDao = locator.getDao(UserVmDao.class);
    _volsDao = locator.getDao(VolumeDao.class);
    _capacityDao = locator.getDao(CapacityDao.class);
    _storagePoolDao = locator.getDao(StoragePoolDao.class);
    _storageManager = locator.getManager(StorageManager.class);
    _storagePoolHostDao = locator.getDao(StoragePoolHostDao.class);

    _executor = Executors.newScheduledThreadPool(3, new NamedThreadFactory("StatsCollector"));

    hostStatsInterval = NumbersUtil.parseLong(configs.get("host.stats.interval"), 60000L);
    hostAndVmStatsInterval = NumbersUtil.parseLong(configs.get("vm.stats.interval"), 60000L);
    storageStatsInterval = NumbersUtil.parseLong(configs.get("storage.stats.interval"), 60000L);
    volumeStatsInterval = NumbersUtil.parseLong(configs.get("volume.stats.interval"), -1L);

    _executor.scheduleWithFixedDelay(
        new HostCollector(), 15000L, hostStatsInterval, TimeUnit.MILLISECONDS);
    _executor.scheduleWithFixedDelay(
        new VmStatsCollector(), 15000L, hostAndVmStatsInterval, TimeUnit.MILLISECONDS);
    _executor.scheduleWithFixedDelay(
        new StorageCollector(), 15000L, storageStatsInterval, TimeUnit.MILLISECONDS);

    // -1 means we don't even start this thread to pick up any data.
    if (volumeStatsInterval > 0) {
      _executor.scheduleWithFixedDelay(
          new VolumeCollector(), 15000L, volumeStatsInterval, TimeUnit.MILLISECONDS);
    } else {
      s_logger.info("Disabling volume stats collector");
    }
  }
  @Override
  public boolean configure(final String name, final Map<String, Object> xmlParams)
      throws ConfigurationException {
    _name = name;
    ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);

    _serverId = ((ManagementServer) ComponentLocator.getComponent(ManagementServer.Name)).getId();

    _investigators = locator.getAdapters(Investigator.class);
    _fenceBuilders = locator.getAdapters(FenceBuilder.class);

    Map<String, String> params = new HashMap<String, String>();
    final ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao != null) {
      params = configDao.getConfiguration(Long.toHexString(_serverId), xmlParams);
    }

    String value = params.get("workers");
    final int count = NumbersUtil.parseInt(value, 1);
    _workers = new WorkerThread[count];
    for (int i = 0; i < _workers.length; i++) {
      _workers[i] = new WorkerThread("HA-Worker-" + i);
    }

    value = params.get("force.ha");
    _forceHA = Boolean.parseBoolean(value);

    value = params.get("time.to.sleep");
    _timeToSleep = NumbersUtil.parseInt(value, 60) * 1000;

    value = params.get("max.retries");
    _maxRetries = NumbersUtil.parseInt(value, 5);

    value = params.get("time.between.failures");
    _timeBetweenFailures = NumbersUtil.parseLong(value, 3600) * 1000;

    value = params.get("time.between.cleanup");
    _timeBetweenCleanups = NumbersUtil.parseLong(value, 3600 * 24);

    value = params.get("stop.retry.interval");
    _stopRetryInterval = NumbersUtil.parseInt(value, 10 * 60);

    value = params.get("restart.retry.interval");
    _restartRetryInterval = NumbersUtil.parseInt(value, 10 * 60);

    value = params.get("investigate.retry.interval");
    _investigateRetryInterval = NumbersUtil.parseInt(value, 1 * 60);

    value = params.get("migrate.retry.interval");
    _migrateRetryInterval = NumbersUtil.parseInt(value, 2 * 60);

    _instance = params.get("instance");
    if (_instance == null) {
      _instance = "VMOPS";
    }

    _haDao.releaseWorkItems(_serverId);

    _stopped = true;

    _executor = Executors.newScheduledThreadPool(count, new NamedThreadFactory("HA"));

    return true;
  }
  public void test217to22Upgrade() throws SQLException {
    s_logger.debug("Finding sample data from 2.1.7");
    DbTestUtils.executeScript("fake.sql", false, true);

    Connection conn;
    PreparedStatement pstmt;

    VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class);
    DatabaseUpgradeChecker checker = ComponentLocator.inject(DatabaseUpgradeChecker.class);

    String version = dao.getCurrentVersion();
    assert version.equals("2.1.7") : "Version returned is not 2.1.7 but " + version;

    checker.upgrade("2.1.7", "2.2.4");

    conn = Transaction.getStandaloneConnection();
    try {
      pstmt = conn.prepareStatement("SELECT version FROM version ORDER BY id DESC LIMIT 1");
      ResultSet rs = pstmt.executeQuery();
      assert rs.next() : "No version selected";
      assert rs.getString(1).equals("2.2.4") : "VERSION stored is not 2.2.4: " + rs.getString(1);
      rs.close();
      pstmt.close();

      pstmt = conn.prepareStatement("SELECT COUNT(*) FROM network_offerings");
      rs = pstmt.executeQuery();
      assert rs.next() : "Unable to get the count of network offerings.";
      assert (rs.getInt(1) == 7) : "Didn't find 7 network offerings but found " + rs.getInt(1);
      rs.close();
      pstmt.close();

      pstmt = conn.prepareStatement("SELECT DISTINCT networktype FROM data_center");
      rs = pstmt.executeQuery();
      assert rs.next() && rs.getString(1).equals("Advanced")
          : "Network type is not advanced? " + rs.getString(1);
      assert !rs.next() : "Why do we have another one? " + rs.getString(1);
      rs.close();
      pstmt.close();

      pstmt =
          conn.prepareStatement(
              "SELECT COUNT(*) FROM disk_offering WHERE removed IS NULL AND system_use=1 AND type='Service' AND recreatable=1");
      rs = pstmt.executeQuery();
      assert (rs.next() && rs.getInt(1) == 3)
          : "DiskOffering for system VMs are incorrect.  Expecting 3 but got " + rs.getInt(1);
      rs.close();
      pstmt.close();

      pstmt =
          conn.prepareStatement(
              "SELECT COUNT(*) FROM op_dc_link_local_ip_address_alloc WHERE nic_id IS NOT NULL");
      rs = pstmt.executeQuery();
      rs.next();
      int controlNics = rs.getInt(1);
      rs.close();
      pstmt.close();

      pstmt =
          conn.prepareStatement(
              "SELECT COUNT(*) FROM nics WHERE reserver_name='ControlNetworkGuru' and ip4_address is NOT NULL");
      rs = pstmt.executeQuery();
      assert (rs.next() && controlNics == rs.getInt(1))
          : "Allocated nics should have been " + controlNics + " but it is " + rs.getInt(1);
      rs.close();
      pstmt.close();

    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
      }
    }
  }
  @Override
  @Before
  public void setUp() throws Exception {
    _locator = new MockComponentLocator("management-server");
    _locator.addDao("StackMaidDao", StackMaidDaoImpl.class);
    _locator.addDao("VMTemplateZoneDao", VMTemplateZoneDaoImpl.class);
    _locator.addDao("DomainRouterDao", DomainRouterDaoImpl.class);
    _locator.addDao("HostDao", HostDaoImpl.class);
    _locator.addDao("VMInstanceDao", VMInstanceDaoImpl.class);
    _locator.addDao("UserVmDao", UserVmDaoImpl.class);
    ComponentInfo<? extends GenericDao<?, ? extends Serializable>> info =
        _locator.addDao("ServiceOfferingDao", ServiceOfferingDaoImpl.class);
    info.addParameter("cache.size", "50");
    info.addParameter("cache.time.to.live", "600");
    info = _locator.addDao("DiskOfferingDao", DiskOfferingDaoImpl.class);
    info.addParameter("cache.size", "50");
    info.addParameter("cache.time.to.live", "600");
    info = _locator.addDao("DataCenterDao", DataCenterDaoImpl.class);
    info.addParameter("cache.size", "50");
    info.addParameter("cache.time.to.live", "600");
    info = _locator.addDao("HostPodDao", HostPodDaoImpl.class);
    info.addParameter("cache.size", "50");
    info.addParameter("cache.time.to.live", "600");
    _locator.addDao("IPAddressDao", IPAddressDaoImpl.class);
    info = _locator.addDao("VlanDao", VlanDaoImpl.class);
    info.addParameter("cache.size", "30");
    info.addParameter("cache.time.to.live", "3600");
    _locator.addDao("PodVlanMapDao", PodVlanMapDaoImpl.class);
    _locator.addDao("AccountVlanMapDao", AccountVlanMapDaoImpl.class);
    _locator.addDao("VolumeDao", VolumeDaoImpl.class);
    _locator.addDao("EventDao", EventDaoImpl.class);
    info = _locator.addDao("UserDao", UserDaoImpl.class);
    info.addParameter("cache.size", "5000");
    info.addParameter("cache.time.to.live", "300");
    _locator.addDao("UserStatisticsDao", UserStatisticsDaoImpl.class);
    _locator.addDao("FirewallRulesDao", FirewallRulesDaoImpl.class);
    _locator.addDao("LoadBalancerDao", LoadBalancerDaoImpl.class);
    _locator.addDao("NetworkRuleConfigDao", NetworkRuleConfigDaoImpl.class);
    _locator.addDao("LoadBalancerVMMapDao", LoadBalancerVMMapDaoImpl.class);
    _locator.addDao("DataCenterIpAddressDao", DataCenterIpAddressDaoImpl.class);
    _locator.addDao("SecurityGroupDao", SecurityGroupDaoImpl.class);
    // _locator.addDao("IngressRuleDao", IngressRuleDaoImpl.class);
    _locator.addDao("SecurityGroupVMMapDao", SecurityGroupVMMapDaoImpl.class);
    _locator.addDao("SecurityGroupRulesDao", SecurityGroupRulesDaoImpl.class);
    _locator.addDao("SecurityGroupWorkDao", SecurityGroupWorkDaoImpl.class);
    _locator.addDao("VmRulesetLogDao", VmRulesetLogDaoImpl.class);
    _locator.addDao("AlertDao", AlertDaoImpl.class);
    _locator.addDao("CapacityDao", CapacityDaoImpl.class);
    _locator.addDao("DomainDao", DomainDaoImpl.class);
    _locator.addDao("AccountDao", AccountDaoImpl.class);
    _locator.addDao("ResourceLimitDao", ResourceLimitDaoImpl.class);
    _locator.addDao("ResourceCountDao", ResourceCountDaoImpl.class);
    _locator.addDao("UserAccountDao", UserAccountDaoImpl.class);
    _locator.addDao("VMTemplateHostDao", VMTemplateHostDaoImpl.class);
    _locator.addDao("UploadDao", UploadDaoImpl.class);
    _locator.addDao("VMTemplatePoolDao", VMTemplatePoolDaoImpl.class);
    _locator.addDao("LaunchPermissionDao", LaunchPermissionDaoImpl.class);
    _locator.addDao("ConfigurationDao", ConfigurationDaoImpl.class);
    info = _locator.addDao("VMTemplateDao", VMTemplateDaoImpl.class);
    info.addParameter("cache.size", "100");
    info.addParameter("cache.time.to.live", "600");
    info.addParameter("routing.uniquename", "routing");
    _locator.addDao("HighAvailabilityDao", HighAvailabilityDaoImpl.class);
    _locator.addDao("ConsoleProxyDao", ConsoleProxyDaoImpl.class);
    _locator.addDao("SecondaryStorageVmDao", SecondaryStorageVmDaoImpl.class);
    _locator.addDao("ManagementServerHostDao", ManagementServerHostDaoImpl.class);
    _locator.addDao("AgentUpgradeDao", AgentUpgradeDaoImpl.class);
    _locator.addDao("SnapshotDao", SnapshotDaoImpl.class);
    _locator.addDao("AsyncJobDao", AsyncJobDaoImpl.class);
    _locator.addDao("SyncQueueDao", SyncQueueDaoImpl.class);
    _locator.addDao("SyncQueueItemDao", SyncQueueItemDaoImpl.class);
    _locator.addDao("GuestOSDao", GuestOSDaoImpl.class);
    _locator.addDao("GuestOSCategoryDao", GuestOSCategoryDaoImpl.class);
    _locator.addDao("StoragePoolDao", StoragePoolDaoImpl.class);
    _locator.addDao("StoragePoolHostDao", StoragePoolHostDaoImpl.class);
    _locator.addDao("DetailsDao", HostDetailsDaoImpl.class);
    _locator.addDao("SnapshotPolicyDao", SnapshotPolicyDaoImpl.class);
    _locator.addDao("SnapshotScheduleDao", SnapshotScheduleDaoImpl.class);
    _locator.addDao("ClusterDao", ClusterDaoImpl.class);
    _locator.addDao("CertificateDao", CertificateDaoImpl.class);
    _locator.addDao("NetworkConfigurationDao", NetworkDaoImpl.class);
    _locator.addDao("NetworkOfferingDao", NetworkOfferingDaoImpl.class);
    _locator.addDao("NicDao", NicDaoImpl.class);
    _locator.addDao("InstanceGroupDao", InstanceGroupDaoImpl.class);
    _locator.addDao("InstanceGroupVMMapDao", InstanceGroupVMMapDaoImpl.class);
    _locator.addDao("RemoteAccessVpnDao", RemoteAccessVpnDaoImpl.class);
    _locator.addDao("VpnUserDao", VpnUserDaoImpl.class);
    _locator.addDao("ItWorkDao", ItWorkDaoImpl.class);
    _locator.addDao("FirewallRulesDao", FirewallRulesDaoImpl.class);
    _locator.addDao("PortForwardingRulesDao", PortForwardingRulesDaoImpl.class);
    _locator.addDao("FirewallRulesCidrsDao", FirewallRulesCidrsDaoImpl.class);
    _locator.addDao("SSHKeyPairDao", SSHKeyPairDaoImpl.class);
    _locator.addDao("UsageEventDao", UsageEventDaoImpl.class);
    _locator.addDao("ClusterDetailsDao", ClusterDetailsDaoImpl.class);
    _locator.addDao("UserVmDetailsDao", UserVmDetailsDaoImpl.class);
    _locator.addDao("StoragePoolWorkDao", StoragePoolWorkDaoImpl.class);
    _locator.addDao("HostTagsDao", HostTagsDaoImpl.class);
    _locator.addDao("NetworkDomainDao", NetworkDomainDaoImpl.class);
    _locator.addDao("KeystoreDao", KeystoreDaoImpl.class);
    _locator.addDao("DcDetailsDao", DcDetailsDaoImpl.class);
    _locator.addDao("SwiftDao", SwiftDaoImpl.class);
    _locator.addDao("AgentTransferMapDao", HostTransferMapDaoImpl.class);
    _locator.addDao("ProjectDao", ProjectDaoImpl.class);
    _locator.addDao("InlineLoadBalancerNicMapDao", InlineLoadBalancerNicMapDaoImpl.class);
    _locator.addDao("ElasticLbVmMap", ElasticLbVmMapDaoImpl.class);
    _locator.addDao("ProjectsAccountDao", ProjectAccountDaoImpl.class);
    info = _locator.addDao("HypervisorCapabilitiesDao", HypervisorCapabilitiesDaoImpl.class);
    info.addParameter("cache.size", "100");
    info.addParameter("cache.time.to.live", "600");

    _locator.addManager("StackMaidManager", CheckPointManagerImpl.class);
    _locator.addManager("account manager", AccountManagerImpl.class);
    _locator.addManager("domain manager", DomainManagerImpl.class);
    _locator.addManager("resource limit manager", ResourceLimitManagerImpl.class);
    _locator.addManager("configuration manager", ConfigurationManagerImpl.class);
    _locator.addManager("network manager", NetworkManagerImpl.class);
    _locator.addManager("download manager", DownloadMonitorImpl.class);
    _locator.addManager("upload manager", UploadMonitorImpl.class);
    _locator.addManager("keystore manager", KeystoreManagerImpl.class);
    _locator.addManager("secondary storage vm manager", SecondaryStorageManagerImpl.class);
    _locator.addManager("vm manager", UserVmManagerImpl.class);
    _locator.addManager("upgrade manager", UpgradeManagerImpl.class);
    _locator.addManager("StorageManager", StorageManagerImpl.class);
    _locator.addManager("SyncQueueManager", SyncQueueManagerImpl.class);
    _locator.addManager("AsyncJobManager", AsyncJobManagerImpl.class);
    _locator.addManager("AsyncJobExecutorContext", AsyncJobExecutorContextImpl.class);
    _locator.addManager("HA Manager", HighAvailabilityManagerImpl.class);
    _locator.addManager("Alert Manager", AlertManagerImpl.class);
    _locator.addManager("Template Manager", TemplateManagerImpl.class);
    _locator.addManager("Snapshot Manager", SnapshotManagerImpl.class);
    _locator.addManager("SnapshotScheduler", SnapshotSchedulerImpl.class);
    _locator.addManager("SecurityGroupManager", SecurityGroupManagerImpl2.class);
    _locator.addManager("DomainRouterManager", VirtualNetworkApplianceManagerImpl.class);
    _locator.addManager("EntityManager", EntityManagerImpl.class);
    _locator.addManager("LoadBalancingRulesManager", LoadBalancingRulesManagerImpl.class);
    _locator.addManager("RulesManager", RulesManagerImpl.class);
    _locator.addManager("RemoteAccessVpnManager", RemoteAccessVpnManagerImpl.class);
    _locator.addManager("OvsTunnelManager", OvsTunnelManagerImpl.class);
    _locator.addManager("Capacity Manager", CapacityManagerImpl.class);
    _locator.addManager("Cluster Manager", ClusterManagerImpl.class);
    _locator.addManager("VirtualMachineManager", ClusteredVirtualMachineManagerImpl.class);
    _locator.addManager("HypervisorGuruManager", HypervisorGuruManagerImpl.class);
    _locator.addManager("ClusterFenceManager", ClusterFenceManagerImpl.class);
    _locator.addManager("ResourceManager", ResourceManagerImpl.class);

    _locator.addManager("OCFS2Manager", OCFS2ManagerImpl.class);
    _locator.addManager("FirewallManager", FirewallManagerImpl.class);
    ComponentInfo<? extends Manager> info1 =
        _locator.addManager("ConsoleProxyManager", ConsoleProxyManagerImpl.class);
    info1.addParameter("consoleproxy.sslEnabled", "true");
    _locator.addManager("ClusteredAgentManager", ClusteredAgentManagerImpl.class);
    _locator.addManager("ProjectManager", ProjectManagerImpl.class);
    _locator.addManager("ElasticLoadBalancerManager", ElasticLoadBalancerManagerImpl.class);

    _locator.makeActive(null);
    _configService = ComponentLocator.inject(ConfigurationManagerImpl.class);
  }
Example #24
0
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      s_logger.error("Unable to get the configuration dao.");
      return false;
    }

    Map<String, String> configs = configDao.getConfiguration("management-server", params);

    // set up the email system for alerts
    String emailAddressList = configs.get("alert.email.addresses");
    String[] emailAddresses = null;
    if (emailAddressList != null) {
      emailAddresses = emailAddressList.split(",");
    }

    String smtpHost = configs.get("alert.smtp.host");
    int smtpPort = NumbersUtil.parseInt(configs.get("alert.smtp.port"), 25);
    String useAuthStr = configs.get("alert.smtp.useAuth");
    boolean useAuth = ((useAuthStr == null) ? false : Boolean.parseBoolean(useAuthStr));
    String smtpUsername = configs.get("alert.smtp.username");
    String smtpPassword = configs.get("alert.smtp.password");
    String emailSender = configs.get("alert.email.sender");
    String smtpDebugStr = configs.get("alert.smtp.debug");
    boolean smtpDebug = false;
    if (smtpDebugStr != null) {
      smtpDebug = Boolean.parseBoolean(smtpDebugStr);
    }

    _emailAlert =
        new EmailAlert(
            emailAddresses,
            smtpHost,
            smtpPort,
            useAuth,
            smtpUsername,
            smtpPassword,
            emailSender,
            smtpDebug);

    String storageCapacityThreshold = _configDao.getValue(Config.StorageCapacityThreshold.key());
    String cpuCapacityThreshold = _configDao.getValue(Config.CPUCapacityThreshold.key());
    String memoryCapacityThreshold = _configDao.getValue(Config.MemoryCapacityThreshold.key());
    String storageAllocCapacityThreshold =
        _configDao.getValue(Config.StorageAllocatedCapacityThreshold.key());
    String publicIPCapacityThreshold = _configDao.getValue(Config.PublicIpCapacityThreshold.key());
    String privateIPCapacityThreshold =
        _configDao.getValue(Config.PrivateIpCapacityThreshold.key());
    String secondaryStorageCapacityThreshold =
        _configDao.getValue(Config.SecondaryStorageCapacityThreshold.key());
    String vlanCapacityThreshold = _configDao.getValue(Config.VlanCapacityThreshold.key());
    String directNetworkPublicIpCapacityThreshold =
        _configDao.getValue(Config.DirectNetworkPublicIpCapacityThreshold.key());
    String localStorageCapacityThreshold =
        _configDao.getValue(Config.LocalStorageCapacityThreshold.key());

    if (storageCapacityThreshold != null) {
      _storageCapacityThreshold = Double.parseDouble(storageCapacityThreshold);
    }
    if (storageAllocCapacityThreshold != null) {
      _storageAllocCapacityThreshold = Double.parseDouble(storageAllocCapacityThreshold);
    }
    if (cpuCapacityThreshold != null) {
      _cpuCapacityThreshold = Double.parseDouble(cpuCapacityThreshold);
    }
    if (memoryCapacityThreshold != null) {
      _memoryCapacityThreshold = Double.parseDouble(memoryCapacityThreshold);
    }
    if (publicIPCapacityThreshold != null) {
      _publicIPCapacityThreshold = Double.parseDouble(publicIPCapacityThreshold);
    }
    if (privateIPCapacityThreshold != null) {
      _privateIPCapacityThreshold = Double.parseDouble(privateIPCapacityThreshold);
    }
    if (secondaryStorageCapacityThreshold != null) {
      _secondaryStorageCapacityThreshold = Double.parseDouble(secondaryStorageCapacityThreshold);
    }
    if (vlanCapacityThreshold != null) {
      _vlanCapacityThreshold = Double.parseDouble(vlanCapacityThreshold);
    }
    if (directNetworkPublicIpCapacityThreshold != null) {
      _directNetworkPublicIpCapacityThreshold =
          Double.parseDouble(directNetworkPublicIpCapacityThreshold);
    }
    if (localStorageCapacityThreshold != null) {
      _localStorageCapacityThreshold = Double.parseDouble(localStorageCapacityThreshold);
    }

    _capacityTypeThresholdMap.put(Capacity.CAPACITY_TYPE_STORAGE, _storageCapacityThreshold);
    _capacityTypeThresholdMap.put(
        Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, _storageAllocCapacityThreshold);
    _capacityTypeThresholdMap.put(Capacity.CAPACITY_TYPE_CPU, _cpuCapacityThreshold);
    _capacityTypeThresholdMap.put(Capacity.CAPACITY_TYPE_MEMORY, _memoryCapacityThreshold);
    _capacityTypeThresholdMap.put(
        Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP, _publicIPCapacityThreshold);
    _capacityTypeThresholdMap.put(Capacity.CAPACITY_TYPE_PRIVATE_IP, _privateIPCapacityThreshold);
    _capacityTypeThresholdMap.put(
        Capacity.CAPACITY_TYPE_SECONDARY_STORAGE, _secondaryStorageCapacityThreshold);
    _capacityTypeThresholdMap.put(Capacity.CAPACITY_TYPE_VLAN, _vlanCapacityThreshold);
    _capacityTypeThresholdMap.put(
        Capacity.CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP, _directNetworkPublicIpCapacityThreshold);
    _capacityTypeThresholdMap.put(
        Capacity.CAPACITY_TYPE_LOCAL_STORAGE, _localStorageCapacityThreshold);

    String capacityCheckPeriodStr = configs.get("capacity.check.period");
    if (capacityCheckPeriodStr != null) {
      _capacityCheckPeriod = Long.parseLong(capacityCheckPeriodStr);
      if (_capacityCheckPeriod <= 0)
        _capacityCheckPeriod = Long.parseLong(Config.CapacityCheckPeriod.getDefaultValue());
    }

    String cpuOverProvisioningFactorStr = configs.get("cpu.overprovisioning.factor");
    if (cpuOverProvisioningFactorStr != null) {
      _cpuOverProvisioningFactor = NumbersUtil.parseFloat(cpuOverProvisioningFactorStr, 1);
      if (_cpuOverProvisioningFactor < 1) {
        _cpuOverProvisioningFactor = 1;
      }
    }

    _timer = new Timer("CapacityChecker");

    return true;
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    s_logger.info("Configure VmwareManagerImpl, manager name: " + name);

    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      throw new ConfigurationException("Unable to get the configuration dao.");
    }

    if (!configDao.isPremium()) {
      s_logger.error("Vmware component can only run under premium distribution");
      throw new ConfigurationException("Vmware component can only run under premium distribution");
    }

    _instance = configDao.getValue(Config.InstanceName.key());
    if (_instance == null) {
      _instance = "DEFAULT";
    }
    s_logger.info("VmwareManagerImpl config - instance.name: " + _instance);

    _mountParent = configDao.getValue(Config.MountParent.key());
    if (_mountParent == null) {
      _mountParent = File.separator + "mnt";
    }

    if (_instance != null) {
      _mountParent = _mountParent + File.separator + _instance;
    }
    s_logger.info("VmwareManagerImpl config - _mountParent: " + _mountParent);

    String value = (String) params.get("scripts.timeout");
    _timeout = NumbersUtil.parseInt(value, 1440) * 1000;

    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
      value = (String) params.get(StorageLayer.ClassConfigKey);
      if (value == null) {
        value = "com.cloud.storage.JavaStorageLayer";
      }

      try {
        Class<?> clazz = Class.forName(value);
        _storage = (StorageLayer) ComponentLocator.inject(clazz);
        _storage.configure("StorageLayer", params);
      } catch (ClassNotFoundException e) {
        throw new ConfigurationException("Unable to find class " + value);
      }
    }

    _privateNetworkVSwitchName = configDao.getValue(Config.VmwarePrivateNetworkVSwitch.key());
    if (_privateNetworkVSwitchName == null) {
      _privateNetworkVSwitchName = "vSwitch0";
    }

    _publicNetworkVSwitchName = configDao.getValue(Config.VmwarePublicNetworkVSwitch.key());
    if (_publicNetworkVSwitchName == null) {
      _publicNetworkVSwitchName = "vSwitch0";
    }

    _guestNetworkVSwitchName = configDao.getValue(Config.VmwareGuestNetworkVSwitch.key());
    if (_guestNetworkVSwitchName == null) {
      _guestNetworkVSwitchName = "vSwitch0";
    }

    _serviceConsoleName = configDao.getValue(Config.VmwareServiceConsole.key());
    if (_serviceConsoleName == null) {
      _serviceConsoleName = "Service Console";
    }

    _managemetPortGroupName = configDao.getValue(Config.VmwareManagementPortGroup.key());
    if (_managemetPortGroupName == null) {
      _managemetPortGroupName = "Management Network";
    }

    configDao.getValue(Config.VmwareServiceConsole.key());
    _additionalPortRangeStart =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwareAdditionalVncPortRangeStart.key()), 59000);
    if (_additionalPortRangeStart > 65535) {
      s_logger.warn(
          "Invalid port range start port ("
              + _additionalPortRangeStart
              + ") for additional VNC port allocation, reset it to default start port 59000");
      _additionalPortRangeStart = 59000;
    }

    _additionalPortRangeSize =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwareAdditionalVncPortRangeSize.key()), 1000);
    if (_additionalPortRangeSize < 0
        || _additionalPortRangeStart + _additionalPortRangeSize > 65535) {
      s_logger.warn(
          "Invalid port range size ("
              + _additionalPortRangeSize
              + " for range starts at "
              + _additionalPortRangeStart);
      _additionalPortRangeSize = Math.min(1000, 65535 - _additionalPortRangeStart);
    }

    _maxHostsPerCluster =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwarePerClusterHostMax.key()),
            VmwareManager.MAX_HOSTS_PER_CLUSTER);
    _cpuOverprovisioningFactor = configDao.getValue(Config.CPUOverprovisioningFactor.key());
    if (_cpuOverprovisioningFactor == null || _cpuOverprovisioningFactor.isEmpty())
      _cpuOverprovisioningFactor = "1";

    _memOverprovisioningFactor = configDao.getValue(Config.MemOverprovisioningFactor.key());
    if (_memOverprovisioningFactor == null || _memOverprovisioningFactor.isEmpty())
      _memOverprovisioningFactor = "1";

    _reserveCpu = configDao.getValue(Config.VmwareReserveCpu.key());
    if (_reserveCpu == null || _reserveCpu.isEmpty()) _reserveCpu = "false";
    _reserveMem = configDao.getValue(Config.VmwareReserveMem.key());
    if (_reserveMem == null || _reserveMem.isEmpty()) _reserveMem = "false";

    s_logger.info(
        "Additional VNC port allocation range is settled at "
            + _additionalPortRangeStart
            + " to "
            + (_additionalPortRangeStart + _additionalPortRangeSize));

    value = configDao.getValue(Config.VmwareGuestNicDeviceType.key());
    if (value == null || "E1000".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.E1000;
    else if ("PCNet32".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.PCNet32;
    else if ("Vmxnet2".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.Vmxnet2;
    else if ("Vmxnet3".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.Vmxnet3;
    else this._guestNicDeviceType = VirtualEthernetCardType.E1000;

    value = configDao.getValue("vmware.host.scan.interval");
    _hostScanInterval = NumbersUtil.parseLong(value, DEFAULT_HOST_SCAN_INTERVAL);
    s_logger.info("VmwareManagerImpl config - vmware.host.scan.interval: " + _hostScanInterval);

    ((VmwareStorageManagerImpl) _storageMgr).configure(params);

    _agentMgr.registerForHostEvents(this, true, true, true);

    s_logger.info("VmwareManagerImpl has been successfully configured");
    return true;
  }
public class IPAddressUsageParser {
  public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName());

  private static ComponentLocator _locator =
      ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
  private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
  private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class);

  public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Parsing IP Address usage for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
      endDate = new Date();
    }

    // - query usage_ip_address table with the following criteria:
    //     - look for an entry for accountId with start date in the given range
    //     - look for an entry for accountId with end date in the given range
    //     - look for an entry for accountId with end date null (currently running vm or owned IP)
    //     - look for an entry for accountId with start date before given range *and* end date after
    // given range
    List<UsageIPAddressVO> usageIPAddress =
        m_usageIPAddressDao.getUsageRecords(
            account.getId(), account.getDomainId(), startDate, endDate);

    if (usageIPAddress.isEmpty()) {
      s_logger.debug("No IP Address usage for this period");
      return true;
    }

    // This map has both the running time *and* the usage amount.
    Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();

    Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>();

    // loop through all the usage IPs, create a usage record for each
    for (UsageIPAddressVO usageIp : usageIPAddress) {
      long IpId = usageIp.getId();

      String key = "" + IpId;

      // store the info in the IP map
      IPMap.put(
          key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat()));

      Date IpAssignDate = usageIp.getAssigned();
      Date IpReleaseDeleteDate = usageIp.getReleased();

      if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) {
        IpReleaseDeleteDate = endDate;
      }

      // clip the start date to the beginning of our aggregation range if the vm has been running
      // for a while
      if (IpAssignDate.before(startDate)) {
        IpAssignDate = startDate;
      }

      long currentDuration =
          (IpReleaseDeleteDate.getTime() - IpAssignDate.getTime())
              + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to
                   // find total number of millis to charge)

      updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration);
    }

    for (String ipIdKey : usageMap.keySet()) {
      Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey);
      long useTime = ipTimeInfo.second().longValue();

      // Only create a usage record if we have a runningTime of bigger than zero.
      if (useTime > 0L) {
        IpInfo info = IPMap.get(ipIdKey);
        createUsageRecord(
            info.getZoneId(),
            useTime,
            startDate,
            endDate,
            account,
            info.getIpId(),
            info.getIPAddress(),
            info.isSourceNat());
      }
    }

    return true;
  }

  private static void updateIpUsageData(
      Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) {
    Pair<Long, Long> ipUsageInfo = usageDataMap.get(key);
    if (ipUsageInfo == null) {
      ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration));
    } else {
      Long runningTime = ipUsageInfo.second();
      runningTime = new Long(runningTime.longValue() + duration);
      ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime);
    }
    usageDataMap.put(key, ipUsageInfo);
  }

  private static void createUsageRecord(
      long zoneId,
      long runningTime,
      Date startDate,
      Date endDate,
      AccountVO account,
      long IpId,
      String IPAddress,
      boolean isSourceNat) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Total usage time " + runningTime + "ms");
    }

    float usage = runningTime / 1000f / 60f / 60f;

    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);

    if (s_logger.isDebugEnabled()) {
      s_logger.debug(
          "Creating IP usage record with id: "
              + IpId
              + ", usage: "
              + usageDisplay
              + ", startDate: "
              + startDate
              + ", endDate: "
              + endDate
              + ", for account: "
              + account.getId());
    }

    String usageDesc = "IPAddress: " + IPAddress;

    // Create the usage record

    UsageVO usageRecord =
        new UsageVO(
            zoneId,
            account.getAccountId(),
            account.getDomainId(),
            usageDesc,
            usageDisplay + " Hrs",
            UsageTypes.IP_ADDRESS,
            new Double(usage),
            null,
            null,
            null,
            null,
            IpId,
            startDate,
            endDate,
            (isSourceNat ? "SourceNat" : ""));
    m_usageDao.persist(usageRecord);
  }

  private static class IpInfo {
    private long zoneId;
    private long IpId;
    private String IPAddress;
    private boolean isSourceNat;

    public IpInfo(long zoneId, long IpId, String IPAddress, boolean isSourceNat) {
      this.zoneId = zoneId;
      this.IpId = IpId;
      this.IPAddress = IPAddress;
      this.isSourceNat = isSourceNat;
    }

    public long getZoneId() {
      return zoneId;
    }

    public long getIpId() {
      return IpId;
    }

    public String getIPAddress() {
      return IPAddress;
    }

    public boolean isSourceNat() {
      return isSourceNat;
    }
  }
}