Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / DestroyedModule.java
1 /*
2  * Copyright (c) 2013 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;
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.osgi.BeanToOsgiServiceManager.OsgiRegistration;
13 import org.opendaylight.protocol.concepts.NamedObject;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Transfer object representing already committed module that needs to be
19  * destroyed. Implements comparable in order to preserve order in which modules
20  * were created. Module instances should be closed in order defined by the
21  * compareTo method.
22  */
23 public class DestroyedModule implements NamedObject<ModuleIdentifier>,
24         AutoCloseable, Comparable<DestroyedModule> {
25     private static final Logger logger = LoggerFactory
26             .getLogger(DestroyedModule.class);
27
28     private final ModuleIdentifier name;
29     private final AutoCloseable instance;
30     private final ModuleJMXRegistrator oldJMXRegistrator;
31     private final OsgiRegistration osgiRegistration;
32     private final int orderingIdx;
33
34     DestroyedModule(ModuleIdentifier name, AutoCloseable instance,
35             ModuleJMXRegistrator oldJMXRegistrator,
36             OsgiRegistration osgiRegistration, int orderingIdx) {
37         this.name = name;
38         this.instance = instance;
39         this.oldJMXRegistrator = oldJMXRegistrator;
40         this.osgiRegistration = osgiRegistration;
41         this.orderingIdx = orderingIdx;
42     }
43
44     @Override
45     public ModuleIdentifier getName() {
46         return name;
47     }
48
49     @Override
50     public void close() {
51         logger.info("Destroying {}", name);
52         try {
53             instance.close();
54         } catch (Exception e) {
55             logger.error("Error while closing instance of {}", name, e);
56         }
57         try {
58             oldJMXRegistrator.close();
59         } catch (Exception e) {
60             logger.error("Error while closing jmx registrator of {}", name, e);
61         }
62         try {
63             osgiRegistration.close();
64         } catch (Exception e) {
65             logger.error("Error while closing osgi registration of {}", name, e);
66         }
67     }
68
69     @Override
70     public int compareTo(DestroyedModule o) {
71         return Integer.compare(orderingIdx, o.orderingIdx);
72     }
73 }