/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 imageRenderer;

import ilog.tgo.model.IltAlarm;
import ilog.tgo.model.IltLed;
import ilog.tgo.model.IltOSI;
import ilog.tgo.model.IltObject;
import ilog.tgo.model.IltObjectState;
import ilog.tgo.model.IltSNMP;

import ilog.cpl.model.AttributeValueEvent;
import ilog.cpl.model.AttributeValueListener;

/**
 * This class models an attribute value change listener. This listener
 * associates the state of a port to the state of a led. Whenever the
 * port changes state, the led state is modified accordingly to reflect
 * the new port state.
 */
public class PortStateListener implements AttributeValueListener {

  // Stores a copy of the LED
  private IltLed led;
  
  public PortStateListener(IltLed l) {
    led = l;
  }

  public void attributeValueChange(AttributeValueEvent ev) {
    if (ev.getAttribute() == IltObject.ObjectStateAttribute) {
      IltObjectState ostate = (IltObjectState) ev.getNewValue();
      // turn the led on only if state is "carrying traffic"
      if (ostate.has(IltOSI.State.Usage.Active)) {
        // check alarms
        if (ostate.getAlarmState() instanceof IltAlarm.State) {
          IltAlarm.State alarm = (IltAlarm.State)ostate.getAlarmState();
          if (alarm.getAlarmCount(IltAlarm.Severity.Major) > 0) {
            // turn the led yellow, there is at least one major alarm
            led.setState(IltSNMP.State.Failed);
          } else {
            // make it glow in green (no major alarms)
            led.setState(IltSNMP.State.Up);
          }
        }
      } else {
        // turn the led off (state is not "carrying traffic")
        led.setState(IltSNMP.State.Down);
      }
    }
  }
}