6f4ed6366a6d95383c6651184934c1794e1684d6
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DestroyedModule.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.dependencyresolver;
9
10 import org.opendaylight.controller.config.api.ModuleIdentifier;
11 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
12 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
13 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Transfer object representing already committed module that needs to be
20  * destroyed. Implements comparable in order to preserve order in which modules
21  * were created. Module instances should be closed in order defined by the
22  * compareTo method.
23  */
24 public class DestroyedModule implements AutoCloseable,
25         Comparable<DestroyedModule>, Identifiable<ModuleIdentifier> {
26     private static final Logger LOG = LoggerFactory
27             .getLogger(DestroyedModule.class);
28
29     private final ModuleIdentifier identifier;
30     private final AutoCloseable instance;
31     private final ModuleJMXRegistrator oldJMXRegistrator;
32     private final OsgiRegistration osgiRegistration;
33     private final int orderingIdx;
34     private final RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
35
36     public DestroyedModule(final ModuleIdentifier identifier, final AutoCloseable instance,
37                            final ModuleJMXRegistrator oldJMXRegistrator,
38                            final OsgiRegistration osgiRegistration, final int orderingIdx,
39                            final RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator) {
40         this.identifier = identifier;
41         this.instance = instance;
42         this.oldJMXRegistrator = oldJMXRegistrator;
43         this.osgiRegistration = osgiRegistration;
44         this.orderingIdx = orderingIdx;
45         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
46     }
47
48     @Override
49     @SuppressWarnings("IllegalCatch")
50     public void close() {
51         LOG.trace("Destroying {}", identifier);
52         try {
53             instance.close();
54         } catch (final Exception e) {
55             LOG.error("Error while closing instance of {}", identifier, e);
56         }
57         try {
58             oldJMXRegistrator.close();
59         } catch (final Exception e) {
60             LOG.error("Error while closing jmx registrator of {}", identifier, e);
61         }
62         try {
63             if (runtimeBeanRegistrator != null) {
64                 runtimeBeanRegistrator.close();
65             }
66         } catch (final Exception e) {
67             LOG.error("Error while closing runtime bean jmx registrator of {}", identifier, e);
68         }
69         try {
70             osgiRegistration.close();
71         } catch (final Exception e) {
72             LOG.error("Error while closing osgi registration of {}", identifier, e);
73         }
74     }
75
76     @Override
77     public int compareTo(final DestroyedModule destroyedModule) {
78         return Integer.compare(orderingIdx, destroyedModule.orderingIdx);
79     }
80
81     @Override
82     public ModuleIdentifier getIdentifier() {
83         return identifier;
84     }
85 }