blob: 3e629cc4eac8ba4304f4580b162d9f4e0bf7a505 [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;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Jian Li6c92b3c2018-08-03 11:26:55 +090023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Lid1ce10a2018-06-12 13:47:23 +090025import org.apache.felix.scr.annotations.Service;
26import org.apache.kafka.clients.producer.ProducerRecord;
Jian Li6c92b3c2018-08-03 11:26:55 +090027import org.onosproject.cfg.ComponentConfigService;
Jian Lid1ce10a2018-06-12 13:47:23 +090028import org.onosproject.openstacktelemetry.api.FlowInfo;
29import org.onosproject.openstacktelemetry.api.GrpcTelemetryService;
30import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService;
31import org.onosproject.openstacktelemetry.api.KafkaTelemetryService;
32import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
boyoung21c5f5f42018-09-27 20:29:41 +090033import org.onosproject.openstacktelemetry.api.PrometheusTelemetryService;
Jian Lid1ce10a2018-06-12 13:47:23 +090034import org.onosproject.openstacktelemetry.api.RestTelemetryService;
35import org.onosproject.openstacktelemetry.api.TelemetryService;
Jian Li0bbbb1c2018-06-22 22:01:17 +090036import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec;
Jian Lid1ce10a2018-06-12 13:47:23 +090037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.nio.ByteBuffer;
41import java.util.List;
Jian Li0bbbb1c2018-06-22 22:01:17 +090042import java.util.Set;
Jian Lid1ce10a2018-06-12 13:47:23 +090043
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +090044import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT;
Jian Lia4947682018-07-07 14:53:32 +090045import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_KEY;
46import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_TOPIC;
Jian Li6c92b3c2018-08-03 11:26:55 +090047import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getPropertyValueAsBoolean;
Jian Lia4947682018-07-07 14:53:32 +090048
Jian Lid1ce10a2018-06-12 13:47:23 +090049/**
50 * Openstack telemetry manager.
51 */
52@Component(immediate = true)
53@Service
54public class OpenstackTelemetryManager implements OpenstackTelemetryService {
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
Jian Li6c92b3c2018-08-03 11:26:55 +090058 private static final String ENABLE_SERVICE = "enableService";
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected ComponentConfigService componentConfigService;
62
Jian Lid1ce10a2018-06-12 13:47:23 +090063 private List<TelemetryService> telemetryServices = Lists.newArrayList();
64
65 @Activate
66 protected void activate() {
67 log.info("Started");
68 }
69
70 @Deactivate
71 protected void deactivate() {
72 log.info("Stopped");
73 }
74
75 @Override
76 public void addTelemetryService(TelemetryService telemetryService) {
77 telemetryServices.add(telemetryService);
78 }
79
80 @Override
81 public void removeTelemetryService(TelemetryService telemetryService) {
82 telemetryServices.remove(telemetryService);
83 }
84
85 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +090086 public void publish(Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +090087 telemetryServices.forEach(service -> {
Jian Li6c92b3c2018-08-03 11:26:55 +090088 if (service instanceof GrpcTelemetryManager &&
89 getPropertyValueAsBoolean(componentConfigService.getProperties(
90 GrpcTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090091 invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090092 }
93
Jian Li6c92b3c2018-08-03 11:26:55 +090094 if (service instanceof InfluxDbTelemetryManager &&
95 getPropertyValueAsBoolean(componentConfigService.getProperties(
96 InfluxDbTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090097 invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090098 }
99
boyoung21c5f5f42018-09-27 20:29:41 +0900100 if (service instanceof PrometheusTelemetryManager &&
101 getPropertyValueAsBoolean(componentConfigService.getProperties(
102 PrometheusTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
103 invokePrometheusPublisher((PrometheusTelemetryService) service, flowInfos);
104 }
105
Jian Li6c92b3c2018-08-03 11:26:55 +0900106 if (service instanceof KafkaTelemetryManager &&
107 getPropertyValueAsBoolean(componentConfigService.getProperties(
108 KafkaTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900109 invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900110 }
111
Jian Li6c92b3c2018-08-03 11:26:55 +0900112 if (service instanceof RestTelemetryManager &&
113 getPropertyValueAsBoolean(componentConfigService.getProperties(
114 RestTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900115 invokeRestPublisher((RestTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900116 }
Jian Lia4947682018-07-07 14:53:32 +0900117
118 log.trace("Publishing Flow Infos {}", flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900119 });
120 }
121
Jian Liae3fcff2018-07-30 11:55:44 +0900122 @Override
123 public Set<TelemetryService> telemetryServices() {
124 return ImmutableSet.copyOf(telemetryServices);
125 }
126
Jian Li0bbbb1c2018-06-22 22:01:17 +0900127 private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900128 // TODO: need provide implementation
129 }
130
Jian Li0bbbb1c2018-06-22 22:01:17 +0900131 private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) {
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900132 DefaultInfluxRecord<String, Set<FlowInfo>> influxRecord
133 = new DefaultInfluxRecord<>(DEFAULT_INFLUXDB_MEASUREMENT, flowInfos);
134 service.publish(influxRecord);
Jian Lid1ce10a2018-06-12 13:47:23 +0900135 }
136
boyoung21c5f5f42018-09-27 20:29:41 +0900137 private void invokePrometheusPublisher(PrometheusTelemetryService service, Set<FlowInfo> flowInfos) {
138 service.publish(flowInfos);
139 }
140
Jian Li0bbbb1c2018-06-22 22:01:17 +0900141 private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) {
142 TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec();
143 ByteBuffer buffer = codec.encode(flowInfos);
144 service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array()));
Jian Lid1ce10a2018-06-12 13:47:23 +0900145 }
146
Jian Li0bbbb1c2018-06-22 22:01:17 +0900147 private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900148 // TODO: need provide implementation
149 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900150}