blob: 783dbd66b17a5031dfb003a12be0739912d255a7 [file] [log] [blame]
Jian Lid1ce10a2018-06-12 13:47:23 +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
Jian Liae3fcff2018-07-30 11:55:44 +090018import com.google.common.collect.ImmutableSet;
Jian Lid1ce10a2018-06-12 13:47:23 +090019import com.google.common.collect.Lists;
Jian Lid1ce10a2018-06-12 13:47:23 +090020import org.apache.kafka.clients.producer.ProducerRecord;
Jian Li6c92b3c2018-08-03 11:26:55 +090021import org.onosproject.cfg.ComponentConfigService;
Jian Lid1ce10a2018-06-12 13:47:23 +090022import org.onosproject.openstacktelemetry.api.FlowInfo;
23import org.onosproject.openstacktelemetry.api.GrpcTelemetryService;
24import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService;
25import org.onosproject.openstacktelemetry.api.KafkaTelemetryService;
26import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
boyoung21c5f5f42018-09-27 20:29:41 +090027import org.onosproject.openstacktelemetry.api.PrometheusTelemetryService;
Jian Lid1ce10a2018-06-12 13:47:23 +090028import org.onosproject.openstacktelemetry.api.RestTelemetryService;
29import org.onosproject.openstacktelemetry.api.TelemetryService;
Jian Li0bbbb1c2018-06-22 22:01:17 +090030import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
Jian Lid1ce10a2018-06-12 13:47:23 +090036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.nio.ByteBuffer;
40import java.util.List;
Jian Li0bbbb1c2018-06-22 22:01:17 +090041import java.util.Set;
Jian Lid1ce10a2018-06-12 13:47:23 +090042
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090043import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT;
Jian Lia4947682018-07-07 14:53:32 +090044import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_KEY;
45import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_TOPIC;
Jian Li6c92b3c2018-08-03 11:26:55 +090046import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getPropertyValueAsBoolean;
Jian Lia4947682018-07-07 14:53:32 +090047
Jian Lid1ce10a2018-06-12 13:47:23 +090048/**
49 * Openstack telemetry manager.
50 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051@Component(immediate = true, service = OpenstackTelemetryService.class)
Jian Lid1ce10a2018-06-12 13:47:23 +090052public class OpenstackTelemetryManager implements OpenstackTelemetryService {
53
54 private final Logger log = LoggerFactory.getLogger(getClass());
55
Jian Li6c92b3c2018-08-03 11:26:55 +090056 private static final String ENABLE_SERVICE = "enableService";
57
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li6c92b3c2018-08-03 11:26:55 +090059 protected ComponentConfigService componentConfigService;
60
Jian Lid1ce10a2018-06-12 13:47:23 +090061 private List<TelemetryService> telemetryServices = Lists.newArrayList();
62
63 @Activate
64 protected void activate() {
65 log.info("Started");
66 }
67
68 @Deactivate
69 protected void deactivate() {
70 log.info("Stopped");
71 }
72
73 @Override
74 public void addTelemetryService(TelemetryService telemetryService) {
75 telemetryServices.add(telemetryService);
76 }
77
78 @Override
79 public void removeTelemetryService(TelemetryService telemetryService) {
80 telemetryServices.remove(telemetryService);
81 }
82
83 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +090084 public void publish(Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +090085 telemetryServices.forEach(service -> {
Jian Li6c92b3c2018-08-03 11:26:55 +090086 if (service instanceof GrpcTelemetryManager &&
87 getPropertyValueAsBoolean(componentConfigService.getProperties(
88 GrpcTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090089 invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090090 }
91
Jian Li6c92b3c2018-08-03 11:26:55 +090092 if (service instanceof InfluxDbTelemetryManager &&
93 getPropertyValueAsBoolean(componentConfigService.getProperties(
94 InfluxDbTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090095 invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090096 }
97
boyoung21c5f5f42018-09-27 20:29:41 +090098 if (service instanceof PrometheusTelemetryManager &&
99 getPropertyValueAsBoolean(componentConfigService.getProperties(
100 PrometheusTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
101 invokePrometheusPublisher((PrometheusTelemetryService) service, flowInfos);
102 }
103
Jian Li6c92b3c2018-08-03 11:26:55 +0900104 if (service instanceof KafkaTelemetryManager &&
105 getPropertyValueAsBoolean(componentConfigService.getProperties(
106 KafkaTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900107 invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900108 }
109
Jian Li6c92b3c2018-08-03 11:26:55 +0900110 if (service instanceof RestTelemetryManager &&
111 getPropertyValueAsBoolean(componentConfigService.getProperties(
112 RestTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900113 invokeRestPublisher((RestTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900114 }
Jian Lia4947682018-07-07 14:53:32 +0900115
116 log.trace("Publishing Flow Infos {}", flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900117 });
118 }
119
Jian Liae3fcff2018-07-30 11:55:44 +0900120 @Override
121 public Set<TelemetryService> telemetryServices() {
122 return ImmutableSet.copyOf(telemetryServices);
123 }
124
Jian Li0bbbb1c2018-06-22 22:01:17 +0900125 private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900126 // TODO: need provide implementation
127 }
128
Jian Li0bbbb1c2018-06-22 22:01:17 +0900129 private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) {
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900130 DefaultInfluxRecord<String, Set<FlowInfo>> influxRecord
131 = new DefaultInfluxRecord<>(DEFAULT_INFLUXDB_MEASUREMENT, flowInfos);
132 service.publish(influxRecord);
Jian Lid1ce10a2018-06-12 13:47:23 +0900133 }
134
boyoung21c5f5f42018-09-27 20:29:41 +0900135 private void invokePrometheusPublisher(PrometheusTelemetryService service, Set<FlowInfo> flowInfos) {
136 service.publish(flowInfos);
137 }
138
Jian Li0bbbb1c2018-06-22 22:01:17 +0900139 private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) {
140 TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec();
141 ByteBuffer buffer = codec.encode(flowInfos);
142 service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array()));
Jian Lid1ce10a2018-06-12 13:47:23 +0900143 }
144
Jian Li0bbbb1c2018-06-22 22:01:17 +0900145 private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900146 // TODO: need provide implementation
147 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900148}