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