BUG-2839: remove dependencies on commons-io
[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 import com.google.common.base.Charsets;
12 import com.google.common.io.Resources;
13 import java.io.IOException;
14 import java.net.URL;
15 import java.util.Collection;
16 import java.util.LinkedList;
17 import java.util.List;
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 {
54             for (String moduleInfoName : Resources.readLines(resource, Charsets.UTF_8)) {
55                 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
56                 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
57                 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
58             }
59         } catch (IOException e) {
60             LOG.error("Error while reading {}", resource, e);
61             throw new RuntimeException(e);
62         }
63
64         LOG.trace("Got following registrations {}", registrations);
65         return registrations;
66     }
67
68     @Override
69     public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
70     }
71
72     @Override
73     public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
74         if(regs == null) {
75             return;
76         }
77
78         for (ObjectRegistration<YangModuleInfo> reg : regs) {
79             try {
80                 reg.close();
81             } catch (Exception e) {
82                 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
83             }
84         }
85     }
86
87     private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
88         String errorMessage;
89         Class<?> clazz = loadClass(moduleInfoClass, bundle);
90
91         if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
92             errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
93             throw new IllegalStateException(errorMessage);
94         }
95         YangModelBindingProvider instance;
96         try {
97             Object instanceObj = clazz.newInstance();
98             instance = YangModelBindingProvider.class.cast(instanceObj);
99         } catch (InstantiationException e) {
100             errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
101             throw new IllegalStateException(errorMessage, e);
102         } catch (IllegalAccessException e) {
103             errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
104                     moduleInfoClass, bundle, e);
105             throw new IllegalStateException(errorMessage, e);
106         }
107         try{
108             return instance.getModuleInfo();
109         } catch (NoClassDefFoundError e) {
110
111
112             LOG.error("Error while executing getModuleInfo on {}", instance, e);
113             throw e;
114         }
115     }
116
117     private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
118         try {
119             return bundle.loadClass(moduleInfoClass);
120         } catch (ClassNotFoundException e) {
121             String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
122             throw new IllegalStateException(errorMessage);
123         }
124     }
125
126     public static String logMessage(String slfMessage, Object... params) {
127         LOG.info(slfMessage, params);
128         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
129         return format(formatMessage, params);
130     }
131 }