AlarmImpl.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.monitor.engine.alarm;

  21. import java.sql.Connection;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.core.allarmi.Allarme;
  27. import org.openspcoop2.core.allarmi.AllarmeHistory;
  28. import org.openspcoop2.core.allarmi.constants.TipoAllarme;
  29. import org.openspcoop2.core.allarmi.dao.IServiceManager;
  30. import org.openspcoop2.core.allarmi.utils.ProjectInfo;
  31. import org.openspcoop2.core.commons.dao.DAOFactory;
  32. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  33. import org.openspcoop2.monitor.sdk.alarm.AlarmStatus;
  34. import org.openspcoop2.monitor.sdk.alarm.IAlarm;
  35. import org.openspcoop2.monitor.sdk.exceptions.AlarmException;
  36. import org.openspcoop2.monitor.sdk.exceptions.AlarmNotifyException;
  37. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  38. import org.openspcoop2.utils.LoggerWrapperFactory;
  39. import org.slf4j.Logger;

  40. /**
  41.  * AlarmImpl
  42.  *
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class AlarmImpl implements IAlarm {

  48.     private Logger _logger = LoggerWrapperFactory.getLogger(AlarmImpl.class);
  49.     private DAOFactory daoFactory = null;
  50.     private String threadName;
  51.     private String username;
  52.     private AlarmLogger alarmLogger;
  53.     private AlarmThreadStatus alarmThreadStatus;
  54.    
  55.     public AlarmImpl(Allarme configAllarme,Logger log, DAOFactory daoFactory) {
  56.         this.id = configAllarme.getNome();
  57.         this.nome = configAllarme.getAlias();
  58.         this.configAllarme = configAllarme;
  59.         this._logger = log;
  60.         this.daoFactory = daoFactory;
  61.         this.threadName = "SDK";
  62.         this.alarmLogger = new AlarmLogger(this.nome, this.id, this.threadName, this._logger,
  63.                 daoFactory!=null && daoFactory.getLog()!=null ? daoFactory.getLog() : this._logger);
  64.     }
  65.    
  66.     protected void setThreadName(String threadName) {
  67.         this.threadName = threadName;
  68.         this.alarmLogger.setThreadName(this.threadName);
  69.     }
  70.    
  71.     @Override
  72.     public AlarmThreadStatus getActiveThreadStatus() {
  73.         return this.alarmThreadStatus;
  74.     }
  75.     protected void setAlarmThreadStatus(AlarmThreadStatus alarmThreadStatus) {
  76.         this.alarmThreadStatus = alarmThreadStatus;
  77.     }
  78.    
  79.     public void setUsername(String username) {
  80.         this.username = username;
  81.     }
  82.    
  83.     @Override
  84.     public AlarmLogger getLogger(){
  85.         return this.alarmLogger;
  86.     }
  87.    
  88.     @Override
  89.     public DAOFactory getDAOFactory(){
  90.         return this.daoFactory;
  91.     }
  92.    
  93.     // Metodo utilizzato dal Gateway in AlarmNotifier
  94.     public void changeStatus(Logger logSql, Connection connection,  ServiceManagerProperties smp, AlarmStatus nuovoStatoAllarme) throws AlarmException, AlarmNotifyException {
  95.         this._changeStatus(logSql, connection, smp, nuovoStatoAllarme);
  96.     }
  97.    
  98.     @Override
  99.     public void changeStatus(AlarmStatus nuovoStatoAllarme) throws AlarmException, AlarmNotifyException {
  100.        
  101.         if(this.alarmThreadStatus!=null && !this.alarmThreadStatus.isRunning()) {
  102.             this.alarmLogger.info("Cambio di stato non effettuato, poichè è stato rilevato uno shutdown in corso");
  103.             return;
  104.         }
  105.        
  106.         this._changeStatus(null, null, null, nuovoStatoAllarme);
  107.     }
  108.    
  109.     private void _changeStatus(Logger logSql, Connection connection,  ServiceManagerProperties smp, AlarmStatus nuovoStatoAllarme) throws AlarmException, AlarmNotifyException {
  110.        
  111.         IServiceManager allarmiSM = null;
  112.         boolean changeEventFromGateway = false;
  113.         List<AllarmeHistory> repositoryHistory = null;
  114.         try {
  115.             if(connection==null) {
  116.                 allarmiSM = (IServiceManager) this.daoFactory.getServiceManager(ProjectInfo.getInstance());
  117.             }
  118.             else {
  119.                 allarmiSM = (IServiceManager) this.daoFactory.getServiceManager(ProjectInfo.getInstance(), connection, smp, logSql);
  120.                 changeEventFromGateway = true;
  121.                 repositoryHistory = new ArrayList<AllarmeHistory>();
  122.             }
  123.         }catch(Exception e) {
  124.             throw new AlarmException(e.getMessage(),e);
  125.         }
  126.        
  127.         // Cambio di stato effettivo ?
  128.         boolean statusChanged = false;
  129.         if(this.status==null || this.status.getStatus()==null){
  130.             statusChanged = true;
  131.         }
  132.         else{
  133.             statusChanged = !this.status.getStatus().equals(nuovoStatoAllarme.getStatus());
  134.         }
  135.        
  136.         // Cambio stato sul database degli allarmi
  137.         AlarmManager.changeStatus(nuovoStatoAllarme, this, allarmiSM, this.username, statusChanged, repositoryHistory);
  138.        
  139.        
  140.         // Switch stato nell'attuale implementazione
  141.         AlarmStatus oldStatus = null;
  142.         if(this.status!=null){
  143.             oldStatus = (AlarmStatus) this.status.clone();
  144.         }
  145.         this.setStatus(nuovoStatoAllarme);
  146.        
  147.         if(changeEventFromGateway) {
  148.            
  149.             this.alarmLogger.debug("Registrazione richiesta notifica su db in corso ...");
  150.             AlarmManager.registraNotifica(this.alarmLogger, this.configAllarme, oldStatus, nuovoStatoAllarme,
  151.                     !repositoryHistory.isEmpty() ? repositoryHistory.get(0) : null,
  152.                     allarmiSM);
  153.             this.alarmLogger.debug("Registrazione richiesta notifica su db terminata correttamente");
  154.            
  155.         }
  156.         else {
  157.        
  158.             AlarmEngineConfig alarmEngineConfig = AlarmManager.getAlarmEngineConfig();
  159.             if(alarmEngineConfig==null){
  160.                 throw new AlarmException("Configurazione Allarme non fornita, utilizzare il metodo AlarmManager.setAlarmEngineConfig(...)");
  161.             }
  162.            
  163.             this.alarmLogger.debug("Analisi nuovo stato in corso ...");
  164.             AlarmManager.notifyChangeStatus(alarmEngineConfig, this.configAllarme,
  165.                     this, this.pluginClassName, this.threadName,
  166.                     this.alarmLogger,
  167.                     oldStatus, nuovoStatoAllarme,
  168.                     (this.configAllarme!=null && TipoAllarme.ATTIVO.equals(this.configAllarme.getTipoAllarme()))
  169.                     );
  170.             this.alarmLogger.info("Analisi nuovo stato terminata correttamente");
  171.            
  172.         }
  173.        
  174.     }

  175.     public void setStatus(AlarmStatus statoAllarme) throws AlarmException {
  176.         this.status = statoAllarme;
  177.     }

  178.     @Override
  179.     public AlarmStatus getStatus() {
  180.         return this.status;
  181.     }

  182.     @Override
  183.     public String getId() {
  184.         return this.id;
  185.     }
  186.    
  187.     @Override
  188.     public String getNome() {
  189.         return this.nome;
  190.     }

  191.     @Override
  192.     public Allarme getConfigAllarme() {
  193.         return this.configAllarme;
  194.     }
  195.    
  196.     @Override
  197.     public Parameter<?> getParameter(String paramId) {
  198.         if (this.parameters != null)
  199.             return this.parameters.get(paramId);
  200.         else
  201.             return null;
  202.     }

  203.     @Override
  204.     public Map<String, Parameter<?>> getParameters() {
  205.         return this.parameters;
  206.     }
  207.    
  208.     protected void addParameter(Parameter<?> param) {
  209.         if (this.parameters == null)
  210.             this.parameters = new HashMap<String, Parameter<?>>();
  211.         this.parameters.put(param.getId(), param);
  212.     }
  213.    
  214.     public String getPluginClassName() {
  215.         return this.pluginClassName;
  216.     }
  217.    
  218.     protected void setPluginClassName(String plugin) {
  219.         this.pluginClassName = plugin;
  220.     }

  221.     @Override
  222.     public boolean isManuallyUpdateState() {
  223.         return this.manuallyUpdateState;
  224.     }
  225.    
  226.     public void setManuallyUpdateState(boolean manuallyUpdateState) {
  227.         this.manuallyUpdateState = manuallyUpdateState;
  228.     }
  229.    
  230.     @Override
  231.     public boolean isManuallyAckCriteria() {
  232.         return this.manuallyAckCriteria;
  233.     }
  234.    
  235.     public void setManuallyAckCriteria(boolean manuallyAckCriteria) {
  236.         this.manuallyAckCriteria = manuallyAckCriteria;
  237.     }

  238.     private String id = null;
  239.     private String nome = null;
  240.     private Allarme configAllarme;
  241.     private HashMap<String, Parameter<?>> parameters = null;
  242.     private AlarmStatus status = null;
  243.     private String pluginClassName = null;
  244.     private boolean manuallyUpdateState = false;
  245.     private boolean manuallyAckCriteria = false;
  246. }