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