blob: cd587e94ce698681557928a79a0623b24e5e839a [file] [log] [blame]
Thomas Vachuska43de6ee2017-03-15 15:45:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Thomas Vachuska43de6ee2017-03-15 15:45:23 -07003 *
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 */
16package org.onosproject.net.driver;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26/**
27 * Abstract bootstrapper for loading and registering driver definitions that
28 * are independent from the default driver definitions.
29 */
30@Component
31public abstract class AbstractIndependentDriverLoader {
32
33 private final Logger log = LoggerFactory.getLogger(getClass());
34
35 private DriverProvider provider;
36 private final String path;
37
38 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
39 protected DriverAdminService driverAdminService;
40
41 /**
42 * Creates a new loader for resource with the specified path.
43 *
44 * @param path drivers definition XML resource path
45 */
46 protected AbstractIndependentDriverLoader(String path) {
47 this.path = path;
48 }
49
50 @Activate
51 protected void activate() {
52 try {
53 provider = new XmlDriverLoader(getClass().getClassLoader(), driverAdminService)
54 .loadDrivers(getClass().getResourceAsStream(path), driverAdminService);
55 driverAdminService.registerProvider(provider);
56 } catch (Exception e) {
57 log.error("Unable to load {} driver definitions", path, e);
58 }
59 log.info("Started");
60 }
61
62 @Deactivate
63 protected void deactivate() {
64 driverAdminService.unregisterProvider(provider);
65 log.info("Stopped");
66 }
67
68}