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