ConfigurazioneAllarmiDel.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.web.ctrlstat.servlet.config;

  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;

  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.allarmi.Allarme;
  32. import org.openspcoop2.core.allarmi.constants.RuoloPorta;
  33. import org.openspcoop2.core.allarmi.constants.TipoAllarme;
  34. import org.openspcoop2.core.commons.Liste;
  35. import org.openspcoop2.message.constants.ServiceBinding;
  36. import org.openspcoop2.monitor.engine.alarm.AlarmEngineConfig;
  37. import org.openspcoop2.monitor.engine.alarm.utils.AllarmiUtils;
  38. import org.openspcoop2.monitor.engine.alarm.wrapper.ConfigurazioneAllarmeBean;
  39. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  40. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  41. import org.openspcoop2.web.ctrlstat.core.Utilities;
  42. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  43. import org.openspcoop2.web.lib.mvc.Costanti;
  44. import org.openspcoop2.web.lib.mvc.ForwardParams;
  45. import org.openspcoop2.web.lib.mvc.GeneralData;
  46. import org.openspcoop2.web.lib.mvc.PageData;
  47. import org.openspcoop2.web.lib.mvc.ServletUtils;

  48. /**
  49.  * ConfigurazioneAllarmiDel
  50.  *
  51.  * @author Giuliano Pintori (pintori@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  *
  55.  */
  56. public final class ConfigurazioneAllarmiDel extends Action {

  57.     @Override
  58.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  59.         HttpSession session = request.getSession(true);

  60.         // Inizializzo PageData
  61.         PageData pd = new PageData();

  62.         GeneralHelper generalHelper = new GeneralHelper(session);

  63.         // Inizializzo GeneralData
  64.         GeneralData gd = generalHelper.initGeneralData(request);

  65.         String userLogin = ServletUtils.getUserLoginFromSession(session);  

  66.         try {
  67.             ConfigurazioneHelper confHelper = new ConfigurazioneHelper(request, pd, session);
  68.            
  69.             String ruoloPortaParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_RUOLO_PORTA);
  70.             RuoloPorta ruoloPorta = null;
  71.             if(ruoloPortaParam!=null) {
  72.                 ruoloPorta = RuoloPorta.toEnumConstant(ruoloPortaParam);
  73.             }
  74.             String nomePorta = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_NOME_PORTA);
  75.             ServiceBinding serviceBinding = null;
  76.             String serviceBindingParam = confHelper.getParameter(ConfigurazioneCostanti.PARAMETRO_CONFIGURAZIONE_ALLARMI_SERVICE_BINDING);
  77.             if(serviceBindingParam!=null && !"".equals(serviceBindingParam)) {
  78.                 serviceBinding = ServiceBinding.valueOf(serviceBindingParam);
  79.             }

  80.             String objToRemove =confHelper.getParameter(Costanti.PARAMETER_NAME_OBJECTS_FOR_REMOVE);
  81.             ArrayList<String> idsToRemove = Utilities.parseIdsToRemove(objToRemove);
  82.             ConfigurazioneCore confCore = new ConfigurazioneCore();

  83.             Long id = null;

  84.             List<Allarme> allarmiToRemove = new ArrayList<>();
  85.             StringBuilder inUsoMessage = new StringBuilder();
  86.            
  87.             for (int i = 0; i < idsToRemove.size(); i++) {

  88.                 id = Long.parseLong(idsToRemove.get(i));
  89.                
  90.                 Allarme allarme = confCore.getAllarmeSenzaPlugin(id);
  91.                
  92.                 allarmiToRemove.add(allarme);
  93.                
  94.             }
  95.            
  96.             List<Allarme> allarmiRimossi = new ArrayList<Allarme>();
  97.             ConfigurazioneUtilities.deleteAllarmi(allarmiToRemove, confHelper, confCore, userLogin, inUsoMessage, org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE, allarmiRimossi);
  98.            
  99.             List<String> allarmiAttivi = new ArrayList<>();
  100.             if(!allarmiRimossi.isEmpty()) {
  101.            
  102.                 for (Allarme allarme : allarmiRimossi) {
  103.                                        
  104.                     if(TipoAllarme.PASSIVO.equals(allarme.getTipoAllarme())){
  105.                         // NOTA: il tipo di allarme non è modificabile.
  106.                         ControlStationCore.getLog().debug("Allarme ["+allarme.getNome()+"] è passivo. Non viene effettuata alcuna rimozione su Allarmi.war");
  107.                         continue;
  108.                     }
  109.                     if(allarme.getEnabled()==0){
  110.                         // NOTA: il tipo di allarme non è modificabile.
  111.                         ControlStationCore.getLog().debug("Allarme ["+allarme.getNome()+"] è disabilitato. Non viene effettuata alcuna rimozione su Allarmi.war");
  112.                         continue;
  113.                     }
  114.                        
  115.                     allarmiAttivi.add(allarme.getNome());
  116.                    
  117.                 }
  118.                  
  119.             }
  120.            
  121.             /* ******** INVIO NOTIFICHE *************** */
  122.             String pdMessage = null;
  123.             try {
  124.                 if(!allarmiAttivi.isEmpty()) {
  125.                    
  126.                     AlarmEngineConfig alarmEngineConfig = confCore.getAllarmiConfig();
  127.                    
  128.                     AllarmiUtils.stopActiveThreads(allarmiAttivi, ControlStationCore.getLog(), alarmEngineConfig);
  129.                 }
  130.             } catch(Exception e) {
  131.                 String errorMsg = MessageFormat.format(ConfigurazioneCostanti.MESSAGGIO_ERRORE_ALLARME_ELIMINATO_NOTIFICA_FALLITA,e.getMessage());
  132.                 ControlStationCore.getLog().error(errorMsg, e);
  133.                 pd.setMessage(errorMsg);
  134.                 pdMessage = errorMsg;
  135.             }
  136.            
  137.            

  138.             Object[] oggetti = allarmiRimossi.toArray(new Allarme[allarmiRimossi.size()]);
  139.             confCore.performDeleteOperation(userLogin, confHelper.smista(), oggetti);
  140.             // Preparo il menu
  141.             confHelper.makeMenu();

  142.             if(inUsoMessage.length()>0) {
  143.                 if(pdMessage!=null) {
  144.                     pd.setMessage(pdMessage+
  145.                             org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE+
  146.                             org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE+
  147.                             inUsoMessage.toString());
  148.                 }
  149.                 else {
  150.                     pd.setMessage(inUsoMessage.toString());
  151.                 }
  152.             }
  153.            
  154.             // Preparo la lista
  155.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  156.             int idLista = Liste.CONFIGURAZIONE_ALLARMI;

  157.             ricerca = confHelper.checkSearchParameters(idLista, ricerca);

  158.             List<ConfigurazioneAllarmeBean> lista = confCore.allarmiList(ricerca, ruoloPorta, nomePorta);
  159.            
  160.             confHelper.prepareAllarmiList(ricerca, lista, ruoloPorta, nomePorta, serviceBinding);
  161.            
  162.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  163.             // Forward control to the specified success URI
  164.             return ServletUtils.getStrutsForward (mapping,
  165.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI,
  166.                     ForwardParams.DEL());
  167.         } catch (Throwable e) {
  168.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  169.                     ConfigurazioneCostanti.OBJECT_NAME_CONFIGURAZIONE_ALLARMI, ForwardParams.DEL());
  170.         }
  171.     }
  172. }