48fdd8855de12c9735a9a10a25ae5b5a9382b7f0
[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 static java.lang.String.format;
11
12 import java.io.InputStream;
13 import java.net.URL;
14 import java.util.Collection;
15 import java.util.LinkedList;
16 import java.util.List;
17
18 import org.apache.commons.io.IOUtils;
19 import org.opendaylight.yangtools.concepts.ObjectRegistration;
20 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
21 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleEvent;
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 BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
33
34     private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
35
36     public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
37
38
39     private final ModuleInfoRegistry moduleInfoRegistry;
40
41     public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
42         this.moduleInfoRegistry = moduleInfoRegistry;
43     }
44
45     @Override
46     public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
47         URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
48         logger.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
49         if(resource==null) {
50             return null;
51         }
52         List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
53
54         try (InputStream inputStream = resource.openStream()) {
55             List<String> lines = IOUtils.readLines(inputStream);
56             for (String moduleInfoName : lines) {
57                 logger.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
58                 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
59                 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
60             }
61
62         } catch (Exception e) {
63             logger.error("Error while reading {}", resource, e);
64             throw new RuntimeException(e);
65         }
66         logger.trace("Got following registrations {}", registrations);
67         return registrations;
68     }
69
70     @Override
71     public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
72     }
73
74     @Override
75     public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
76         if(regs == null) {
77             return;
78         }
79
80         for (ObjectRegistration<YangModuleInfo> reg : regs) {
81             try {
82                 reg.close();
83             } catch (Exception e) {
84                 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
85             }
86         }
87     }
88
89     private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
90         String errorMessage;
91         Class<?> clazz = loadClass(moduleInfoClass, bundle);
92
93         if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
94             errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
95             throw new IllegalStateException(errorMessage);
96         }
97         YangModelBindingProvider instance;
98         try {
99             Object instanceObj = clazz.newInstance();
100             instance = YangModelBindingProvider.class.cast(instanceObj);
101         } catch (InstantiationException e) {
102             errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
103             throw new IllegalStateException(errorMessage, e);
104         } catch (IllegalAccessException e) {
105             errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
106                     moduleInfoClass, bundle, e);
107             throw new IllegalStateException(errorMessage, e);
108         }
109         try{
110             return instance.getModuleInfo();
111         } catch (NoClassDefFoundError e) {
112
113
114             logger.error("Error while executing getModuleInfo on {}", instance, e);
115             throw e;
116         }
117     }
118
119     private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
120         try {
121             return bundle.loadClass(moduleInfoClass);
122         } catch (ClassNotFoundException e) {
123             String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
124             throw new IllegalStateException(errorMessage);
125         }
126     }
127
128     public static String logMessage(String slfMessage, Object... params) {
129         logger.info(slfMessage, params);
130         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
131         return format(formatMessage, params);
132     }
133 }