blob: dcc7023d4109ea3fffcc1c60e393d34b4eb29cf7 [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 Milkey8e406512018-10-24 15:56:50 -070017
boyoung21c5f5f42018-09-27 20:29:41 +090018import org.onlab.util.Tools;
19import org.onosproject.cfg.ComponentConfigService;
20import org.onosproject.openstacktelemetry.api.PrometheusTelemetryAdminService;
21import org.onosproject.openstacktelemetry.api.PrometheusTelemetryConfigService;
22import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
23import org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig;
24import org.osgi.service.component.ComponentContext;
Ray Milkey8e406512018-10-24 15:56:50 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Modified;
29import org.osgi.service.component.annotations.Reference;
30import org.osgi.service.component.annotations.ReferenceCardinality;
boyoung21c5f5f42018-09-27 20:29:41 +090031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Dictionary;
35
boyoung2a8549d22018-11-23 20:42:37 +090036import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT;
37import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT;
38import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT;
Ray Milkey8e406512018-10-24 15:56:50 -070039import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_ENABLE_SERVICE;
40import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_ADDRESS;
41import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_PORT;
boyoung21c5f5f42018-09-27 20:29:41 +090042import 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 */
Ray Milkey8e406512018-10-24 15:56:50 -070048@Component(
49 immediate = true,
50 service = PrometheusTelemetryConfigService.class,
51 property = {
boyoung2a8549d22018-11-23 20:42:37 +090052 PROP_PROMETHEUS_ENABLE_SERVICE + ":Boolean=" + PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT,
53 PROP_PROMETHEUS_EXPORTER_ADDRESS + "=" + PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT,
54 PROP_PROMETHEUS_EXPORTER_PORT + ":Integer=" + PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT
Ray Milkey8e406512018-10-24 15:56:50 -070055 }
56)
boyoung21c5f5f42018-09-27 20:29:41 +090057public class PrometheusTelemetryConfigManager implements PrometheusTelemetryConfigService {
58
59 private final Logger log = LoggerFactory.getLogger(getClass());
60
Ray Milkeydf521292018-10-04 15:13:33 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
boyoung21c5f5f42018-09-27 20:29:41 +090062 protected ComponentConfigService componentConfigService;
63
Ray Milkeydf521292018-10-04 15:13:33 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY)
boyoung21c5f5f42018-09-27 20:29:41 +090065 protected PrometheusTelemetryAdminService prometheusTelemetryAdminService;
66
Ray Milkey8e406512018-10-24 15:56:50 -070067 /** Default IP address of prometheus exporter. */
boyoung2a8549d22018-11-23 20:42:37 +090068 protected String address = PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +090069
Ray Milkey8e406512018-10-24 15:56:50 -070070 /** Default port number of prometheus exporter. */
boyoung2a8549d22018-11-23 20:42:37 +090071 protected Integer port = PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +090072
Ray Milkey8e406512018-10-24 15:56:50 -070073 /** Specify the default behavior of telemetry service. */
boyoung2a8549d22018-11-23 20:42:37 +090074 protected Boolean enableService = PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +090075
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
Ray Milkey8e406512018-10-24 15:56:50 -0700117 String addressStr = Tools.get(properties, PROP_PROMETHEUS_EXPORTER_ADDRESS);
boyoung2a8549d22018-11-23 20:42:37 +0900118 address = addressStr != null ? addressStr : PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +0900119 log.info("Configured. Prometheus exporter address is {}", address);
120
Ray Milkey8e406512018-10-24 15:56:50 -0700121 Integer portConfigured = Tools.getIntegerProperty(properties, PROP_PROMETHEUS_EXPORTER_PORT);
boyoung21c5f5f42018-09-27 20:29:41 +0900122 if (portConfigured == null) {
boyoung2a8549d22018-11-23 20:42:37 +0900123 port = PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +0900124 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
Ray Milkey8e406512018-10-24 15:56:50 -0700130 Boolean enableServiceConfigured = getBooleanProperty(properties, PROP_PROMETHEUS_ENABLE_SERVICE);
boyoung21c5f5f42018-09-27 20:29:41 +0900131 if (enableServiceConfigured == null) {
boyoung2a8549d22018-11-23 20:42:37 +0900132 enableService = PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT;
boyoung21c5f5f42018-09-27 20:29:41 +0900133 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}