blob: 486cea574adc1ba499e8afb1a8752e60cffdc4c9 [file] [log] [blame]
Jian Li69600e02018-12-24 13:21:18 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
19import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider;
20import org.osgi.service.component.annotations.Activate;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27/**
28 * Abstract bootstrapper for loading and registering telemetry configurations
29 * that are independent from the default telemetry configurations.
30 */
31public abstract class AbstractTelemetryConfigLoader {
32
33 private final Logger log = LoggerFactory.getLogger(getClass());
34
35 private TelemetryConfigProvider provider;
36 private final String path;
37
38 @Reference(cardinality = ReferenceCardinality.MANDATORY)
39 protected TelemetryConfigAdminService configAdminService;
40
41 /**
42 * Creates a new loader for resource with the specified path.
43 *
44 * @param path configurations definition XML resource path
45 */
46 protected AbstractTelemetryConfigLoader(String path) {
47 this.path = path;
48 }
49
50 @Activate
51 protected void activate() {
52 try {
53 provider = new XmlTelemetryConfigLoader().loadTelemetryConfigs(
54 getClass().getResourceAsStream(path));
55 configAdminService.registerProvider(provider);
56 } catch (Exception e) {
57 log.error("Unable to load {} telemetry configuration definitions", path, e);
58 }
59 log.info("Started");
60 }
61
62 @Deactivate
63 protected void deactivate() {
64 configAdminService.unregisterProvider(provider);
65 log.info("Stopped");
66 }
67}