Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / ModuleInfoBundleTracker.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.osgi.mapping;
9
10 import com.google.common.io.Resources;
11 import java.io.IOException;
12 import java.net.URL;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.LinkedList;
17 import java.util.List;
18 import org.opendaylight.yangtools.concepts.ObjectRegistration;
19 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
20 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleEvent;
23 import org.osgi.util.tracker.BundleTracker;
24 import org.osgi.util.tracker.BundleTrackerCustomizer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed
30  * into ModuleInfoRegistry.
31  */
32 public final class ModuleInfoBundleTracker
33         implements AutoCloseable, BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
36
37     public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
38     private static final String YANG_MODULE_INFO_SERVICE_PATH = MODULE_INFO_PROVIDER_PATH_PREFIX
39             + YangModelBindingProvider.class.getName();
40
41     private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
42
43     private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
44     private boolean starting;
45
46     public ModuleInfoBundleTracker(final RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
47         this.moduleInfoRegistry = moduleInfoRegistry;
48     }
49
50     public void open(final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
51         LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
52
53         if (bundleTracker != null) {
54             this.bundleTracker = bundleTracker;
55             starting = true;
56             bundleTracker.open();
57
58             starting = false;
59             moduleInfoRegistry.updateService();
60         } else {
61             starting = false;
62         }
63
64         LOG.debug("ModuleInfoBundleTracker open complete");
65     }
66
67     @Override
68     public void close() {
69         if (bundleTracker != null) {
70             bundleTracker.close();
71             bundleTracker = null;
72         }
73     }
74
75     @Override
76     @SuppressWarnings("IllegalCatch")
77     public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
78         URL resource = bundle.getEntry(YANG_MODULE_INFO_SERVICE_PATH);
79         LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
80         if (resource == null) {
81             return Collections.emptyList();
82         }
83         List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
84
85         try {
86             for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
87                 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
88                 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
89                 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
90             }
91
92             if (!starting) {
93                 moduleInfoRegistry.updateService();
94             }
95         } catch (final IOException e) {
96             LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
97         } catch (final RuntimeException e) {
98             LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
99         }
100
101         LOG.trace("Got following registrations {}", registrations);
102         return registrations;
103     }
104
105     @Override
106     public void modifiedBundle(final Bundle bundle, final BundleEvent event,
107             final Collection<ObjectRegistration<YangModuleInfo>> object) {
108     }
109
110     @Override
111     @SuppressWarnings("IllegalCatch")
112     public void removedBundle(final Bundle bundle, final BundleEvent event,
113             final Collection<ObjectRegistration<YangModuleInfo>> regs) {
114         if (regs == null) {
115             return;
116         }
117
118         for (ObjectRegistration<YangModuleInfo> reg : regs) {
119             try {
120                 reg.close();
121             } catch (final Exception e) {
122                 LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
123             }
124         }
125     }
126
127     private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
128         String errorMessage;
129         Class<?> clazz = loadClass(moduleInfoClass, bundle);
130
131         if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
132             errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
133                     YangModelBindingProvider.class, bundle);
134             throw new IllegalStateException(errorMessage);
135         }
136         final YangModelBindingProvider instance;
137         try {
138             Object instanceObj = clazz.newInstance();
139             instance = YangModelBindingProvider.class.cast(instanceObj);
140         } catch (final InstantiationException e) {
141             errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
142             throw new IllegalStateException(errorMessage, e);
143         } catch (final IllegalAccessException e) {
144             errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
145                     moduleInfoClass, bundle, e);
146             throw new IllegalStateException(errorMessage, e);
147         }
148
149         try {
150             return instance.getModuleInfo();
151         } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
152             throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
153         }
154     }
155
156     private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
157         try {
158             return bundle.loadClass(moduleInfoClass);
159         } catch (final ClassNotFoundException e) {
160             String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle,
161                     e);
162             throw new IllegalStateException(errorMessage);
163         }
164     }
165
166     public static String logMessage(final String slfMessage, final Object... params) {
167         LOG.info(slfMessage, params);
168         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
169         return String.format(formatMessage, params);
170     }
171 }