/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package monitoring.web;

import ilog.cpl.datasource.IlpDataSource;
import ilog.tgo.model.IltAlarm;
import monitoring.generator.AlarmGenerator;

/**
 * Implementation of an <code>AbstractSampleContext</code> targeted at an alarm
 * generation system based solely in TGO abstractions.
 */
public class SampleContext extends AbstractSampleContext {

  /**
   * Initializes the alarm generator which typically is specific to the
   * underlying OSS technology.
   */
  Override
  protected void initializeAlarmGenerator() {

    AlarmGenerator.initialize();

    alarmGenerator = new AlarmGenerator(getControls().getSharedControls().getAlarmGenerationRate(),
        getControls().getSharedControls().getMaxNumberOfAlarms(),
        getDataStructures().getDataSources().getNetworkDataSource(),
        getDataStructures().getDataSources().getAlarmsDataSource());
  }

  /**
   * Creates sample alarms to show at application startup.
   * 
   */
  Override
  protected void createSampleAlarms() {

    // Get alarm generator
    AlarmGenerator alarmGenerator = (AlarmGenerator) getAlarmGenerator();

    String[] managedObjectIds = { "RouterB", "NetE", "NetE", "NetI", "NetF" };

    IltAlarm.Severity[] severities = { IltAlarm.Severity.Minor, IltAlarm.Severity.Critical, IltAlarm.Severity.Major,
        IltAlarm.Severity.Minor, IltAlarm.Severity.Warning };

    // Save current values
    int originalMaxNumberOfAlarms = alarmGenerator.getMaxNumberOfAlarms();

    // Set temporary configuration to create the alarms quickly
    alarmGenerator.setMaxNumberOfAlarms(managedObjectIds.length);

    IlpDataSource dataSource = alarmGenerator.getMODataSource();

    for (int i = 0; i < managedObjectIds.length; i++) {
      alarmGenerator.generateOneAlarm(dataSource.getObject(managedObjectIds[i]), severities[i]);
    }

    // Restore original values
    alarmGenerator.setMaxNumberOfAlarms(originalMaxNumberOfAlarms);
  }

  Override
  protected String getSampleLoggerName() {
    return "samples.monitoring.web";
  }
}