blob: 23cc240ff048d39bc4bcf3a9742836b2aafd3e1e [file] [log] [blame]
boyoung21c5f5f42018-09-27 20:29:41 +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.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.Modified;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.util.Tools;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.openstacktelemetry.api.PrometheusTelemetryAdminService;
29import org.onosproject.openstacktelemetry.api.PrometheusTelemetryConfigService;
30import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
31import org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig;
32import org.osgi.service.component.ComponentContext;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Dictionary;
37
38import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
39import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_ENABLE;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_PROMETHEUS_EXPORTER_IP;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_PROMETHEUS_EXPORTER_PORT;
42import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
43import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
44
45/**
46 * Prometheus exporter configuration manager for publishing openstack telemetry.
47 */
48@Component(immediate = true)
49@Service
50public class PrometheusTelemetryConfigManager implements PrometheusTelemetryConfigService {
51
52 private final Logger log = LoggerFactory.getLogger(getClass());
53
54 private static final String ENABLE_SERVICE = "enableService";
55 private static final String ADDRESS = "address";
56 private static final String PORT = "port";
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ComponentConfigService componentConfigService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected PrometheusTelemetryAdminService prometheusTelemetryAdminService;
63
64 @Property(name = ADDRESS, value = DEFAULT_PROMETHEUS_EXPORTER_IP,
65 label = "Default IP address of prometheus exporter")
66 protected String address = DEFAULT_PROMETHEUS_EXPORTER_IP;
67
68 @Property(name = PORT, intValue = DEFAULT_PROMETHEUS_EXPORTER_PORT,
69 label = "Default port number of prometheus exporter")
70 protected Integer port = DEFAULT_PROMETHEUS_EXPORTER_PORT;
71
72 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_ENABLE,
73 label = "Specify the default behavior of telemetry service")
74 protected Boolean enableService = DEFAULT_ENABLE;
75
76 @Activate
77 protected void activate(ComponentContext context) {
78 componentConfigService.registerProperties(getClass());
79 if (enableService) {
80 prometheusTelemetryAdminService.start(getConfig());
81 }
82 log.info("Started");
83 }
84
85 @Deactivate
86 protected void deactivate() {
87 componentConfigService.unregisterProperties(getClass(), false);
88 if (enableService) {
89 prometheusTelemetryAdminService.stop();
90 }
91 log.info("Stopped");
92 }
93
94 @Modified
95 private void modified(ComponentContext context) {
96 readComponentConfiguration(context);
97 initTelemetryService(prometheusTelemetryAdminService, getConfig(), enableService);
98 log.info("Modified");
99 }
100
101 @Override
102 public TelemetryConfig getConfig() {
103 return new DefaultPrometheusTelemetryConfig.DefaultBuilder()
104 .withAddress(address)
105 .withPort(port)
106 .build();
107 }
108
109 /**
110 * Extracts properties from the component configuration context.
111 *
112 * @param context the component context
113 */
114 private void readComponentConfiguration(ComponentContext context) {
115 Dictionary<?, ?> properties = context.getProperties();
116
117 String addressStr = Tools.get(properties, ADDRESS);
118 address = addressStr != null ? addressStr : DEFAULT_PROMETHEUS_EXPORTER_IP;
119 log.info("Configured. Prometheus exporter address is {}", address);
120
121 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
122 if (portConfigured == null) {
123 port = DEFAULT_PROMETHEUS_EXPORTER_PORT;
124 log.info("Prometheus exporter port is NOT configured, default value is {}", port);
125 } else {
126 port = portConfigured;
127 log.info("Configured. Prometheus exporter port is {}", port);
128 }
129
130 Boolean enableServiceConfigured = getBooleanProperty(properties, ENABLE_SERVICE);
131 if (enableServiceConfigured == null) {
132 enableService = DEFAULT_DISABLE;
133 log.info("Prometheus service enable flag is NOT " +
134 "configured, default value is {}", enableService);
135 } else {
136 enableService = enableServiceConfigured;
137 log.info("Configured. Prometheus service enable flag is {}", enableService);
138 }
139 }
140}