From a974aa0cfb827476104c140096de676711d2b673 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:31:53 -0500 Subject: Initial OpenECOMP policy/common commit Change-Id: I61cd29d6d8bf8702c1a66915895b519bf3484afa Signed-off-by: Pamela Dragosh --- .../policy/common/im/jmx/ComponentAdmin.java | 227 +++++++++++++++++++++ .../policy/common/im/jmx/ComponentAdminMBean.java | 50 +++++ .../policy/common/im/jmx/JmxAgentConnection.java | 133 ++++++++++++ 3 files changed, 410 insertions(+) create mode 100644 integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdmin.java create mode 100644 integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdminMBean.java create mode 100644 integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/JmxAgentConnection.java (limited to 'integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx') diff --git a/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdmin.java b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdmin.java new file mode 100644 index 00000000..4d8399a1 --- /dev/null +++ b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdmin.java @@ -0,0 +1,227 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Monitor + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.im.jmx; + +import java.util.ArrayList; +import java.util.Iterator; + +import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MBeanServerFactory; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; + +import org.apache.log4j.Logger; + +import org.openecomp.policy.common.im.IntegrityMonitor; +import org.openecomp.policy.common.im.StateManagement; + +/** + * Base class for component MBeans. + */ +public class ComponentAdmin implements ComponentAdminMBean { + private static final Logger logger = Logger.getLogger(ComponentAdmin.class.getName()); + + private final String name; + private MBeanServer registeredMBeanServer; + private ObjectName registeredObjectName; + private IntegrityMonitor integrityMonitor = null; + private StateManagement stateManager = null; + + /** + * Constructor. + * @param name the MBean name + * @param integrityMonitor + * @param stateManager + * @throws Exception + */ + public ComponentAdmin(String name, IntegrityMonitor integrityMonitor, StateManagement stateManager) throws Exception { + if ((name == null) || (integrityMonitor == null) || (stateManager == null)) { + logger.error("Error: ComponentAdmin constructor called with invalid input"); + throw new NullPointerException("null input"); + } + + this.name = "ECOMP_POLICY_COMP:name=" + name; + this.integrityMonitor = integrityMonitor; + this.stateManager = stateManager; + + try { + register(); + } catch (Exception e) { + logger.info("Failed to register ComponentAdmin MBean"); + throw e; + } + } + + /** + * Registers with the MBean server. + * @throws MalformedObjectNameException a JMX exception + * @throws InstanceNotFoundException a JMX exception + * @throws MBeanRegistrationException a JMX exception + * @throws NotCompliantMBeanException a JMX exception + * @throws InstanceAlreadyExistsException a JMX exception + */ + public synchronized void register() throws MalformedObjectNameException, + MBeanRegistrationException, InstanceNotFoundException, + InstanceAlreadyExistsException, NotCompliantMBeanException { + + //if (LOGGER.isDebugEnabled()) { + logger.info("Registering " + name + " MBean"); + //} + + MBeanServer mbeanServer = findMBeanServer(); + + if (mbeanServer == null) { + //LOGGER.warn("No MBeanServer to register " + name + " MBean"); + return; + } + + ObjectName objectName = new ObjectName(name); + + if (mbeanServer.isRegistered(objectName)) { + logger.info("Unregistering a previously registered " + + name + " MBean"); + mbeanServer.unregisterMBean(objectName); + } + + mbeanServer.registerMBean(this, objectName); + registeredMBeanServer = mbeanServer; + registeredObjectName = objectName; + } + + /** + * Checks if this MBean is registered with the MBeanServer. + * @return true if this MBean is registered with the MBeanServer. + */ + public boolean isRegistered() { + return registeredObjectName != null; + } + + /** + * Unregisters with the MBean server. + * @throws InstanceNotFoundException a JMX exception + * @throws MBeanRegistrationException a JMX exception + */ + public synchronized void unregister() throws MBeanRegistrationException, + InstanceNotFoundException { + + if (registeredObjectName == null) { + return; + } + + //if (LOGGER.isDebugEnabled()) { + //LOGGER.debug("Unregistering " + name + " MBean"); + //} + + registeredMBeanServer.unregisterMBean(registeredObjectName); + registeredMBeanServer = null; + registeredObjectName = null; + } + + /** + * {@inheritDoc} + */ + public String toString() { + return ComponentAdmin.class.getSimpleName() + "[" + name + "]"; + } + + /** + * Finds the MBeanServer. + * @return the MBeanServer, or null if it is not found + */ + public static MBeanServer findMBeanServer() { + ArrayList mbeanServers = + MBeanServerFactory.findMBeanServer(null); + + Iterator iter = mbeanServers.iterator(); + MBeanServer mbeanServer = null; + + while (iter.hasNext()) { + mbeanServer = iter.next(); + if (mbeanServer.getDefaultDomain().equals("DefaultDomain")) { + return mbeanServer; + } + } + + return null; + } + + /** + * Creates the MBeanServer (intended for unit testing only). + * @return the MBeanServer + */ + public static MBeanServer createMBeanServer() { + return MBeanServerFactory.createMBeanServer("DefaultDomain"); + } + + /** + * Get the MBean object name for the specified feature name. + * @param componentName component name + * @return the object name + * @throws MalformedObjectNameException a JMX exception + */ + public static ObjectName getObjectName(String componentName) + throws MalformedObjectNameException { + return new ObjectName("ECOMP_POLICY_COMP:name=" + componentName); + } + + @Override + public void test() throws Exception { + // Call evaluateSanity on IntegrityMonitor to run the test + logger.info("test() called..."); + if (integrityMonitor != null) { + integrityMonitor.evaluateSanity(); + } + else { + logger.error("Unable to invoke test() - state manager instance is null"); + throw new NullPointerException("stateManager"); + } + + } + + @Override + public void lock() throws Exception { + logger.info("lock() called..."); + if (stateManager != null) { + stateManager.lock(); + } + else { + logger.error("Unable to invoke lock() - state manager instance is null"); + throw new NullPointerException("stateManager"); + } + } + + @Override + public void unlock() throws Exception { + logger.info("unlock() called..."); + if (stateManager != null) { + stateManager.unlock(); + } + else { + logger.error("Unable to invoke unlock() - state manager instance is null"); + throw new NullPointerException("stateManager"); + } + + } +} diff --git a/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdminMBean.java b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdminMBean.java new file mode 100644 index 00000000..5cf24b6b --- /dev/null +++ b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/ComponentAdminMBean.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Monitor + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.im.jmx; + +/** + * Provides operations to test health, lock and unlock components. + */ +public interface ComponentAdminMBean { + /** + * Test health of component. + * + * @throws Exception + * if the component fails the health check + */ + void test() throws Exception; + + /** + * Administratively lock component. + * + * @throws Exception + * if the component lock fails + */ + void lock() throws Exception; + + /** + * Administratively unlock component. + * + * @throws Exception + * if the component unlock fails + */ + void unlock() throws Exception; +} diff --git a/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/JmxAgentConnection.java b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/JmxAgentConnection.java new file mode 100644 index 00000000..4940c4d2 --- /dev/null +++ b/integrity-monitor/src/main/java/org/openecomp/policy/common/im/jmx/JmxAgentConnection.java @@ -0,0 +1,133 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Monitor + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +/** + * + */ +package org.openecomp.policy.common.im.jmx; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import javax.management.MBeanServerConnection; +import javax.management.Notification; +import javax.management.NotificationListener; +import javax.management.remote.JMXConnectionNotification; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; + +/** + * Class to create a JMX RMI connection to the JmxAgent. + */ +public final class JmxAgentConnection { + + private static final String DEFAULT_HOST = "localhost"; + private static final String DEFAULT_PORT = "9996"; + + private String host; + private String port; + private JMXConnector connector; + private String jmxUrl = null; + + //private final static Logger Log = Logger.getLogger(JmxAgentConnection.class); + + /** + * Set up the host/port from the properties. Use defaults if missing from the properties. + * @param properties the properties used to look for host and port + */ + //JmxAgentConnection(Properties properties) { + //host = properties.getProperty("jmxAgent.host", DEFAULT_HOST); + //port = properties.getProperty("jmxAgent.port", DEFAULT_PORT); + //} + + public JmxAgentConnection() { + host = DEFAULT_HOST; + port = DEFAULT_PORT; + } + + public JmxAgentConnection(String url) { + jmxUrl = url; + } + + /** + * Generate jmxAgent url. + * service:jmx:rmi:///jndi/rmi://host.domain:9999/jmxAgent + * + * @param host + * host.domain + * @param port + * 9999 + * @return jmxAgent url. + */ + private static String jmxAgentUrl(String host, String port) { + + String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + + "/jmxrmi"; + + return url; + } + + /** + * Get a connection to the jmxAgent MBeanServer. + * @return the connection + * @throws Exception on error + */ + public MBeanServerConnection getMBeanConnection() throws Exception { + JMXServiceURL url; + if (jmxUrl == null) { + url = new JMXServiceURL(jmxAgentUrl(host, port)); + } + else { + url = new JMXServiceURL(jmxUrl); + } + Map env = new HashMap(); + + connector = JMXConnectorFactory.newJMXConnector(url, env); + connector.connect(); + connector.addConnectionNotificationListener( + new NotificationListener() { + + @Override + public void handleNotification( + Notification notification, Object handback) { + if (notification.getType().equals( + JMXConnectionNotification.FAILED)) { + //Log.debug("JMXAgent connection failure"); + // handle disconnect + disconnect(); + } + } + }, null, null); + + return connector.getMBeanServerConnection(); + } + + /** + * Disconnect. + */ + public void disconnect() { + if (connector != null) { + try { connector.close(); } catch (IOException e) { } + } + } +} -- cgit 1.2.3-korg