Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REQ-201 One ESME in Many Clusters #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Telestax, Open Source Cloud Communications Copyright 2011-2017,
* Telestax Inc and individual contributors by the @authors tag.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.mobicents.smsc.domain;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restcomm.smpp.EsmeCluster;

/**
* The Class CustomSmsRoutingRule.
*/
public final class CustomSmsRoutingRule extends DefaultSmsRoutingRule {

private static final Log LOG = LogFactory.getLog(CustomSmsRoutingRule.class);

@Override
public String getEsmeClusterName(final int aTon, final int anNpi, final String anAddress, final String aName,
final int aNetworkId) {
final EsmeCluster ec = getEsmeManagement().getEsmeCluster(aNetworkId);
if (ec == null) {
LOG.warn("No cluster configured for NetworkId: " + aNetworkId + ".");
return null;
}
if (ec.isOkFor(aTon, anNpi, anAddress, aName)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using cluster: " + ec + ". TON: " + aTon + ". NPI: " + anNpi + ". Address: " + anAddress
+ ". ESME Name (source): " + aName + ".");
}
return ec.getClusterName();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Selected cluster: " + ec + " is not applicable for the following parameters. TON: " + aTon
+ ". NPI: " + anNpi + ". Address: " + anAddress + ". ESME Name (source): " + aName + ".");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,14 @@ public String getSipClusterName(int ton, int npi, String address, int networkId)
}
return null;
}

/**
* Gets the ESME management.
*
* @return the ESME management
*/
protected final EsmeManagement getEsmeManagement() {
return esmeManagement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1396,62 +1396,8 @@ private void processSms(Sms sms0, PersistenceRAInterface store, Esme esme, Submi
if (smscPropertiesManagement.getIncomeReceiptsProcessing() && MessageUtil.isDeliveryReceipt(sms0)) {
DeliveryReceiptData deliveryReceiptData = MessageUtil.parseDeliveryReceipt(sms0.getShortMessageText(),
sms0.getTlvSet());

if (deliveryReceiptData != null) {
String clusterName = esme.getClusterName();
String dlvTlvMessageId = deliveryReceiptData.getTlvReceiptedMessageId();
String dlvMessageId = deliveryReceiptData.getMessageId();
Long messageId = null;
String drFormat = null;

if (dlvTlvMessageId != null) {
try {
messageId = persistence.c2_getMessageIdByRemoteMessageId(dlvTlvMessageId, clusterName);
drFormat = "dlvTlvMessageId";
} catch (PersistenceException e) {
logger.severe("Exception when running c2_getMessageIdByRemoteMessageId() - 1: " + e.getMessage(), e);
}
}
if (messageId == null) {
// trying to parse as a hex format
try {
messageId = persistence.c2_getMessageIdByRemoteMessageId(dlvMessageId, clusterName);
drFormat = "dlvMessageId";
} catch (PersistenceException e) {
logger.severe("Exception when running c2_getMessageIdByRemoteMessageId() - 2: " + e.getMessage(), e);
} catch (NumberFormatException e) {
}
}

if (messageId != null) {
// we found in local cache / database a reference to an origin
logger.info("Remote delivery receipt: clusterName=" + clusterName + ", dlvMessageId=" + dlvMessageId
+ ", dlvTlvMessageId=" + dlvTlvMessageId + ", receipt=" + sms0.getShortMessageText()
+ ", drFormat=" + drFormat);

if (dlvTlvMessageId != null) {
sms0.setReceiptOrigMessageId(dlvTlvMessageId);
sms0.getTlvSet().removeOptionalParameter(SmppConstants.TAG_RECEIPTED_MSG_ID);
} else {
sms0.setReceiptOrigMessageId(dlvMessageId);
}
sms0.setReceiptLocalMessageId(messageId);

String messageIdStr = MessageUtil.createMessageIdString(messageId);
String updatedReceiptText = MessageUtil.createDeliveryReceiptMessage(messageIdStr, deliveryReceiptData
.getSubmitDate(), deliveryReceiptData.getDoneDate(), deliveryReceiptData.getError(),
deliveryReceiptData.getText(),
deliveryReceiptData.getStatus().equals(MessageUtil.DELIVERY_ACK_STATE_DELIVERED), null,
deliveryReceiptData.getStatus().equals(MessageUtil.DELIVERY_ACK_STATE_ENROUTE));
sms0.setShortMessageText(updatedReceiptText);
} else {
// we have not found a local message - marking as unrecognized receipt
logger.warning("Remote delivery receipt - but no original message is found in local cache: clusterName="
+ clusterName + ", dlvMessageId=" + dlvMessageId + ", dlvTlvMessageId=" + dlvTlvMessageId
+ ", receipt=" + sms0.getShortMessageText() + ", drFormat=" + drFormat);

sms0.setReceiptLocalMessageId(-1L);
}
handleDeliveryReceipt(deliveryReceiptData, sms0, esme.getClusterName());
}
}

Expand Down Expand Up @@ -1534,6 +1480,74 @@ private void processSms(Sms sms0, PersistenceRAInterface store, Esme esme, Submi


}

private void handleDeliveryReceipt(final DeliveryReceiptData aDeliveryReceiptData, final Sms anSms,
final String aClusterName) {
for (final String c : aClusterName.split(",")) {
if (handleDeliveryReceiptInCluster(aDeliveryReceiptData, anSms, c.trim())) {
return;
}
}
// we have not found a local message - marking as unrecognized receipt
logger.warning("Remote delivery receipt - but no original message is found in local cache. ClusterName: "
+ aClusterName + ". DLR: " + aDeliveryReceiptData + ". Short Message: " + anSms.getShortMessageText()
+ ".");
anSms.setReceiptLocalMessageId(-1L);
}

private boolean handleDeliveryReceiptInCluster(final DeliveryReceiptData aDeliveryReceiptData, final Sms anSms,
final String aClusterName) {
final String dlvTlvMessageId = aDeliveryReceiptData.getTlvReceiptedMessageId();
final String dlvMessageId = aDeliveryReceiptData.getMessageId();
Long messageId = null;
String drFormat = null;

if (dlvTlvMessageId != null) {
try {
messageId = persistence.c2_getMessageIdByRemoteMessageId(dlvTlvMessageId, aClusterName);
drFormat = "dlvTlvMessageId";
} catch (PersistenceException e) {
logger.severe("Exception when running c2_getMessageIdByRemoteMessageId() - 1: " + e.getMessage(), e);
}
}
if (messageId == null) {
// trying to parse as a hex format
try {
messageId = persistence.c2_getMessageIdByRemoteMessageId(dlvMessageId, aClusterName);
drFormat = "dlvMessageId";
} catch (PersistenceException e) {
logger.severe("Exception when running c2_getMessageIdByRemoteMessageId() - 2: " + e.getMessage(), e);
} catch (NumberFormatException e) {
}
}

if (messageId == null) {
if (logger.isFineEnabled()) {
logger.fine("Original message ID not found for cluster: " + aClusterName + ".");
}
return false;
}
// we found in local cache / database a reference to an origin
logger.info("Remote delivery receipt: clusterName=" + aClusterName + ", dlvMessageId=" + dlvMessageId
+ ", dlvTlvMessageId=" + dlvTlvMessageId + ", receipt=" + anSms.getShortMessageText() + ", drFormat="
+ drFormat);

if (dlvTlvMessageId != null) {
anSms.setReceiptOrigMessageId(dlvTlvMessageId);
anSms.getTlvSet().removeOptionalParameter(SmppConstants.TAG_RECEIPTED_MSG_ID);
} else {
anSms.setReceiptOrigMessageId(dlvMessageId);
}
anSms.setReceiptLocalMessageId(messageId);
final String messageIdStr = MessageUtil.createMessageIdString(messageId);
final String updatedReceiptText = MessageUtil.createDeliveryReceiptMessage(messageIdStr,
aDeliveryReceiptData.getSubmitDate(), aDeliveryReceiptData.getDoneDate(),
aDeliveryReceiptData.getError(), aDeliveryReceiptData.getText(),
aDeliveryReceiptData.getStatus().equals(MessageUtil.DELIVERY_ACK_STATE_DELIVERED), null,
aDeliveryReceiptData.getStatus().equals(MessageUtil.DELIVERY_ACK_STATE_ENROUTE));
anSms.setShortMessageText(updatedReceiptText);
return true;
}

// *********
// private methods
Expand Down