blob: 4629a4e3c599fde4998798b688d3994b3b15751b [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;
24import org.onosproject.openstacktelemetry.api.ByteBufferCodec;
25import org.onosproject.openstacktelemetry.api.FlowInfo;
26import org.onosproject.openstacktelemetry.api.GrpcTelemetryService;
27import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService;
28import org.onosproject.openstacktelemetry.api.KafkaTelemetryService;
29import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
30import org.onosproject.openstacktelemetry.api.RestTelemetryService;
31import org.onosproject.openstacktelemetry.api.TelemetryService;
32import org.onosproject.openstacktelemetry.codec.TinaFlowInfoByteBufferCodec;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.nio.ByteBuffer;
37import java.util.List;
38
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
48 private List<TelemetryService> telemetryServices = Lists.newArrayList();
49
50 @Activate
51 protected void activate() {
52 log.info("Started");
53 }
54
55 @Deactivate
56 protected void deactivate() {
57 log.info("Stopped");
58 }
59
60 @Override
61 public void addTelemetryService(TelemetryService telemetryService) {
62 telemetryServices.add(telemetryService);
63 }
64
65 @Override
66 public void removeTelemetryService(TelemetryService telemetryService) {
67 telemetryServices.remove(telemetryService);
68 }
69
70 @Override
71 public void publish(FlowInfo flowInfo) {
72 telemetryServices.forEach(service -> {
73
74
75 if (service instanceof GrpcTelemetryManager) {
76 invokeGrpcPublisher((GrpcTelemetryService) service, flowInfo);
77 }
78
79 if (service instanceof InfluxDbTelemetryManager) {
80 invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfo);
81 }
82
83 if (service instanceof KafkaTelemetryManager) {
84 invokeKafkaPublisher((KafkaTelemetryService) service, flowInfo);
85 }
86
87 if (service instanceof RestTelemetryManager) {
88 invokeRestPublisher((RestTelemetryService) service, flowInfo);
89 }
90
91 });
92 }
93
94 private void invokeGrpcPublisher(GrpcTelemetryService service, FlowInfo flowInfo) {
95 // TODO: need provide implementation
96 }
97
98 private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, FlowInfo flowInfo) {
99 // TODO: need provide implementation
100 }
101
102 private void invokeKafkaPublisher(KafkaTelemetryService service, FlowInfo flowInfo) {
103 ByteBufferCodec codec = new TinaFlowInfoByteBufferCodec();
104 ByteBuffer buffer = codec.encode(flowInfo);
105 service.publish(new ProducerRecord<>("sona.flow", "flowdata", buffer.array()));
106 }
107
108 private void invokeRestPublisher(RestTelemetryService service, FlowInfo flowInfo) {
109 // TODO: need provide implementation
110 }
111
112}