ConfigurazionePdD_controlloTraffico.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.pdd.config;

  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.commons.dao.DAOFactory;
  27. import org.openspcoop2.core.config.Proprieta;
  28. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  29. import org.openspcoop2.core.config.driver.DriverConfigurazioneNotFound;
  30. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDB;
  31. import org.openspcoop2.core.constants.TipoPdD;
  32. import org.openspcoop2.core.controllo_traffico.AttivazionePolicy;
  33. import org.openspcoop2.core.controllo_traffico.ConfigurazioneGenerale;
  34. import org.openspcoop2.core.controllo_traffico.ConfigurazionePolicy;
  35. import org.openspcoop2.core.controllo_traffico.ConfigurazioneRateLimitingProprieta;
  36. import org.openspcoop2.core.controllo_traffico.ElencoIdPolicy;
  37. import org.openspcoop2.core.controllo_traffico.ElencoIdPolicyAttive;
  38. import org.openspcoop2.core.controllo_traffico.IdActivePolicy;
  39. import org.openspcoop2.core.controllo_traffico.IdPolicy;
  40. import org.openspcoop2.core.controllo_traffico.beans.UniqueIdentifierUtilities;
  41. import org.openspcoop2.core.controllo_traffico.constants.RuoloPolicy;
  42. import org.openspcoop2.core.controllo_traffico.constants.TipoRisorsaPolicyAttiva;
  43. import org.openspcoop2.core.controllo_traffico.dao.IAttivazionePolicyServiceSearch;
  44. import org.openspcoop2.core.controllo_traffico.dao.IConfigurazionePolicyServiceSearch;
  45. import org.openspcoop2.generic_project.exception.NotFoundException;
  46. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  47. import org.openspcoop2.generic_project.expression.SortOrder;
  48. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  49. import org.openspcoop2.pdd.core.controllo_traffico.policy.config.PolicyConfiguration;
  50. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;

  51. /**    
  52.  * ConfigurazionePdD_controlloTraffico
  53.  *
  54.  * @author Poli Andrea (poli@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */
  58. public class ConfigurazionePdD_controlloTraffico extends AbstractConfigurazionePdDConnectionResourceManager {

  59.     private ServiceManagerProperties smp;
  60.    
  61.    
  62.     public ConfigurazionePdD_controlloTraffico(OpenSPCoop2Properties openspcoopProperties, DriverConfigurazioneDB driver, boolean useConnectionPdD) {
  63.         super(openspcoopProperties, driver, useConnectionPdD, OpenSPCoop2Logger.getLoggerOpenSPCoopControlloTrafficoSql(openspcoopProperties.isControlloTrafficoDebug()));
  64.            
  65.         this.smp = new ServiceManagerProperties();
  66.         this.smp.setShowSql(this.openspcoopProperties.isControlloTrafficoDebug());
  67.         this.smp.setDatabaseType(this.driver.getTipoDB());
  68.     }
  69.    
  70.    
  71.    
  72.    
  73.     private static ConfigurazioneGenerale configurazioneGenerale = null;
  74.     public ConfigurazioneGenerale getConfigurazioneControlloTraffico(Connection connectionPdD) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  75.        
  76.         if( this.configurazioneDinamica || ConfigurazionePdD_controlloTraffico.configurazioneGenerale==null){
  77.             ConfigurazionePdDConnectionResource cr = null;
  78.             try{
  79.                 cr = this.getConnection(connectionPdD, "ControlloTraffico.getConfigurazioneGenerale");
  80.                 org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  81.                         (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  82.                         getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  83.                                 cr.connectionDB,this.smp,this.log);
  84.                
  85.                 ConfigurazionePdD_controlloTraffico.configurazioneGenerale = sm.getConfigurazioneGeneraleServiceSearch().get();
  86.             }
  87.             catch(NotFoundException e) {
  88.                 String errorMsg = "Configurazione del Controllo del Traffico non trovata: "+e.getMessage();
  89.                 this.log.debug(errorMsg,e);
  90.                 throw new DriverConfigurazioneNotFound(errorMsg,e);
  91.             }
  92.             catch(Exception e){
  93.                 String errorMsg = "Errore durante la configurazione del Controllo del Traffico: "+e.getMessage();
  94.                 this.log.error(errorMsg,e);
  95.                 throw new DriverConfigurazioneException(errorMsg,e);
  96.             }
  97.             finally {
  98.                 this.releaseConnection(cr);
  99.             }
  100.         }

  101.         return ConfigurazionePdD_controlloTraffico.configurazioneGenerale;

  102.     }
  103.    
  104.    
  105.    
  106.    
  107.     private static PolicyConfiguration configurazionePolicyRateLimitingGlobali = null;
  108.     public PolicyConfiguration getConfigurazionePolicyRateLimitingGlobali(Connection connectionPdD) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  109.        
  110.         if( this.configurazioneDinamica || ConfigurazionePdD_controlloTraffico.configurazionePolicyRateLimitingGlobali==null){
  111.             ConfigurazionePdDConnectionResource cr = null;
  112.             try{
  113.                 cr = this.getConnection(connectionPdD, "ControlloTraffico.getConfigurazionePolicyRateLimitingGlobali");
  114.                                
  115.                 org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  116.                         (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  117.                         getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  118.                                 cr.connectionDB,this.smp,this.log);
  119.                
  120.                 List<Proprieta> list = new ArrayList<Proprieta>();
  121.                 List<ConfigurazioneRateLimitingProprieta> l = sm.getConfigurazioneRateLimitingProprietaServiceSearch().findAll(sm.getConfigurazioneRateLimitingProprietaServiceSearch().newPaginatedExpression());
  122.                 if(l!=null && !l.isEmpty()) {
  123.                     for (ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta : l) {
  124.                         Proprieta p = new Proprieta();
  125.                         p.setNome(configurazioneRateLimitingProprieta.getNome());
  126.                         p.setValore(configurazioneRateLimitingProprieta.getValore());
  127.                         list.add(p);
  128.                     }
  129.                 }
  130.                
  131.                 ConfigurazionePdD_controlloTraffico.configurazionePolicyRateLimitingGlobali = new PolicyConfiguration(list);
  132.             }
  133.             catch(NotFoundException e) {
  134.                 String errorMsg = "Configurazione delle policy globali di rate limiting non trovata: "+e.getMessage();
  135.                 this.log.debug(errorMsg,e);
  136.                 throw new DriverConfigurazioneNotFound(errorMsg,e);
  137.             }
  138.             catch(Exception e){
  139.                 String errorMsg = "Errore durante la configurazione del Controllo del Traffico: "+e.getMessage();
  140.                 this.log.error(errorMsg,e);
  141.                 throw new DriverConfigurazioneException(errorMsg,e);
  142.             }
  143.             finally {
  144.                 this.releaseConnection(cr);
  145.             }
  146.         }

  147.         return ConfigurazionePdD_controlloTraffico.configurazionePolicyRateLimitingGlobali;

  148.     }
  149.    
  150.    
  151.    
  152.     public Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> getElencoIdPolicyAttiveAPI(Connection connectionPdD, TipoPdD tipoPdD, String nomePorta) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  153.         return this.getElencoIdPolicyApiAttiveEngine(connectionPdD, tipoPdD, nomePorta, true,
  154.                 false,true);
  155.     }
  156.     public Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> getElencoIdPolicyAttiveGlobali(Connection connectionPdD) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  157.         return this.getElencoIdPolicyApiAttiveEngine(connectionPdD, null, null, false,
  158.                 false,true);
  159.     }
  160.     public Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> getElencoIdPolicyAttiveAPIDimensioneMessaggio(Connection connectionPdD, TipoPdD tipoPdD, String nomePorta) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  161.         return this.getElencoIdPolicyApiAttiveEngine(connectionPdD, tipoPdD, nomePorta, true,
  162.                 true,false);
  163.     }
  164.     public Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> getElencoIdPolicyAttiveGlobaliDimensioneMessaggio(Connection connectionPdD) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  165.         return this.getElencoIdPolicyApiAttiveEngine(connectionPdD, null, null, false,
  166.                 true,false);
  167.     }
  168.     private Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> getElencoIdPolicyApiAttiveEngine(Connection connectionPdD, TipoPdD tipoPdD, String nomePorta, boolean api,
  169.             boolean includiSoloPolicyDimensioneMessaggio, boolean escludiPolicyDimensioneMessaggio) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  170.        
  171.         String prefix = "";
  172.         if(includiSoloPolicyDimensioneMessaggio) {
  173.             prefix = " [DimensioneMessaggio]";
  174.         }
  175.        
  176.         if(api) {
  177.             if(tipoPdD==null) {
  178.                 throw new DriverConfigurazioneException("Tipo PdD non fornito; richiesto per una policy API"+prefix);
  179.             }
  180.             if(nomePorta==null) {
  181.                 throw new DriverConfigurazioneException("Nome Porta non fornito; richiesto per una policy API"+prefix);
  182.             }
  183.         }
  184.        
  185.         ConfigurazionePdDConnectionResource cr = null;
  186.         try{
  187.             cr = this.getConnection(connectionPdD, "ControlloTraffico.getElencoIdPolicyAttive");
  188.             org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  189.                     (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  190.                     getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  191.                             cr.connectionDB,this.smp,this.log);
  192.            
  193.             IAttivazionePolicyServiceSearch search =  sm.getAttivazionePolicyServiceSearch();
  194.             IPaginatedExpression expression = search.newPaginatedExpression();
  195.            
  196.             if(api) {
  197.                 if(TipoPdD.DELEGATA.equals(tipoPdD)) {
  198.                     expression.equals(AttivazionePolicy.model().FILTRO.RUOLO_PORTA, RuoloPolicy.DELEGATA.getValue());
  199.                 }
  200.                 else {
  201.                     expression.equals(AttivazionePolicy.model().FILTRO.RUOLO_PORTA, RuoloPolicy.APPLICATIVA.getValue());
  202.                 }
  203.                 expression.equals(AttivazionePolicy.model().FILTRO.NOME_PORTA,nomePorta);
  204.             }
  205.             else {
  206.                 expression.isNull(AttivazionePolicy.model().FILTRO.NOME_PORTA);
  207.             }
  208.            
  209.             expression.limit(100000); // non dovrebbero esistere tante regole
  210.             expression.addOrder(AttivazionePolicy.model().POSIZIONE, SortOrder.ASC);
  211.             List<IdActivePolicy> list = search.findAllIds(expression);
  212.             if(list!=null && list.size()>0){
  213.                
  214.                 // Devo raggrupparle per risorse.
  215.                 // Ordinate lo sono gia' poiche' ho fatto l'ordine precedentemente
  216.                 Map<TipoRisorsaPolicyAttiva, ElencoIdPolicyAttive> map = new HashMap<>();
  217.                 for (IdActivePolicy idActivePolicy : list) {
  218.                    
  219.                     ConfigurazionePolicy confPolicy = null;
  220.                     try {
  221.                         confPolicy = this.getConfigurazionePolicy(cr, idActivePolicy.getIdPolicy());
  222.                     }
  223.                     catch(DriverConfigurazioneNotFound e) {
  224.                         this.log.error("Configurazione Policy '"+idActivePolicy.getIdPolicy()+"' non esistente ? ",e);
  225.                         throw new NotFoundException("Configurazione Policy '"+idActivePolicy.getIdPolicy()+"' non esistente", e);  
  226.                     }
  227.                     if(confPolicy==null) {
  228.                         throw new NotFoundException("Configurazione Policy '"+idActivePolicy.getIdPolicy()+"' non esistente");  
  229.                     }
  230.                    
  231.                     ElencoIdPolicyAttive elencoIdPolicy = null;
  232.                    
  233.                     TipoRisorsaPolicyAttiva tipoPolicyAttiva = TipoRisorsaPolicyAttiva.getTipo(confPolicy.getRisorsa(), confPolicy.isSimultanee());
  234.                    
  235.                     if(escludiPolicyDimensioneMessaggio) {
  236.                         if(TipoRisorsaPolicyAttiva.DIMENSIONE_MASSIMA_MESSAGGIO.equals(tipoPolicyAttiva)) {
  237.                             continue;
  238.                         }
  239.                     }
  240.                     else if(includiSoloPolicyDimensioneMessaggio) {
  241.                         if(!TipoRisorsaPolicyAttiva.DIMENSIONE_MASSIMA_MESSAGGIO.equals(tipoPolicyAttiva)) {
  242.                             continue;
  243.                         }
  244.                     }
  245.                    
  246.                     if(map.containsKey(tipoPolicyAttiva)) {
  247.                         elencoIdPolicy = map.get(tipoPolicyAttiva);
  248.                     }
  249.                     else {
  250.                         elencoIdPolicy = new ElencoIdPolicyAttive();
  251.                         map.put(tipoPolicyAttiva, elencoIdPolicy);
  252.                     }
  253.                    
  254.                     elencoIdPolicy.addIdActivePolicy(idActivePolicy);
  255.                 }
  256.                
  257.                 if(map.isEmpty()) {
  258.                     if(api) {
  259.                         throw new NotFoundException("policy API non esistenti per la porta '"+nomePorta+"' (ruolo: "+tipoPdD.getTipo()+")"+prefix);
  260.                     }
  261.                     else {
  262.                         throw new NotFoundException("policy globali non esistenti"+prefix);
  263.                     }
  264.                 }
  265.                
  266.                 return map;
  267.             }
  268.             else {
  269.                 if(api) {
  270.                     throw new NotFoundException("policy API non esistenti per la porta '"+nomePorta+"' (ruolo: "+tipoPdD.getTipo()+")"+prefix);
  271.                 }
  272.                 else {
  273.                     throw new NotFoundException("policy globali non esistenti"+prefix);
  274.                 }
  275.             }
  276.         }
  277.         catch(NotFoundException e) {
  278.             String errorMsg = "ElencoIdPolicyAttive del Controllo del Traffico non trovata: "+e.getMessage();
  279.             this.log.debug(errorMsg,e);
  280.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  281.         }
  282.         catch(Exception e){
  283.             String errorMsg = "Errore durante la lettura dell'ElencoIdPolicyAttive del Controllo del Traffico: "+e.getMessage();
  284.             this.log.error(errorMsg,e);
  285.             throw new DriverConfigurazioneException(errorMsg,e);
  286.         }
  287.         finally {
  288.             this.releaseConnection(cr);
  289.         }

  290.     }
  291.    
  292.    
  293.    
  294.    
  295.    
  296.     public AttivazionePolicy getAttivazionePolicy(Connection connectionPdD, String id) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  297.        
  298.         ConfigurazionePdDConnectionResource cr = null;
  299.         try{
  300.             cr = this.getConnection(connectionPdD, "ControlloTraffico.getAttivazionePolicy_"+id);
  301.             org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  302.                     (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  303.                     getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  304.                             cr.connectionDB,this.smp,this.log);
  305.            
  306.             IAttivazionePolicyServiceSearch search =  sm.getAttivazionePolicyServiceSearch();
  307.             IdActivePolicy policyId = new IdActivePolicy();
  308.             policyId.setNome(UniqueIdentifierUtilities.extractIdActivePolicy(id));
  309.             return search.get(policyId);
  310.         }
  311.         catch(NotFoundException e) {
  312.             String errorMsg = "AttivazionePolicy del Controllo del Traffico non trovata: "+e.getMessage();
  313.             this.log.debug(errorMsg,e);
  314.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  315.         }
  316.         catch(Exception e){
  317.             String errorMsg = "Errore durante la lettura dell'AttivazionePolicy del Controllo del Traffico: "+e.getMessage();
  318.             this.log.error(errorMsg,e);
  319.             throw new DriverConfigurazioneException(errorMsg,e);
  320.         }
  321.         finally {
  322.             this.releaseConnection(cr);
  323.         }

  324.     }
  325.    
  326.    
  327.    
  328.    
  329.     public ElencoIdPolicy getElencoIdPolicy(Connection connectionPdD) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  330.        
  331.         ConfigurazionePdDConnectionResource cr = null;
  332.         try{
  333.             cr = this.getConnection(connectionPdD, "ControlloTraffico.getElencoIdPolicy");
  334.             org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  335.                     (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  336.                     getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  337.                             cr.connectionDB,this.smp,this.log);
  338.            
  339.             ElencoIdPolicy elencoIdPolicy = new ElencoIdPolicy();
  340.             IConfigurazionePolicyServiceSearch search =  sm.getConfigurazionePolicyServiceSearch();
  341.             IPaginatedExpression expression = search.newPaginatedExpression();
  342.             expression.limit(100000); // non dovrebbero esistere tante regole
  343.             List<IdPolicy> list = search.findAllIds(expression);
  344.             if(list!=null && !list.isEmpty()){
  345.                 elencoIdPolicy.setIdPolicyList(list);
  346.             }
  347.             return elencoIdPolicy;
  348.         }
  349.         catch(Exception e){
  350.             String errorMsg = "Errore durante la lettura dell'ElencoIdPolicy del Controllo del Traffico: "+e.getMessage();
  351.             this.log.error(errorMsg,e);
  352.             throw new DriverConfigurazioneException(errorMsg,e);
  353.         }
  354.         finally {
  355.             this.releaseConnection(cr);
  356.         }

  357.     }
  358.    
  359.    
  360.    
  361.    
  362.    
  363.     public ConfigurazionePolicy getConfigurazionePolicy(Connection connectionPdD, String id) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  364.        
  365.         ConfigurazionePdDConnectionResource cr = null;
  366.         try{
  367.             cr = this.getConnection(connectionPdD, "ControlloTraffico.getConfigurazionePolicy_"+id);
  368.             return getConfigurazionePolicy(cr, id);
  369.         }
  370.         catch(DriverConfigurazioneNotFound e){
  371.             throw e;
  372.         }
  373.         catch(DriverConfigurazioneException e){
  374.             throw e;
  375.         }
  376.         catch(Exception e){
  377.             String errorMsg = "Errore durante la lettura della ConfigurazionePolicy del Controllo del Traffico: "+e.getMessage();
  378.             this.log.error(errorMsg,e);
  379.             throw new DriverConfigurazioneException(errorMsg,e);
  380.         }
  381.         finally {
  382.             this.releaseConnection(cr);
  383.         }

  384.     }
  385.     private ConfigurazionePolicy getConfigurazionePolicy(ConfigurazionePdDConnectionResource crParam, String id) throws DriverConfigurazioneException, DriverConfigurazioneNotFound{
  386.        
  387.         try{
  388.             org.openspcoop2.core.controllo_traffico.dao.IServiceManager sm =
  389.                     (org.openspcoop2.core.controllo_traffico.dao.IServiceManager) DAOFactory.getInstance(this.log).
  390.                     getServiceManager(org.openspcoop2.core.controllo_traffico.utils.ProjectInfo.getInstance(),
  391.                             crParam.connectionDB,this.smp,this.log);
  392.            
  393.             IConfigurazionePolicyServiceSearch search =  sm.getConfigurazionePolicyServiceSearch();
  394.             IdPolicy policyId = new IdPolicy();
  395.             policyId.setNome(id);
  396.             return search.get(policyId);

  397.         }
  398.         catch(NotFoundException e) {
  399.             String errorMsg = "ConfigurazionePolicy del Controllo del Traffico non trovata: "+e.getMessage();
  400.             this.log.debug(errorMsg,e);
  401.             throw new DriverConfigurazioneNotFound(errorMsg,e);
  402.         }
  403.         catch(Exception e){
  404.             String errorMsg = "Errore durante la lettura della ConfigurazionePolicy del Controllo del Traffico: "+e.getMessage();
  405.             this.log.error(errorMsg,e);
  406.             throw new DriverConfigurazioneException(errorMsg,e);
  407.         }

  408.     }
  409.    
  410.    
  411. }