Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / ServiceReferenceRegistrator.java
1 /*
2  * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.impl.jmx;
9
10 import javax.management.InstanceAlreadyExistsException;
11 import javax.management.ObjectName;
12 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
13
14 public interface ServiceReferenceRegistrator extends AutoCloseable {
15
16     String getNullableTransactionName();
17
18     ServiceReferenceJMXRegistration registerMBean(ServiceReferenceMXBeanImpl object, ObjectName on)
19             throws InstanceAlreadyExistsException;
20
21     @Override
22     void close();
23
24     class ServiceReferenceJMXRegistration implements AutoCloseable {
25         private final InternalJMXRegistration registration;
26
27         ServiceReferenceJMXRegistration(final InternalJMXRegistration registration) {
28             this.registration = registration;
29         }
30
31         @Override
32         public void close() {
33             registration.close();
34         }
35     }
36
37     interface ServiceReferenceTransactionRegistratorFactory {
38         ServiceReferenceRegistrator create();
39     }
40
41     class ServiceReferenceRegistratorImpl implements ServiceReferenceRegistrator {
42         private final InternalJMXRegistrator currentJMXRegistrator;
43         private final String nullableTransactionName;
44
45         public ServiceReferenceRegistratorImpl(final NestableJMXRegistrator parentRegistrator,
46                 final String nullableTransactionName) {
47             currentJMXRegistrator = parentRegistrator.createChild();
48             this.nullableTransactionName = nullableTransactionName;
49         }
50
51         @Override
52         public String getNullableTransactionName() {
53             return nullableTransactionName;
54         }
55
56         @Override
57         public ServiceReferenceJMXRegistration registerMBean(final ServiceReferenceMXBeanImpl object,
58                 final ObjectName on) throws InstanceAlreadyExistsException {
59             String actualTransactionName = ObjectNameUtil.getTransactionName(on);
60             boolean broken = false;
61             broken |= nullableTransactionName == null != (actualTransactionName == null);
62             broken |= nullableTransactionName != null && !nullableTransactionName.equals(actualTransactionName);
63             if (broken) {
64                 throw new IllegalArgumentException("Transaction name mismatch between expected "
65                         + nullableTransactionName + ", got " + actualTransactionName + " in " + on);
66             }
67             if (!ObjectNameUtil.isServiceReference(on)) {
68                 throw new IllegalArgumentException("Invalid type of " + on);
69             }
70             return new ServiceReferenceJMXRegistration(currentJMXRegistrator.registerMBean(object, on));
71         }
72
73         @Override
74         public void close() {
75             currentJMXRegistrator.close();
76         }
77
78         public interface ServiceReferenceTransactionRegistratorFactory {
79             ServiceReferenceRegistrator create();
80         }
81     }
82
83     class ServiceReferenceTransactionRegistratorFactoryImpl implements ServiceReferenceTransactionRegistratorFactory {
84         private final NestableJMXRegistrator parentRegistrator;
85         private final String nullableTransactionName;
86
87         public ServiceReferenceTransactionRegistratorFactoryImpl(
88                 final TransactionModuleJMXRegistrator parentRegistrator, final String nullableTransactionName) {
89             this.parentRegistrator = parentRegistrator;
90             this.nullableTransactionName = nullableTransactionName;
91         }
92
93         public ServiceReferenceTransactionRegistratorFactoryImpl(final BaseJMXRegistrator baseJMXRegistrator) {
94             this.parentRegistrator = baseJMXRegistrator;
95             this.nullableTransactionName = null;
96         }
97
98         @Override
99         public ServiceReferenceRegistrator create() {
100             return new ServiceReferenceRegistratorImpl(parentRegistrator, nullableTransactionName);
101         }
102     }
103 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.