@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;
  }
  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(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;
  }
  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 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 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(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;
  }
  @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;
  }
  @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;
  }
  @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;
  }
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;
    }
  }
}