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