blob: fa8d9fe62682557c2159ec46f4e02889793bd34e [file] [log] [blame]
Jian Li6803ccd2018-06-08 09:26:09 +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;
Jian Lid1ce10a2018-06-12 13:47:23 +090021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li6803ccd2018-06-08 09:26:09 +090023import org.apache.felix.scr.annotations.Service;
24import org.influxdb.InfluxDB;
25import org.influxdb.InfluxDBFactory;
26import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService;
27import org.onosproject.openstacktelemetry.api.InfluxRecord;
Jian Lid1ce10a2018-06-12 13:47:23 +090028import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
Jian Li6803ccd2018-06-08 09:26:09 +090029import org.onosproject.openstacktelemetry.api.config.InfluxDbTelemetryConfig;
30import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34/**
35 * InfluxDB telemetry manager.
36 */
37@Component(immediate = true)
38@Service
39public class InfluxDbTelemetryManager implements InfluxDbTelemetryAdminService {
40
41 private final Logger log = LoggerFactory.getLogger(getClass());
42
Jian Lid1ce10a2018-06-12 13:47:23 +090043 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 protected OpenstackTelemetryService openstackTelemetryService;
45
Jian Li6803ccd2018-06-08 09:26:09 +090046 private static final String PROTOCOL = "http";
47 private InfluxDB producer = null;
48
49 @Activate
50 protected void activate() {
Jian Lid1ce10a2018-06-12 13:47:23 +090051
52 openstackTelemetryService.addTelemetryService(this);
53
Jian Li6803ccd2018-06-08 09:26:09 +090054 log.info("Started");
55 }
56
57 @Deactivate
58 protected void deactivate() {
59 stop();
Jian Lid1ce10a2018-06-12 13:47:23 +090060
61 openstackTelemetryService.removeTelemetryService(this);
62
Jian Li6803ccd2018-06-08 09:26:09 +090063 log.info("Stopped");
64 }
65
66 @Override
67 public void start(TelemetryConfig config) {
68 if (producer != null) {
69 log.info("InfluxDB producer has already been started");
70 return;
71 }
72
73 InfluxDbTelemetryConfig influxDbConfig = (InfluxDbTelemetryConfig) config;
74
75 StringBuilder influxDbServerBuilder = new StringBuilder();
76 influxDbServerBuilder.append(PROTOCOL);
77 influxDbServerBuilder.append(":");
78 influxDbServerBuilder.append("//");
79 influxDbServerBuilder.append(influxDbConfig.address());
80 influxDbServerBuilder.append(":");
81 influxDbServerBuilder.append(influxDbConfig.port());
82
83 producer = InfluxDBFactory.connect(influxDbServerBuilder.toString(),
84 influxDbConfig.username(), influxDbConfig.password());
85 log.info("InfluxDB producer has Started");
86 }
87
88 @Override
89 public void stop() {
90 if (producer != null) {
91 producer = null;
92 }
93
94 log.info("Kafka producer has Stopped");
95 }
96
97 @Override
98 public void restart(TelemetryConfig config) {
99 stop();
100 start(config);
101 }
102
103 @Override
104 public void publish(InfluxRecord<String, Object> record) {
105 // TODO: need to find a way to invoke InfluxDB endpoint using producer
Jian Lid1ce10a2018-06-12 13:47:23 +0900106
107 if (producer == null) {
108 log.warn("InfluxDB telemetry service has not been enabled!");
109 }
110 }
111
112 @Override
113 public boolean isRunning() {
114 return producer != null;
Jian Li6803ccd2018-06-08 09:26:09 +0900115 }
116}