Skip to content
MonitorCollectorImpl.java 15.3 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/**
 *
 */
package alma.TMCDB.MonitorCollectorImpl;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import alma.ACS.CharacteristicComponent;
import alma.ACS.CharacteristicComponentHelper;
import alma.ACSErr.ErrorTrace;
import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
import alma.MonitorArchiver.Controller;
import alma.MonitorArchiver.ControllerHelper;
import alma.MonitorErr.CollectorRegistrationFailedEx;
import alma.MonitorErr.DeviceAlreadyRegisteredEx;
import alma.MonitorErr.DeviceNotRegisteredEx;
import alma.MonitorErr.RegisteringDeviceProblemEx;
import alma.MonitorErr.StartMonitoringProblemEx;
import alma.MonitorErr.StopMonitoringProblemEx;
import alma.TMCDB.MonitorBlob;
import alma.TMCDB.MonitorCollectorOperations;
import alma.TMCDB.MonitorDataBlock;
import alma.TMCDB.doubleBlobData;
import alma.TMCDB.doubleBlobDataSeqHelper;
import alma.TMCDB.propertySerailNumber;
import alma.TMCDB.MonitorCollectorImpl.MonitorComponent;
import alma.acs.component.ComponentImplBase;
import alma.acs.component.ComponentLifecycleException;
import alma.acs.container.ContainerServices;
import alma.maciErrType.wrappers.AcsJComponentCleanUpEx;

/**
 * @author Claudio Tanci, ported from ACS C++ MonitorCollectorImpl by bjeram
 *
 */
public class MonitorCollectorImpl extends ComponentImplBase implements
		MonitorCollectorOperations {

	/*
	 * Monitor Control instance name, a note from MonitorCollectorImpl.cpp
	 * suggests that the instance could be obtained by type instead
	 * ("IDL:alma/MonitorArchiver/Controller:1.0")
	 */
	private String monitorControllerInstanceName = "ARCHIVE/TMCDB/MONITOR_CONTROL";
	private Controller monitorControl;

	/*
	 *  Monitored components maps, thread safe.
	 *  component instances names as keys.
	 */
	ConcurrentHashMap<String, MonitorComponent> monitorComponents = new ConcurrentHashMap<String, MonitorComponent>();
	ConcurrentHashMap<String, Boolean> isComponentMonitored = new ConcurrentHashMap<String, Boolean>();
	/**
	 *
	 */
	public MonitorCollectorImpl() {

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.acs.component.ComponentImplBase#initialize(alma.acs.container.
	 * ContainerServices)
	 */
	@Override
	public void initialize(ContainerServices containerServices)
			throws ComponentLifecycleException {

		super.initialize(containerServices);

		/*
		 * Registering with the controller, which will assign a blobber to this
		 * collector (m_containerServices is a ContainerServices from
		 * ComponentImplBase)
		 */
		org.omg.CORBA.Object monitorControllerRef;
		try {
			monitorControllerRef = m_containerServices
					.getComponentNonSticky(monitorControllerInstanceName);
			monitorControl = ControllerHelper
					.narrow(monitorControllerRef);
			monitorControl.registerCollector(name());
		} catch (AcsJContainerServicesEx e) {
			// TODO: is this correct?
			throw new ComponentLifecycleException(e);
		} catch (CollectorRegistrationFailedEx e) {
			// TODO: deal with this exception, e.g. raise an alarm.
			System.out.println("---> Failed registration");

		}

		System.out.println("---> Claudio MonitorCollector v. 0.1");

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.acs.component.ComponentImplBase#cleanUp()
	 */
	@Override
	public void cleanUp() throws AcsJComponentCleanUpEx {

		// For the moment ComponentImplBase.cleanUp() does nothing
		super.cleanUp();

		monitorControl.deregisterCollector(name());
		monitorComponents.clear();

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#registerMonitoredDevice(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public void registerMonitoredDevice(String componentName,
			String serialNumber) throws RegisteringDeviceProblemEx,
			DeviceAlreadyRegisteredEx {

		System.out.println("---> registerMonitoredDevice");
		registerCollocatedMonitoredDevice(componentName, serialNumber);

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#registerCollocatedMonitoredDevice
	 * (java.lang.String, java.lang.String)
	 */
	public void registerCollocatedMonitoredDevice(String componentName,
			String serialNumber) throws RegisteringDeviceProblemEx,
			DeviceAlreadyRegisteredEx {

		System.out.println("---> registerCollocatedMonitoredDevice");
		registerMonitoredComponentWithSerial(componentName, serialNumber, true);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#registerNonCollocatedMonitoredDevice
	 * (java.lang.String, java.lang.String)
	 */
	public void registerNonCollocatedMonitoredDevice(String componentName,
			String serialNumber) throws RegisteringDeviceProblemEx,
			DeviceAlreadyRegisteredEx {

		registerMonitoredComponentWithSerial(componentName, serialNumber, false);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.TMCDB.MonitorCollectorOperations#
	 * registerMonitoredDeviceWithMultipleSerial(java.lang.String,
	 * alma.TMCDB.propertySerailNumber[])
	 */
	public void registerMonitoredDeviceWithMultipleSerial(String componentName,
			propertySerailNumber[] serialNumbers)
			throws RegisteringDeviceProblemEx, DeviceAlreadyRegisteredEx {

		registerMonitoredComponentWithMultipleSerial(componentName,
				serialNumbers, true);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.TMCDB.MonitorCollectorOperations#
	 * registerCollocatedMonitoredDeviceWithMultipleSerial(java.lang.String,
	 * alma.TMCDB.propertySerailNumber[])
	 */
	public void registerCollocatedMonitoredDeviceWithMultipleSerial(
			String componentName, propertySerailNumber[] serialNumbers)
			throws RegisteringDeviceProblemEx, DeviceAlreadyRegisteredEx {

		registerMonitoredComponentWithMultipleSerial(componentName,
				serialNumbers, false);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.TMCDB.MonitorCollectorOperations#
	 * registerNonCollocatedMonitoredDeviceWithMultipleSerial(java.lang.String,
	 * alma.TMCDB.propertySerailNumber[])
	 */
	public void registerNonCollocatedMonitoredDeviceWithMultipleSerial(
			String componentName, propertySerailNumber[] serialNumbers)
			throws RegisteringDeviceProblemEx, DeviceAlreadyRegisteredEx {

		registerMonitoredComponentWithMultipleSerial(componentName,
				serialNumbers, false);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#deregisterMonitoredDevice(java.
	 * lang.String)
	 */
	@Override
	public void deregisterMonitoredDevice(String componentName)
			throws DeviceNotRegisteredEx {

		System.out.println("---> deregisterMonitoredDevice "+componentName);
		for (String k : monitorComponents.keySet()) {
			System.out.println("---> deregisterMonitoredDevice monitored component key "+k);
		}
		System.out.flush();

		if (monitorComponents.containsKey(componentName)) {

			// stop the monitoring
			if (isComponentMonitored.get(componentName)){
				MonitorComponent mc = monitorComponents.get(componentName);
				mc.stopMonitoring();
			}

			System.out.println("---> deregisterMonitoredDevice - stopped monitoring "+componentName);
			System.out.flush();

			for (String k : monitorComponents.keySet()) {
				System.out.println("---> monitored component key "+k);
				System.out.flush();
			}

			// remove the component from monitorComponents
			monitorComponents.remove(componentName);

		} else {
			// TODO: what is the standard way in ACS Java components to raise an
			// exception?
			// here I have copied the C++ way
			ErrorTrace errorTrace = new ErrorTrace();
			errorTrace.file = new Throwable().getStackTrace()[1].getFileName();
			errorTrace.lineNum = new Throwable().getStackTrace()[1]
					.getLineNumber();
			throw new DeviceNotRegisteredEx(errorTrace);
		}
	}

	/**
	 * @param componentName
	 * @param serialNumber
	 * @param b
	 * @throws RegisteringDeviceProblemEx
	 * @throws DeviceAlreadyRegisteredEx
	 */
	private void registerMonitoredComponentWithSerial(String componentName,
			String serialNumber, boolean checkCollocation)
			throws DeviceAlreadyRegisteredEx, RegisteringDeviceProblemEx {

		System.out.println("---> registerMonitoredComponentWithSerial");

		try {
			MonitorComponent mc = registerMonitoredComponent(componentName,
					checkCollocation);
			mc.setDeviceSerialNumber(serialNumber);
		} catch (AcsJContainerServicesEx e) {
			/*
			 * In the C++ implementation there is a check to remove mc from
			 * monitorComponents. I don't think it is possibile here to receive
			 * an AcsJContainerServicesEx and a mc in monitorComponents.
			 */
			throw new RegisteringDeviceProblemEx(e.getErrorTrace());
		}
	}

	/**
	 * @param componentName
	 * @param serialNumber
	 * @param b
	 * @throws DeviceAlreadyRegisteredEx
	 * @throws RegisteringDeviceProblemEx
	 */
	private void registerMonitoredComponentWithMultipleSerial(
			String componentName, propertySerailNumber[] serialNumbers,
			boolean checkCollocation) throws DeviceAlreadyRegisteredEx, RegisteringDeviceProblemEx {

		try {
			MonitorComponent mc = registerMonitoredComponent(componentName,
					checkCollocation);

			for (propertySerailNumber propertySerailNumber : serialNumbers) {
				mc.setPropertySerialNumber(propertySerailNumber.propertyName, propertySerailNumber.serialNumbers);
			}

		} catch (AcsJContainerServicesEx e) {
			/*
			 * In the C++ implementation there is a check to remove mc from
			 * monitorComponents. I don't think it is possibile here to receive
			 * an AcsJContainerServicesEx and a mc in monitorComponents.
			 */
			throw new RegisteringDeviceProblemEx(e.getErrorTrace());
		}
	}

	/**
	 * @param componentName
	 * @param checkCollocation
	 * @return
	 * @throws DeviceAlreadyRegisteredEx
	 * @throws AcsJContainerServicesEx
	 */
	private MonitorComponent registerMonitoredComponent(
			String componentName, boolean checkCollocation)
			throws DeviceAlreadyRegisteredEx, AcsJContainerServicesEx {

		/*
		 * In the C++ implementation an ACE_GUARD_RETURN and an
		 * ACE_Hash_Map_Manager are used.
		 * I will use a ConcurrentHashMap
		 * TODO: check if this is correct
		 */

		// Check to see if the component is already registered
		if (monitorComponents.containsKey(componentName)) {
			// TODO: what is the standard way in ACS Java components to raise an
			// exception?
			// here I have copied the C++ way
			ErrorTrace errorTrace = new ErrorTrace();
			errorTrace.file = new Throwable().getStackTrace()[1].getFileName();
			errorTrace.lineNum = new Throwable().getStackTrace()[1]
					.getLineNumber();
			throw new DeviceAlreadyRegisteredEx(errorTrace);

		}

		org.omg.CORBA.Object componentRef = m_containerServices
				.getComponentNonSticky(componentName);
		CharacteristicComponent component = CharacteristicComponentHelper
				.narrow(componentRef);

		// TODO check if colocation is requested
		// if (checkCollocation && (!component. )) {
		// // TODO: what is the standard way in ACS Java components to raise
		// an exception?
		// // here I have copied the C++ way
		// ErrorTrace errorTrace = new ErrorTrace();
		// errorTrace.file = new
		// Throwable().getStackTrace()[1].getFileName();
		// errorTrace.lineNum = new
		// Throwable().getStackTrace()[1].getLineNumber();
		// throw new NotCollocatedComponentEx(errorTrace);
		// }

		MonitorComponent mc = new MonitorComponent(component,
				m_containerServices);
		mc.addAllProperties();

		monitorComponents.put(componentName, mc);
		isComponentMonitored.put(componentName,false);

		return mc;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#startMonitoring(java.lang.String)
	 */
	@Override
	public void startMonitoring(String componentName)
			throws StartMonitoringProblemEx {

		if (monitorComponents.containsKey(componentName)) {
			// try {
			MonitorComponent mc = monitorComponents.get(componentName);
			mc.startMonitoring();
			// }
			isComponentMonitored.replace(componentName, true);

		} else {
			// TODO: what is the standard way in ACS Java components to raise an
			// exception?
			// here I have copied the C++ way
			ErrorTrace errorTrace = new ErrorTrace();
			errorTrace.file = new Throwable().getStackTrace()[1].getFileName();
			errorTrace.lineNum = new Throwable().getStackTrace()[1]
					.getLineNumber();
			throw new StartMonitoringProblemEx(errorTrace);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#stopMonitoring(java.lang.String)
	 */
	@Override
	public void stopMonitoring(String componentName)
			throws StopMonitoringProblemEx {

		System.out.println("---> stopMonitoring");
		for (String k : monitorComponents.keySet()) {
			System.out.println("---> monitored component key "+k);
		}

		if (monitorComponents.containsKey(componentName)) {
			// try {
			if (isComponentMonitored.get(componentName)){
				MonitorComponent mc = (MonitorComponent) monitorComponents
						.get(componentName);
				mc.stopMonitoring();
				isComponentMonitored.replace(componentName,false);
			}
			
			// }

		} else {
			// TODO: what is the standard way in ACS Java components to raise an
			// exception?
			// here I have copied the C++ way
			ErrorTrace errorTrace = new ErrorTrace();
			errorTrace.file = new Throwable().getStackTrace()[1].getFileName();
			errorTrace.lineNum = new Throwable().getStackTrace()[1]
					.getLineNumber();
			throw new StopMonitoringProblemEx(errorTrace);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see alma.TMCDB.MonitorCollectorOperations#getMonitorData()
	 */
	@Override
	public MonitorDataBlock[] getMonitorData() {



		MonitorDataBlock[] monitorDataBlock = new MonitorDataBlock[monitorComponents
				.size()];

		int i = 0;
		for (MonitorComponent mc : monitorComponents.values()) {

			mc.fillSeq();
			monitorDataBlock[i] = mc.getMonitorDataBlock();
			i++;
		}

//		System.out.println("---> MonitorCollector getMonitorData, length="+monitorDataBlock[0].monitorBlobs.length);
//		for (MonitorBlob b : monitorDataBlock[0].monitorBlobs) {
//			System.out.println("---> "+b.propertyName);
//		}

		return monitorDataBlock;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#set_archiving_interval(java.lang
	 * .String, java.lang.String, long)
	 */
	@Override
	public void set_archiving_interval(String componentName,
			String propertyName, long time) {

		if (monitorComponents.containsKey(componentName)) {
			// try {
			MonitorComponent mc = (MonitorComponent) monitorComponents
					.get(componentName);
			mc.set_archiving_interval(propertyName, time);
			// }

		} else {
			// TODO: What to do here? C++ implementation throw a
			// DeviceNotRegisteredEx, against IDL specification
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#suppress_archiving(java.lang.String
	 * , java.lang.String)
	 */
	@Override
	public void suppress_archiving(String componentName, String propertyName) {

		if (monitorComponents.containsKey(componentName)) {
			// try {
			MonitorComponent mc = (MonitorComponent) monitorComponents
					.get(componentName);
			mc.suppress_archiving(propertyName);
			// }

		} else {
			// TODO: What to do here? C++ implementation throw a
			// DeviceNotRegisteredEx, against IDL specification
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * alma.TMCDB.MonitorCollectorOperations#enable_archiving(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void enable_archiving(String componentName, String propertyName) {

		if (monitorComponents.containsKey(componentName)) {
			// try {
			MonitorComponent mc = (MonitorComponent) monitorComponents
					.get(componentName);
			mc.enable_archiving(propertyName);
			// }

		} else {
			// TODO: What to do here? C++ implementation throw a
			// DeviceNotRegisteredEx, it seems not compliant with IDL specification. (?)
		}
	}
}