/*
* 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.admin.mbeans.extensions;
import com.sun.enterprise.admin.mbeans.ConfigsMBean;
import com.sun.enterprise.admin.server.core.AdminService;
import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
import com.sun.enterprise.admin.target.ConfigTarget;
import com.sun.enterprise.admin.target.Target;
import com.sun.enterprise.config.ConfigContext;
import com.sun.enterprise.config.ConfigException;
import com.sun.enterprise.config.serverbeans.Acctapp;
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.Authapp;
import com.sun.enterprise.config.serverbeans.Bindto;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.ConfigAPIHelper;
import com.sun.enterprise.config.serverbeans.DiameterApplications;
import com.sun.enterprise.config.serverbeans.DiameterService;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.PeerConfiguration;
import com.sun.enterprise.config.serverbeans.Listener;
import com.sun.enterprise.config.serverbeans.Peer;
import com.sun.enterprise.config.serverbeans.Peers;
import com.sun.enterprise.config.serverbeans.Ssl;
import com.sun.enterprise.config.serverbeans.SystemProperty;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.i18n.StringManager;
import com.sun.enterprise.util.i18n.StringManagerBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.jvnet.glassfish.comms.util.LogUtil;
public class DiameterMBean extends ConfigsMBean {
private static final StringManager _strMgr = StringManager.getManager(DiameterMBean.class);
private static Logger _logger = LogUtil.SIP_LOGGER.getLogger();
private static final StringManagerBase _sMgr = StringManagerBase.getStringManager(_logger.getResourceBundleName());
private static final String DIAMETER_SUPPORT_FILE = "diametersupportfile";
private static final String DIAMETER_JAR_PATH = "diameterjarpath";
public DiameterMBean() {
super();
}
private Config getConfig(String targetName) throws MBeanException {
final Target target = getTarget(targetName);
Config config;
try {
final ConfigTarget configTarget = target.getConfigTarget();
config = ConfigAPIHelper.getConfigByName(getConfigContext(), configTarget.getName());
} catch (Exception e) {
throw new MBeanException(e);
}
return config;
}
private DiameterService getDiameterService(String targetName) throws MBeanException {
Config config = getConfig(targetName);
return config.getDiameterService()!= null?config.getDiameterService():null;
}
public void createDiameterService(String targetName) throws MBeanException {
try {
checkDiameterInstalled();
Config config = getConfig(targetName);
if (config.getDiameterService() != null) {
String msg = _strMgr.getString("DiameterSvcAlreadyExists", targetName);
throw new ConfigException(msg);
}
DiameterService diaService = new DiameterService();
diaService.setPeerConfiguration(createPeerConfig(targetName));
_logger.log(Level.FINE, _sMgr.getString("diameteradmin.defaultValuesCreation"));
config.setDiameterService(diaService);
addSystemProperty(config);
} catch (ConfigException e) {
throw new MBeanException(e);
}
}
private void addSystemProperty(Config config) throws ConfigException {
SystemProperty sp = new SystemProperty();
if (config.getSystemPropertyByName("DIAMETER_TCP_ADDRESS") == null) {
sp.setName("DIAMETER_TCP_ADDRESS");
sp.setValue("localhost");
config.addSystemProperty(sp);
}
sp = new SystemProperty();
if (config.getSystemPropertyByName("DIAMETER_SSL_ADDRESS") == null) {
sp.setName("DIAMETER_SSL_ADDRESS");
sp.setValue("localhost");
config.addSystemProperty(sp);
}
sp = new SystemProperty();
if (config.getSystemPropertyByName("DIAMETER_TCP_PORT") == null) {
sp.setName("DIAMETER_TCP_PORT");
sp.setValue("3777");
config.addSystemProperty(sp);
}
sp = new SystemProperty();
if (config.getSystemPropertyByName("DIAMETER_SSL_PORT") == null) {
sp.setName("DIAMETER_SSL_PORT");
sp.setValue("3778");
config.addSystemProperty(sp);
}
}
public void deleteDiameterService(String targetName) throws MBeanException {
try {
checkDiameterInstalled();
final Target target = getTarget(targetName);
final ConfigTarget configTarget = target.getConfigTarget();
Config config = ConfigAPIHelper.getConfigByName(getConfigContext(), configTarget.getName());
if (config.getDiameterService() == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new ConfigException(msg);
}
config.setDiameterService(null);
} catch (Exception e) {
throw new MBeanException(e);
}
}
public Listener createListener(String targetName) {
Listener listener = new Listener();
listener.setVendorid("31345");
listener.setProductname("Sun Diameter");
listener.setFirmwareRevision("1.0");
listener.setMode("client");
Bindto[] bindto = new Bindto[2];
Bindto bindtoTCP = new Bindto();
bindtoTCP.setPort("${DIAMETER_TCP_PORT}");
bindtoTCP.setHost("${DIAMETER_TCP_ADDRESS}");
bindtoTCP.setTransport("tcp");
bindto[0] = bindtoTCP;
Bindto bindtoTLS = new Bindto();
bindtoTLS.setPort("${DIAMETER_SSL_PORT}");
bindtoTLS.setHost("${DIAMETER_SSL_ADDRESS}");
bindtoTLS.setTransport("tls");
Ssl ssl = new Ssl();
ssl.setCertNickname("s1cs");
ssl.setClientAuthEnabled(false);
ssl.setSsl2Enabled(false);
ssl.setSsl3Enabled(false);
ssl.setTlsEnabled(true);
ssl.setTlsRollbackEnabled(true);
bindtoTLS.setSsl(ssl);
bindto[1] = bindtoTLS;
listener.setBindto(bindto);
listener.setAcceptUnknownPeer("true");
listener.setTctimer("10000");
String realmName = null;
try {
InetAddress addr = InetAddress.getLocalHost();
realmName = addr.getHostName();
} catch (UnknownHostException ue) {
realmName = "localhost";
}
realmName = realmName + "." + targetName;
listener.setRealm(realmName);
listener.setDiameterApplicationRef("one");
return listener;
}
public PeerConfiguration createPeerConfig(String targetName) throws MBeanException {
PeerConfiguration peerConfig;
try {
peerConfig = new PeerConfiguration();
Peers peers = new Peers();
peers.setRetry("300");
peerConfig.setPeers(peers);
peerConfig.setListener(createListener(targetName));
peerConfig.setMaxThreads("10");
peerConfig.setMsgBufferSize("4096");
peerConfig.setEnableMsgPersistence("false");
DiameterApplications diaApps = new DiameterApplications();
diaApps.setId("one");
Application diaApp = new Application();
Authapp authApp = new Authapp();
authApp.setId("16777221");
diaApp.addAuthapp(authApp);
diaApp.setVendorid("10415");
diaApps.addApplication(diaApp);
peerConfig.addDiameterApplications(diaApps);
} catch (ConfigException e) {
throw new MBeanException(e);
}
return peerConfig;
}
public void createPeer(String targetName, String diameterAppRef, String host,
String port, String realm, String poolName, String jndiName, String connId, String id) throws MBeanException {
try {
if (connId.equals("Sh"))
connId = "com.sun.diameter.application.sh.api.UserProfileServer";
else if (connId.equals("Ro"))
connId = "com.sun.diameter.application.credit.ro.api.RoChargingFactory";
else if (connId.equals("Rf"))
connId = "com.sun.diameter.application.credit.rf.api.RfChargingFactory";
else {
String msg = _strMgr.getString("InvConnDefn");
throw new ConfigException(msg);
}
if (poolName == null)
poolName = "pool_" + id;
if (jndiName == null)
jndiName = "eas/" + poolName;
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
createConnector(poolName, jndiName, connId, targetName);
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
Peer newPeer = new Peer();
newPeer.setId(id);
newPeer.setHost(host);
newPeer.setPort(port);
newPeer.setRealm(realm);
newPeer.setDiameterApplicationRef(diameterAppRef);
Peers peers = peerConfig.getPeers();
peers.addPeer(newPeer);
} catch (ConfigException e) {
throw new MBeanException(e);
}
}
void checkConnectorModule() throws ConfigException {
ConfigContext ctx = AdminService.getAdminService().getAdminContext()
.getAdminConfigContext();
Domain domain = (Domain) ctx.getRootConfigBean();
if (domain.getApplications().getConnectorModuleByName("sundiameter") == null) {
String msg = _strMgr.getString("ConnectorModuleNotDeployed");
throw new ConfigException(msg);
}
}
void createConnector(String poolName, String jndiName, String connId, String targetName)
throws ConfigException {
checkConnectorModule();
AttributeList al = new AttributeList();
al.add(new Attribute("resource_adapter_name", "sundiameter"));
al.add(new Attribute("connection_definition_name", connId));
al.add(new Attribute("name", poolName));
ObjectName target;
try {
// create Connector Connection Pool
target = new ObjectName("com.sun.appserv:type=resources,category=config");
target = (ObjectName)getMBeanServer().invoke(target, "createConnectorConnectionPool",
new Object[]{al, null, targetName},
new String[]{"javax.management.AttributeList", "java.util.Properties", "java.lang.String"});
// create connector resource
al = new AttributeList();
al.add(new Attribute("jndi_name", jndiName));
al.add(new Attribute("pool_name", poolName));
target = new ObjectName("com.sun.appserv:type=resources,category=config");
target = (ObjectName)getMBeanServer().invoke(target, "createConnectorResource",
new Object[]{al, null, targetName},
new String[]{"javax.management.AttributeList", "java.util.Properties", "java.lang.String"});
} catch (Exception ex) {
throw new ConfigException(ex.getMessage());
}
}
public void deletePeer(String targetName, String id) throws Exception {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
boolean deleted = false;
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
Peers peers = peerConfig.getPeers();
Peer[] peer = peers.getPeer();
for (int i=0; i < peer.length; i++) {
if (peer[i].getId().equals(id)) {
peers.removePeer(peer[i]);
deleted = true;
}
}
if (!deleted) {
String msg = _strMgr.getString("PeerDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
}
public String[] listPeer(String targetName) throws MBeanException {
String[] peerNames;
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
Peers peers = peerConfig.getPeers();
Peer[] peer = peers.getPeer();
peerNames = new String[peer.length];
for (int i=0; i < peer.length; i++)
peerNames[i] = peer[i].getId();
return peerNames;
}
public void createApplication(String targetName, String authAppId,
String acctAppId, String applicationsId, String vendorId) throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
try {
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
diaApps = new DiameterApplications();
diaApps.setId(applicationsId);
peerConfig.addDiameterApplications(diaApps);
}
Application diaApp = new Application();
diaApp.setVendorid(vendorId);
if (acctAppId != null) {
Acctapp acctApp = new Acctapp();
acctApp.setId(acctAppId);
diaApp.addAcctapp(acctApp);
}
if (authAppId != null) {
Authapp authApp = new Authapp();
authApp.setId(authAppId);
diaApp.addAuthapp(authApp);
}
diaApps.addApplication(diaApp);
} catch (ConfigException e) {
throw new MBeanException(e);
}
}
public void createAuthApp(String targetName, String applicationsId, String vendorId, String authAppId)
throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
try {
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
Application diaApp = diaApps.getApplicationByVendorid(vendorId);
if (diaApp == null) {
String msg = _strMgr.getString("ApplicationNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
if (authAppId != null) {
Authapp authApp = new Authapp();
authApp.setId(authAppId);
diaApp.addAuthapp(authApp);
}
} catch (ConfigException e) {
throw new MBeanException(e);
}
}
public void deleteAuthApp(String targetName, String applicationsId, String vendorId, String authAppId)
throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
try {
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
Application diaApp = diaApps.getApplicationByVendorid(vendorId);
if (diaApp == null) {
String msg = _strMgr.getString("ApplicationNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
if (diaApp.getAuthappById(authAppId) != null) {
Authapp authApp = diaApp.getAuthappById(authAppId);
diaApp.removeAuthapp(authApp);
}
} catch (MBeanException e) {
throw new MBeanException(e);
}
}
public void createAcctApp(String targetName, String applicationsId, String vendorId, String acctAppId)
throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
try {
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
Application diaApp = diaApps.getApplicationByVendorid(vendorId);
if (diaApp == null) {
String msg = _strMgr.getString("ApplicationNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
if (acctAppId != null) {
Acctapp acctApp = new Acctapp();
acctApp.setId(acctAppId);
diaApp.addAcctapp(acctApp);
}
} catch (ConfigException e) {
throw new MBeanException(e);
}
}
public void deleteAcctApp(String targetName, String applicationsId, String vendorId, String acctAppId)
throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
try {
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
Application diaApp = diaApps.getApplicationByVendorid(vendorId);
if (diaApp == null) {
String msg = _strMgr.getString("ApplicationNotAvl", targetName);
throw new MBeanException(new ConfigException(msg));
}
if (diaApp.getAcctappById(acctAppId) != null) {
Acctapp acctApp = diaApp.getAcctappById(acctAppId);
diaApp.removeAcctapp(acctApp);
}
} catch (MBeanException e) {
throw new MBeanException(e);
}
}
public void deleteApplication(String targetName, String applicationsId, String vendorId)
throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new Exception(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
if (diaApps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl");
throw new MBeanException(new ConfigException(msg));
}
Application[] apps = diaApps.getApplication();
if (apps.length == 1) {
String msg = _strMgr.getString("ApplicationRequiredAtleastOne");
throw new MBeanException(new ConfigException(msg));
}
Application diaApp = diaApps.getApplicationByVendorid(vendorId);
if (diaApp != null)
diaApps.removeApplication(diaApp);
else {
String msg = _strMgr.getString("ApplicationNotAvl");
throw new MBeanException(new ConfigException(msg));
}
}
public void deleteApplications(String targetName, String applicationsId) throws MBeanException {
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications apps = peerConfig.getDiameterApplicationsById(applicationsId);
if (apps == null) {
String msg = _strMgr.getString("ApplicationsNotAvl");
throw new MBeanException(new ConfigException(msg));
}
DiameterApplications[] diaApps = peerConfig.getDiameterApplications();
if (diaApps.length == 1) {
String msg = _strMgr.getString("ApplicationsRequiredAtleastOne");
throw new MBeanException(new ConfigException(msg));
}
peerConfig.removeDiameterApplications(apps);
}
public String[] listApplications(String targetName) throws MBeanException {
checkDiameterInstalled();
String[] applicationsNames;
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications[] diaApps = peerConfig.getDiameterApplications();
applicationsNames = new String[diaApps.length];
for (int i=0; i < diaApps.length; i++)
applicationsNames[i] = diaApps[i].getId();
return applicationsNames;
}
public String[] listApplication(String targetName, String applicationsId) throws MBeanException {
String[] applicationNames;
checkDiameterInstalled();
DiameterService diaService = getDiameterService(targetName);
if (diaService == null) {
String msg = _strMgr.getString("DiameterSvcDoesNotExists", targetName);
throw new MBeanException(new ConfigException(msg));
}
PeerConfiguration peerConfig = diaService.getPeerConfiguration();
DiameterApplications diaApps = peerConfig.getDiameterApplicationsById(applicationsId);
Application[] apps = diaApps.getApplication();
applicationNames = new String[apps.length];
for (int i=0; i < apps.length; i++)
applicationNames[i] = apps[i].getVendorid();
return applicationNames;
}
private void checkDiameterInstalled() throws MBeanException {
String iRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
String diameterFile = iRoot + File.separator + PEFileLayout.LIB_DIR
+ File.separator + DIAMETER_SUPPORT_FILE;
File f = new File(diameterFile);
if (f.exists()) {
try {
Properties prop = new Properties();
FileInputStream fin = new FileInputStream(f);
prop.load(fin);
String diaJarPath = (String) prop.get(DIAMETER_JAR_PATH);
File diaJar = new File(diaJarPath);
if (diaJar.exists())
return;
} catch (IOException ioe) {
// error
}
}
String msg = _strMgr.getString("DiameterSupportNotInst");
throw new MBeanException(new ConfigException(msg));
}
}