blob: e07c65eba69c82700bd39f0c3b772cf7d7092ec1 [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Andrea Campanella238d96e2016-01-20 11:52:02 -080016package org.onosproject.net.driver;
alshabib77b88482015-04-07 15:47:50 -070017
18import org.apache.felix.scr.annotations.Activate;
Jonathan Hart17d00452015-04-21 17:10:00 -070019import org.apache.felix.scr.annotations.Component;
alshabib77b88482015-04-07 15:47:50 -070020import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070026import java.io.InputStream;
alshabib77b88482015-04-07 15:47:50 -070027
28/**
29 * Bootstrap for built in drivers.
30 */
Andrea Campanella238d96e2016-01-20 11:52:02 -080031@Component
32public abstract class AbstractDriverLoader {
alshabib77b88482015-04-07 15:47:50 -070033
34 private final Logger log = LoggerFactory.getLogger(getClass());
35
Andrea Campanella238d96e2016-01-20 11:52:02 -080036 //private static final String DRIVERS_XML = "/onos-drivers.xml";
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070037
alshabib77b88482015-04-07 15:47:50 -070038 private DriverProvider provider;
39
Thomas Vachuska866b46a2015-04-30 00:26:55 -070040 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 protected DriverAdminService driverAdminService;
42
alshabib77b88482015-04-07 15:47:50 -070043 @Activate
44 protected void activate() {
alshabib77b88482015-04-07 15:47:50 -070045 try {
Andrea Campanella238d96e2016-01-20 11:52:02 -080046 provider = new XmlDriverLoader(getClassLoaderInstance())
Jonathan Hart51539b82015-10-29 09:53:04 -070047 .loadDrivers(loadXmlDriversStream(), driverAdminService);
Thomas Vachuska866b46a2015-04-30 00:26:55 -070048 driverAdminService.registerProvider(provider);
Thomas Vachuska635c2d72015-05-08 14:32:13 -070049 } catch (Exception e) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070050 log.error("Unable to load default drivers", e);
alshabib77b88482015-04-07 15:47:50 -070051 }
alshabib77b88482015-04-07 15:47:50 -070052 log.info("Started");
53 }
54
55 @Deactivate
56 protected void deactivate() {
Thomas Vachuska866b46a2015-04-30 00:26:55 -070057 driverAdminService.unregisterProvider(provider);
alshabib77b88482015-04-07 15:47:50 -070058 log.info("Stopped");
59 }
60
Jonathan Hart51539b82015-10-29 09:53:04 -070061 protected abstract InputStream loadXmlDriversStream();
Andrea Campanella238d96e2016-01-20 11:52:02 -080062
63 protected abstract ClassLoader getClassLoaderInstance();
64
alshabib77b88482015-04-07 15:47:50 -070065}