/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.clb.core.sip;
import com.ericsson.ssa.container.NetworkManager;
import com.ericsson.ssa.container.reporter.Reporter;
import com.ericsson.ssa.container.reporter.ReporterResolver;
import com.ericsson.ssa.sip.Header;
import com.ericsson.ssa.sip.Layer;
import com.ericsson.ssa.sip.LayerHelper;
import com.ericsson.ssa.sip.SipServletRequestImpl;
import com.ericsson.ssa.sip.SipServletResponseImpl;
import com.ericsson.ssa.sip.dns.TargetResolver;
import com.ericsson.ssa.sip.dns.TargetTuple;
import java.util.ListIterator;
import javax.servlet.sip.Address;
import javax.servlet.sip.ServletParseException;
import org.jvnet.glassfish.comms.clb.core.CLBConstants;
import org.jvnet.glassfish.comms.clb.core.Controller;
import org.jvnet.glassfish.comms.clb.core.ControllerInitializer;
import org.jvnet.glassfish.comms.util.LogUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.clb.core.monitor.CLBMonitoringManager;
import org.jvnet.glassfish.comms.clb.core.ProxyKeyExtractor;
/**
* This is the SipLoadBalancingManager that together with
* {@link SipLoadBalancerManagerBackEnd} makes up the layers for the SIP part of the Converged Load
* Balancer.
* <p>
* This part of the CLB is responsible for handling incoming requests/responses.
* It is not present in pure back-end servers.
*
* It is a singleton and will be inserted into the SIP chain by the
* {@link org.jvnet.glassfish.comms.startup.SipServiceListener} and
* {@link com.ericsson.ssa.config.LayerHandler} at start-up.
*
* It is this class that is responsible for reading the configuration and
* creating all the runtime objects for the front-end parts.
* <p>
* TODO (qbinjoe) Ensure that we share runtime objects (ServerCluster, ServerInstance,
* etc.) between front-end and back-end layer.
*/
public class SipLoadBalancerManager extends ControllerInitializer
implements Layer {
private static Logger logger = LogUtil.CLB_LOGGER.getLogger();
private static final String CONTROLLER_IS_NULL = "clb.sip.warning.controller_null";
private static final SipLoadBalancerManager instance = new SipLoadBalancerManager(); // The singleton
// instance
private static final CLBMonitoringManager clbMonitoringManager =
CLBMonitoringManager.getInstance();
private Layer nextLayer; // The next layer above
private Reporter reporter;
/**
* Create the instance
*/
private SipLoadBalancerManager() {
}
/**
* Gets the singleton instance. Used by the
* {@link com.ericsson.ssa.config.LayerHandler} (via reflection) when
* setting up the SIP and HTTP processing chains at start-up.
*
* @return the singleton instance
*/
public static SipLoadBalancerManager getInstance() {
return instance;
}
/**
* Called by {@link org.jvnet.glassfish.comms.startup.SipServiceListener} at
* Lifecycle.AFTER_START_EVENT.
*/
public void start() {
logger.log(Level.INFO, "clb.sip.starting_sip_LB");
// Configure the single SIP request group
if(controller != null) {
logger.log(Level.FINE, "clb.sip.started_successfully_sip_LB");
} else {
logger.log(Level.WARNING, CONTROLLER_IS_NULL);
}
}
@Override
public Controller createController() {
Controller newController = super.createController();
newController.setLBType(CLBConstants.SIP_CLB);
return newController;
}
// --------------- Layer interface ----------------
/**
* Handle an incoming SIP request.
*
* @param req the received request
* @see com.ericsson.ssa.sip.Layer#next(com.ericsson.ssa.sip.SipServletRequestImpl)
*/
public void next(SipServletRequestImpl req) {
//Controller can only be null in case CLB is not initialized
//Send error response to client
if (controller == null) {
sendErrorResponse(req);
return;
}
try {
// check if this request is already proxied
// In such a case this need to send up the stack
// without any further processing in CLB
if (ProxyKeyExtractor.isSipRequestProxied(req)) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "This sip request is already proxied, " + "continuing on this instance itself without further " + "processing in CLB.");
}
// Continue on this instance
LayerHelper.next(req, this, nextLayer);
return;
}
} catch (SipRoutingException ex) {
logger.log(Level.WARNING,
"clb.sip.failed_to_route_incoming_request", ex.getMessage());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
SipServletResponseImpl errorResponse =
req.createTerminatingResponse(503);
if (errorResponse != null) {
errorResponse.popDispatcher().dispatch(errorResponse);
}
return;
}
//Check if request is send by backend to be forwarded to
//client.
//Such a request will have a topmost route with parameter
//"felb".
Header routeHeader = req.getRawHeader(Header.ROUTE);
ListIterator<Address> routeIterator = null;
try {
if(routeHeader != null){
routeIterator = routeHeader.getAddressValues();
}
} catch (ServletParseException ex) {
logger.log(Level.WARNING,
"clb.sip.failed_to_route_incoming_request", ex.getMessage());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
SipServletResponseImpl errorResponse =
req.createTerminatingResponse(503);
if (errorResponse != null) {
errorResponse.popDispatcher().dispatch(errorResponse);
}
return;
}
if(routeIterator != null && routeIterator.hasNext()){
Address topRoute = routeIterator.next();
String feParam = topRoute.getURI().getParameter(CLBConstants.FE_LB_PARAM);
if(feParam != null){
//remove topmost route
boolean readOnly = routeHeader.isReadOnly();
routeHeader.setReadOnly(false);
routeIterator.remove();
routeHeader.setReadOnly(readOnly);
TargetTuple tt = null;
try{
tt = TargetResolver.getInstance().resolveRequest(
req, req.getContentLength());
}catch(Exception ex){
logger.log(Level.WARNING,
"clb.sip.failed_to_route_incoming_request", ex.getMessage());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
SipServletResponseImpl errorResponse =
req.createTerminatingResponse(503);
if (errorResponse != null) {
errorResponse.popDispatcher().dispatch(errorResponse);
}
return;
}
req.setResolvedRemote(tt);
LayerHelper.resetDispatcher(req, NetworkManager.getInstance());
req.popDispatcher().dispatch(req);
return;
}
}
SipRequestGroup sipRequestGroup =
(SipRequestGroup) controller.getRequestGroup(null);
if (sipRequestGroup != null) {
try {
boolean localServer =
sipRequestGroup.handleIncomingRequest(req);
if (localServer) {
// Continue on this server
req.pushTransactionDispatcher(this);
req.pushApplicationDispatcher(this);
LayerHelper.next(req, this, nextLayer);
} else {
//proxy request to the BE
req.popDispatcher().dispatch(req);
}
} catch (SipRoutingException e) {
if (clbMonitoringManager.isCLBMonitoringEnabled()) {
clbMonitoringManager.getCLBStatsUpdater().
incrementTotalErrorSipResponsesAtCLBLayerFE();
}
logger.log(Level.WARNING,
"clb.sip.failed_to_route_incoming_request", e.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", e);
}
SipServletResponseImpl errorResponse =
req.createTerminatingResponse(503);
if (errorResponse != null) {
errorResponse.popDispatcher().dispatch(errorResponse);
}
}
} else {
if (clbMonitoringManager.isCLBMonitoringEnabled()) {
clbMonitoringManager.getCLBStatsUpdater().
incrementTotalIncomingSipRequestsFE();
}
// No SIP request group just fall through this layer
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"No request group, fall through; request: " + req);
}
LayerHelper.next(req, this, nextLayer);
}
}
private void sendErrorResponse(SipServletRequestImpl req) {
if (clbMonitoringManager.isCLBMonitoringEnabled()) {
clbMonitoringManager.getCLBStatsUpdater().
incrementTotalErrorSipResponsesAtCLBLayerFE();
}
logger.log(Level.WARNING, CONTROLLER_IS_NULL);
SipServletResponseImpl errorResponse =
req.createTerminatingResponse(503,
"Could not service request as server is initializing");
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Could not service request as server is initializing:\n"
+ req + "Send error response:\n" + errorResponse);
}
if(errorResponse != null)
errorResponse.popDispatcher().dispatch(errorResponse);
}
/**
* Handle an incoming SIP response.
*
* @param resp the received request
* @see com.ericsson.ssa.sip.Layer#next(com.ericsson.ssa.sip.SipServletResponseImpl)
*/
public void next(SipServletResponseImpl resp) {
//Controller can only be null in case CLB is not initialized
//Drop response from client
if(controller == null){
logger.log(Level.WARNING, CONTROLLER_IS_NULL);
//drop the response
return;
}
SipRequestGroup sipRequestGroup =
(SipRequestGroup) controller.getRequestGroup(null);
if (sipRequestGroup != null) {
try{
boolean localServer =
sipRequestGroup.handleIncomingResponse(resp);
if(localServer){
// Continue on this server
LayerHelper.next(resp, this, nextLayer);
} else {
resp.popDispatcher().dispatch(resp);
}
} catch (SipRoutingException e) {
if (clbMonitoringManager.isCLBMonitoringEnabled()) {
clbMonitoringManager.getCLBStatsUpdater().
incrementTotalDiscardedSipResponsesFE();
}
logger.log(Level.WARNING,
"clb.sip.failed_to_route_incoming_response", e.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", e);
}
// Drop the response
}
} else {
if (clbMonitoringManager.isCLBMonitoringEnabled()) {
clbMonitoringManager.getCLBStatsUpdater().
incrementTotalIncomingSipResponsesFE();
}
// No SIP request group just fall through this layer
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"No request group, fall through; request: " + resp);
}
LayerHelper.next(resp, this, nextLayer);
}
}
/**
* No handling of outgoing requests (handled by
* {@link SipLoadBalancerManagerBackEnd}).
*
* @param req the request to be sent
* @see com.ericsson.ssa.sip.Dispatcher#dispatch(com.ericsson.ssa.sip.SipServletRequestImpl)
*/
public void dispatch(SipServletRequestImpl req) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "Dispatch request: " + req);
}
req.popDispatcher().dispatch(req);
}
/**
* No handling of outgoing responses (handled by
* {@link SipLoadBalancerManagerBackEnd}).
*
* @param resp the response to be sent
* @see com.ericsson.ssa.sip.Dispatcher#dispatch(com.ericsson.ssa.sip.SipServletResponseImpl)
*/
public void dispatch(SipServletResponseImpl resp) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "Dispatch response: " + resp);
}
resp.popDispatcher().dispatch(resp);
}
/**
* Register the next layer (used by
* {@link com.ericsson.ssa.config.LayerHandler})
*
* @see com.ericsson.ssa.sip.Layer#registerNext(com.ericsson.ssa.sip.Layer)
*/
public void registerNext(Layer layer) {
nextLayer = layer;
}
public void setReporters(String reporters){
reporter = ReporterResolver.getInstance().getReporter(reporters);
}
public Reporter getReporter(){
return reporter;
}
}