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

©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.