blob: da492a9aa1237386b18d0ef0d88a5d367e4b24c9 [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
18import com.google.common.collect.Lists;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
23import org.apache.kafka.clients.producer.ProducerRecord;
Jian Lid1ce10a2018-06-12 13:47:23 +090024import org.onosproject.openstacktelemetry.api.FlowInfo;
25import org.onosproject.openstacktelemetry.api.GrpcTelemetryService;
26import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService;
27import org.onosproject.openstacktelemetry.api.KafkaTelemetryService;
28import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
29import org.onosproject.openstacktelemetry.api.RestTelemetryService;
30import org.onosproject.openstacktelemetry.api.TelemetryService;
Jian Li0bbbb1c2018-06-22 22:01:17 +090031import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec;
Jian Lid1ce10a2018-06-12 13:47:23 +090032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.nio.ByteBuffer;
36import java.util.List;
Jian Li0bbbb1c2018-06-22 22:01:17 +090037import java.util.Set;
Jian Lid1ce10a2018-06-12 13:47:23 +090038
39/**
40 * Openstack telemetry manager.
41 */
42@Component(immediate = true)
43@Service
44public class OpenstackTelemetryManager implements OpenstackTelemetryService {
45
46 private final Logger log = LoggerFactory.getLogger(getClass());
47
Jian Li0bbbb1c2018-06-22 22:01:17 +090048 private static final String KAFKA_TOPIC = "sona.flow";
49 private static final String KAFKA_KEY = "flowdata";
50
Jian Lid1ce10a2018-06-12 13:47:23 +090051 private List<TelemetryService> telemetryServices = Lists.newArrayList();
52
53 @Activate
54 protected void activate() {
55 log.info("Started");
56 }
57
58 @Deactivate
59 protected void deactivate() {
60 log.info("Stopped");
61 }
62
63 @Override
64 public void addTelemetryService(TelemetryService telemetryService) {
65 telemetryServices.add(telemetryService);
66 }
67
68 @Override
69 public void removeTelemetryService(TelemetryService telemetryService) {
70 telemetryServices.remove(telemetryService);
71 }
72
73 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +090074 public void publish(Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +090075 telemetryServices.forEach(service -> {
76
Jian Lid1ce10a2018-06-12 13:47:23 +090077 if (service instanceof GrpcTelemetryManager) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090078 invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090079 }
80
81 if (service instanceof InfluxDbTelemetryManager) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090082 invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090083 }
84
85 if (service instanceof KafkaTelemetryManager) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090086 invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090087 }
88
89 if (service instanceof RestTelemetryManager) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090090 invokeRestPublisher((RestTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090091 }
Jian Lid1ce10a2018-06-12 13:47:23 +090092 });
93 }
94
Jian Li0bbbb1c2018-06-22 22:01:17 +090095 private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +090096 // TODO: need provide implementation
97 }
98
Jian Li0bbbb1c2018-06-22 22:01:17 +090099 private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900100 // TODO: need provide implementation
101 }
102
Jian Li0bbbb1c2018-06-22 22:01:17 +0900103 private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) {
104 TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec();
105 ByteBuffer buffer = codec.encode(flowInfos);
106 service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array()));
Jian Lid1ce10a2018-06-12 13:47:23 +0900107 }
108
Jian Li0bbbb1c2018-06-22 22:01:17 +0900109 private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900110 // TODO: need provide implementation
111 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900112}