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