ImplementationConfiguration.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.protocol.basic.config;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.core.allarmi.Allarme;
  24. import org.openspcoop2.core.allarmi.utils.AllarmiDriverUtils;
  25. import org.openspcoop2.core.config.PortaApplicativa;
  26. import org.openspcoop2.core.config.PortaApplicativaAzione;
  27. import org.openspcoop2.core.config.constants.PortaApplicativaAzioneIdentificazione;
  28. import org.openspcoop2.core.controllo_traffico.AttivazionePolicy;
  29. import org.openspcoop2.core.controllo_traffico.utils.ControlloTrafficoDriverUtils;
  30. import org.openspcoop2.core.id.IDPortaApplicativa;
  31. import org.openspcoop2.core.id.IDServizio;
  32. import org.openspcoop2.core.mapping.ImplementationUtils;
  33. import org.openspcoop2.core.mapping.MappingErogazionePortaApplicativa;
  34. import org.openspcoop2.protocol.manifest.IntegrationConfiguration;
  35. import org.openspcoop2.protocol.manifest.IntegrationConfigurationResourceIdentificationMode;
  36. import org.openspcoop2.protocol.manifest.constants.ResourceIdentificationType;
  37. import org.openspcoop2.protocol.sdk.ProtocolException;
  38. import org.openspcoop2.protocol.sdk.config.ProtocolImplementation;
  39. import org.openspcoop2.protocol.sdk.constants.ConsoleInterfaceType;
  40. import org.openspcoop2.protocol.sdk.registry.IConfigIntegrationReader;

  41. /**
  42.  * ImplementationConfiguration
  43.  *
  44.  *
  45.  * @author Poli Andrea (apoli@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  */

  49. public class ImplementationConfiguration extends AbstractIntegrationConfiguration {

  50.     /**
  51.      *
  52.      */
  53.     private static final long serialVersionUID = 1L;

  54.     protected ImplementationConfiguration(IntegrationConfiguration integrationConfiguration) {
  55.         super(integrationConfiguration);
  56.     }
  57.    
  58.     public static boolean isDescriptionDefault(String descrizione) {
  59.         return descrizione!=null &&
  60.                 (descrizione.startsWith(ImplementationConfiguration.INTERNAL_IMPLEMENTATION_PREFIX) ||
  61.                 descrizione.startsWith(ImplementationConfiguration.SERVICE_IMPLEMENTATION_PREFIX));
  62.     }

  63.     private static final String SERVICE_IMPLEMENTATION_PREFIX = "Service implementation ";
  64.     public ProtocolImplementation createDefaultImplementation(IDServizio idServizio) throws ProtocolException {
  65.        
  66.         ProtocolImplementation implementation = new ProtocolImplementation();
  67.        
  68.         PortaApplicativa portaApplicativa = new PortaApplicativa();
  69.         portaApplicativa.setNome(this.getNome(idServizio, null, null, null,
  70.                 this.integrationConfiguration.getName().getParamList()));
  71.         portaApplicativa.setDescrizione(SERVICE_IMPLEMENTATION_PREFIX+idServizio.toString());
  72.         if(portaApplicativa.getDescrizione().length()>255) {
  73.             portaApplicativa.setDescrizione(SERVICE_IMPLEMENTATION_PREFIX+idServizio.toString());
  74.         }
  75.         if(portaApplicativa.getDescrizione().length()>255) {
  76.             portaApplicativa.setDescrizione(null);
  77.         }
  78.        
  79.         IDPortaApplicativa idPortaApplicativa = ImplementationUtils.setCommonParameter(portaApplicativa, idServizio, true, false);
  80.        
  81.         PortaApplicativaAzione pdAzione = new PortaApplicativaAzione();
  82.         ResourceIdentificationType defaultIdentification = this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getDefault();
  83.         if(defaultIdentification==null) {
  84.             defaultIdentification = this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getMode(0).getName();
  85.         }
  86.         boolean setPattern = false;
  87.         switch (defaultIdentification) {
  88.         case CONTENT:
  89.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.CONTENT_BASED);      
  90.             setPattern = true;
  91.             break;
  92.         case HEADER:
  93.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.HEADER_BASED);        
  94.             setPattern = true;
  95.             break;
  96.         case URL:
  97.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.URL_BASED);      
  98.             setPattern = true;
  99.             break;
  100.         case INPUT:
  101.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.INPUT_BASED);    
  102.             break;
  103.         case INTERFACE:
  104.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.INTERFACE_BASED);    
  105.             break;
  106.         case SOAP_ACTION:
  107.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.SOAP_ACTION_BASED);      
  108.             break;
  109.         case PROTOCOL:
  110.             pdAzione.setIdentificazione(PortaApplicativaAzioneIdentificazione.PROTOCOL_BASED);
  111.             break;
  112.         }
  113.         if(setPattern) {
  114.             pdAzione.setPattern(this.getNome(idServizio, null, portaApplicativa.getNome(), null,
  115.                     this.integrationConfiguration.getResourceIdentification().getIdentificationParameter().getParamList()));
  116.         }      
  117.         if(this.integrationConfiguration.getResourceIdentification().getIdentificationModes().isForceInterfaceMode()) {
  118.             pdAzione.setForceInterfaceBased(org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO);
  119.         }
  120.         else {
  121.             pdAzione.setForceInterfaceBased(org.openspcoop2.core.config.constants.StatoFunzionalita.DISABILITATO);
  122.         }
  123.         portaApplicativa.setAzione(pdAzione);

  124.         portaApplicativa.setRicercaPortaAzioneDelegata(org.openspcoop2.core.config.constants.StatoFunzionalita.ABILITATO);
  125.        
  126.         implementation.setPortaApplicativa(portaApplicativa);      

  127.         MappingErogazionePortaApplicativa mappingErogazione = ImplementationUtils.createMappingDefault(idServizio, idPortaApplicativa);
  128.                
  129.         implementation.setMapping(mappingErogazione);
  130.        
  131.         return implementation;
  132.        
  133.        
  134.     }
  135.    
  136.     public List<PortaApplicativaAzioneIdentificazione> supportedIdentificationModes(ConsoleInterfaceType consoleType) {
  137.         List<PortaApplicativaAzioneIdentificazione> list = new ArrayList<>();
  138.         for (IntegrationConfigurationResourceIdentificationMode mode :
  139.             this.integrationConfiguration.getResourceIdentification().getIdentificationModes().getModeList()) {
  140.            
  141.             if(mode.isOnlyAdvancedMode() &&
  142.                 ConsoleInterfaceType.STANDARD.equals(consoleType)) {
  143.                 continue;
  144.             }
  145.            
  146.             ResourceIdentificationType type = mode.getName();
  147.             switch (type) {
  148.             case CONTENT:
  149.                 list.add(PortaApplicativaAzioneIdentificazione.CONTENT_BASED);      
  150.                 break;
  151.             case HEADER:
  152.                 list.add(PortaApplicativaAzioneIdentificazione.HEADER_BASED);      
  153.                 break;
  154.             case URL:
  155.                 list.add(PortaApplicativaAzioneIdentificazione.URL_BASED);      
  156.                 break;
  157.             case INPUT:
  158.                 list.add(PortaApplicativaAzioneIdentificazione.INPUT_BASED);        
  159.                 break;
  160.             case INTERFACE:
  161.                 list.add(PortaApplicativaAzioneIdentificazione.INTERFACE_BASED);        
  162.                 break;
  163.             case SOAP_ACTION:
  164.                 list.add(PortaApplicativaAzioneIdentificazione.SOAP_ACTION_BASED);      
  165.                 break;
  166.             case PROTOCOL:
  167.                 list.add(PortaApplicativaAzioneIdentificazione.PROTOCOL_BASED);
  168. /**             throw new ProtocolException("IdentificationMode '"+type+"' unsupported");*/
  169.             }
  170.         }
  171.         return list;
  172.     }
  173.    
  174.     public ProtocolImplementation createImplementation(IConfigIntegrationReader configIntegrationReader, IDServizio idServizio,
  175.             PortaApplicativa portaApplicativaDefault,
  176.             String ruleName, String description, String ... azione ) throws ProtocolException {
  177.         return createImplementation(configIntegrationReader, idServizio,
  178.                 portaApplicativaDefault, null,
  179.                 ruleName, description, azione);
  180.     }
  181.     private static final String INTERNAL_IMPLEMENTATION_PREFIX = "Internal Implementation '";
  182.     public ProtocolImplementation createImplementation(IConfigIntegrationReader configIntegrationReader, IDServizio idServizio,
  183.             PortaApplicativa portaApplicativaDefault, PortaApplicativa portaApplicativaDaClonare,
  184.             String ruleName, String description, String ... azione ) throws ProtocolException {
  185.        
  186.         if(azione==null || azione.length<=0) {
  187.             throw new ProtocolException("Actions undefined");
  188.         }
  189.        
  190.         String nomePortaDelegante = portaApplicativaDefault.getNome();
  191.         String nomeNuovaPortaApplicativa = this.getNome(idServizio, null, nomePortaDelegante, ruleName,
  192.                 this.integrationConfiguration.getResourceIdentification().getSpecificResource().getName().getParamList());
  193.         String descrizioneNuovaPortaApplicativa = INTERNAL_IMPLEMENTATION_PREFIX+ruleName+"' for "+nomePortaDelegante;  
  194.         if(descrizioneNuovaPortaApplicativa.length()>255) {
  195.             descrizioneNuovaPortaApplicativa = INTERNAL_IMPLEMENTATION_PREFIX+ruleName+"'";
  196.         }
  197.         if(descrizioneNuovaPortaApplicativa.length()>255) {
  198.             descrizioneNuovaPortaApplicativa = null;
  199.         }
  200.        
  201.         ProtocolImplementation implementation = new ProtocolImplementation();
  202.        
  203.         PortaApplicativa portaApplicativa = null;
  204.        
  205.         // creo una nuova porta applicativa clonando quella selezionata
  206.         boolean setDatiServizio = false;
  207.         boolean portaClonata = false;
  208.         if(portaApplicativaDaClonare!=null) {
  209.            
  210.             portaApplicativa = (PortaApplicativa) portaApplicativaDaClonare.clone();
  211.             portaApplicativa.setId(null);// annullo il table id
  212.             portaApplicativa.setGestioneCors(null); // annulla la gestione Cors poiche' gestito solo nella porta di default
  213.             portaApplicativa.setCanale(null); // annullo il canale poiche' gestito solo nella porta di default
  214.            
  215.             IDPortaApplicativa idPA = new IDPortaApplicativa();
  216.             idPA.setNome(portaApplicativaDaClonare.getNome());
  217.            
  218.             // riporto Rate Limiting
  219.             List<AttivazionePolicy> listAP = null;
  220.             try {
  221.                 listAP = configIntegrationReader.getRateLimitingPolicy(idPA);
  222.             }catch(Exception e) {
  223.                 // ignore
  224.             }
  225.             List<String> idPolicyCreate = new ArrayList<>();
  226.             if(listAP!=null && !listAP.isEmpty()) {
  227.                 for (AttivazionePolicy attivazionePolicy : listAP) {
  228.                    
  229.                     AttivazionePolicy apCloned = (AttivazionePolicy) attivazionePolicy.clone();
  230.                     if(apCloned.getIdPolicy()!=null && apCloned.getFiltro()!=null && portaApplicativaDaClonare.getNome().equals(apCloned.getFiltro().getNomePorta())){
  231.                         try {
  232.                             apCloned.getFiltro().setNomePorta(nomeNuovaPortaApplicativa);
  233.                             String serialId = configIntegrationReader.getNextPolicyInstanceSerialId(apCloned.getIdPolicy());
  234.                             String idActive = ControlloTrafficoDriverUtils.buildIdActivePolicy(apCloned.getIdPolicy(), serialId);
  235.                             int limit = 0;
  236.                             while(idPolicyCreate.contains(idActive) && limit<1000) { // provo 1000 volte
  237.                                 limit++;
  238.                                 serialId = ControlloTrafficoDriverUtils.incrementPolicyInstanceSerialId(serialId);
  239.                                 idActive = ControlloTrafficoDriverUtils.buildIdActivePolicy(apCloned.getIdPolicy(), serialId);
  240.                             }
  241.                             idPolicyCreate.add(idActive);
  242.                             apCloned.setIdActivePolicy(idActive);
  243.                            
  244.                             if(implementation.getRateLimitingPolicies()==null) {
  245.                                 implementation.setRateLimitingPolicies(new ArrayList<>());
  246.                             }
  247.                             implementation.getRateLimitingPolicies().add(apCloned);
  248.                            
  249.                         }catch(Exception e) {
  250.                             // ignore
  251.                         }
  252.                     }
  253.                 }
  254.             }
  255.            
  256.             // riporto Allarmi
  257.             List<Allarme> listAllarmi = null;
  258.             try {
  259.                 listAllarmi = configIntegrationReader.getAllarmi(idPA);
  260.             }catch(Exception e) {
  261.                 // ignore
  262.             }
  263.             List<String> idAllarmiCreate = new ArrayList<>();
  264.             if(listAllarmi!=null && !listAllarmi.isEmpty()) {
  265.                 for (Allarme allarme : listAllarmi) {
  266.                    
  267.                     Allarme allarmeCloned = (Allarme) allarme.clone();
  268.                     if(allarmeCloned.getTipo()!=null && allarmeCloned.getFiltro()!=null && portaApplicativaDaClonare.getNome().equals(allarmeCloned.getFiltro().getNomePorta())){
  269.                         try {
  270.                             allarmeCloned.getFiltro().setNomePorta(nomeNuovaPortaApplicativa);
  271.                             String serialId = configIntegrationReader.getNextAlarmInstanceSerialId(allarmeCloned.getTipo());
  272.                             String uniqueName = AllarmiDriverUtils.buildIdAlarm(allarmeCloned.getTipo(), serialId);
  273.                             int limit = 0;
  274.                             while(idAllarmiCreate.contains(uniqueName) && limit<1000) { // provo 1000 volte
  275.                                 limit++;
  276.                                 serialId = AllarmiDriverUtils.incrementAlarmInstanceSerialId(serialId);
  277.                                 uniqueName = AllarmiDriverUtils.buildIdAlarm(allarmeCloned.getTipo(), serialId);
  278.                             }
  279.                             idAllarmiCreate.add(uniqueName);
  280.                             allarmeCloned.setNome(uniqueName);
  281.                            
  282.                             if(implementation.getAllarmi()==null) {
  283.                                 implementation.setAllarmi(new ArrayList<>());
  284.                             }
  285.                             implementation.getAllarmi().add(allarmeCloned);
  286.                            
  287.                         }catch(Exception e) {
  288.                             // ignore
  289.                         }
  290.                     }
  291.                 }
  292.             }
  293.            
  294.             portaClonata = true;
  295.        
  296.         } else {
  297.            
  298.             portaApplicativa = new PortaApplicativa();
  299.             setDatiServizio = true;    
  300.            
  301.         }

  302.         portaApplicativa.setNome(nomeNuovaPortaApplicativa);
  303.         portaApplicativa.setDescrizione(descrizioneNuovaPortaApplicativa);
  304.        
  305.         IDPortaApplicativa idPortaApplicativa = ImplementationUtils.setCommonParameter(portaApplicativa, idServizio, setDatiServizio, portaClonata);
  306.        
  307.         ImplementationUtils.setAzioneDelegate(portaApplicativa, nomePortaDelegante, azione);
  308.                
  309.         implementation.setPortaApplicativa(portaApplicativa);      

  310.         MappingErogazionePortaApplicativa mappingErogazione = ImplementationUtils.createMapping(idServizio, idPortaApplicativa, ruleName, description);
  311.        
  312.         implementation.setMapping(mappingErogazione);
  313.        
  314.         return implementation;
  315.        
  316.     }
  317. }