ConfigurazioneLoadBalancer.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.core.behaviour.built_in.load_balance;

  21. import org.openspcoop2.core.config.PortaApplicativa;
  22. import org.openspcoop2.core.config.PortaApplicativaBehaviour;
  23. import org.openspcoop2.core.config.PortaApplicativaServizioApplicativo;
  24. import org.openspcoop2.core.config.Proprieta;
  25. import org.openspcoop2.message.OpenSPCoop2Message;
  26. import org.openspcoop2.pdd.core.PdDContext;
  27. import org.openspcoop2.pdd.core.behaviour.BehaviourEmitDiagnosticException;
  28. import org.openspcoop2.pdd.core.behaviour.BehaviourException;
  29. import org.openspcoop2.pdd.core.behaviour.BehaviourPropertiesUtils;
  30. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  31. import org.openspcoop2.protocol.sdk.Busta;
  32. import org.openspcoop2.protocol.sdk.state.IState;
  33. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  34. import org.slf4j.Logger;

  35. /**
  36.  * ConfigurazioneLoadBalancer
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class ConfigurazioneLoadBalancer  {

  43.     public static void addLoadBalancerType(PortaApplicativaBehaviour paBehaviour, String type) {
  44.         BehaviourPropertiesUtils.addProprieta(paBehaviour, Costanti.LOAD_BALANCER_TYPE, type);
  45.     }
  46.     public static String readLoadBalancerType(PortaApplicativaBehaviour paBehaviour) {
  47.         if(paBehaviour!=null && paBehaviour.sizeProprietaList()>0) {
  48.             for (Proprieta p : paBehaviour.getProprietaList()) {
  49.                 if(Costanti.LOAD_BALANCER_TYPE.equals(p.getNome())) {
  50.                     return p.getValore();
  51.                 }
  52.             }
  53.         }
  54.         return null;
  55.     }  
  56.    
  57.    
  58.     public static void addLoadBalancerWeight(PortaApplicativaServizioApplicativo paSA, String weight) {
  59.         BehaviourPropertiesUtils.addProprieta(paSA.getDatiConnettore(), Costanti.LOAD_BALANCER_WEIGHT, weight);
  60.     }
  61.     public static String readLoadBalancerWeight(PortaApplicativaServizioApplicativo paSA) {
  62.         if(paSA!=null && paSA.getDatiConnettore()!=null && paSA.getDatiConnettore().sizeProprietaList()>0) {
  63.             for (Proprieta p : paSA.getDatiConnettore().getProprietaList()) {
  64.                 if(Costanti.LOAD_BALANCER_WEIGHT.equals(p.getNome())) {
  65.                     return p.getValore();
  66.                 }
  67.             }
  68.         }
  69.         return LoadBalancerPool.DEFAULT_WEIGHT+"";
  70.     }  
  71.    
  72.    
  73.    
  74.     public static ConfigurazioneLoadBalancer read(PortaApplicativa pa, OpenSPCoop2Message message, Busta busta,
  75.             RequestInfo requestInfo, PdDContext pddContext,
  76.             MsgDiagnostico msgDiag, Logger log, IState state) throws BehaviourException, BehaviourEmitDiagnosticException {
  77.         ConfigurazioneLoadBalancer config = new ConfigurazioneLoadBalancer();
  78.         if(pa.getBehaviour()==null || pa.getBehaviour().sizeProprietaList()<=0) {
  79.             throw new BehaviourException("Load Balancer type undefined");
  80.         }
  81.         String type = null;
  82.         for (Proprieta p : pa.getBehaviour().getProprietaList()) {
  83.             if(Costanti.LOAD_BALANCER_TYPE.equals(p.getNome())) {
  84.                 type = p.getValore();
  85.             }
  86.         }
  87.         if(type==null) {
  88.             throw new BehaviourException("Load Balancer type undefined");
  89.         }
  90.         LoadBalancerType enumType = LoadBalancerType.toEnumConstant(type);
  91.         if(enumType==null) {
  92.             throw new BehaviourException("Load Balancer type '"+type+"' unknown");
  93.         }
  94.         config.setType(enumType);
  95.        
  96.         LoadBalancerInstance lbInstance = GestoreLoadBalancerCaching.getLoadBalancerInstance(pa, message, busta,
  97.                 requestInfo, pddContext,
  98.                 msgDiag, log,
  99.                 enumType, state);
  100.        
  101.         config.setPool(lbInstance.getLoadBalancerPool());

  102.         config.setConnectorSelected(lbInstance.getConnectorSelected());
  103.                
  104.         return config;
  105.     }

  106.     private LoadBalancerType type;
  107.     private LoadBalancerPool pool;
  108.     private String connectorSelected;
  109.    
  110.     public String getConnectorSelected() {
  111.         return this.connectorSelected;
  112.     }
  113.     public void setConnectorSelected(String connectorSelected) {
  114.         this.connectorSelected = connectorSelected;
  115.     }
  116.     public LoadBalancerPool getPool() {
  117.         return this.pool;
  118.     }
  119.     public void setPool(LoadBalancerPool pool) {
  120.         this.pool = pool;
  121.     }
  122.     public LoadBalancerType getType() {
  123.         return this.type;
  124.     }
  125.     public void setType(LoadBalancerType type) {
  126.         this.type = type;
  127.     }

  128. }