blob: 761e01bc02c13a6e8d37fba76c3761484fafb96c [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;
33import org.onosproject.openstacktelemetry.api.RestTelemetryService;
34import org.onosproject.openstacktelemetry.api.TelemetryService;
Jian Li0bbbb1c2018-06-22 22:01:17 +090035import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec;
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 */
51@Component(immediate = true)
52@Service
53public class OpenstackTelemetryManager implements OpenstackTelemetryService {
54
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
Jian Li6c92b3c2018-08-03 11:26:55 +090057 private static final String ENABLE_SERVICE = "enableService";
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected ComponentConfigService componentConfigService;
61
Jian Lid1ce10a2018-06-12 13:47:23 +090062 private List<TelemetryService> telemetryServices = Lists.newArrayList();
63
64 @Activate
65 protected void activate() {
66 log.info("Started");
67 }
68
69 @Deactivate
70 protected void deactivate() {
71 log.info("Stopped");
72 }
73
74 @Override
75 public void addTelemetryService(TelemetryService telemetryService) {
76 telemetryServices.add(telemetryService);
77 }
78
79 @Override
80 public void removeTelemetryService(TelemetryService telemetryService) {
81 telemetryServices.remove(telemetryService);
82 }
83
84 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +090085 public void publish(Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +090086 telemetryServices.forEach(service -> {
Jian Li6c92b3c2018-08-03 11:26:55 +090087 if (service instanceof GrpcTelemetryManager &&
88 getPropertyValueAsBoolean(componentConfigService.getProperties(
89 GrpcTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090090 invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090091 }
92
Jian Li6c92b3c2018-08-03 11:26:55 +090093 if (service instanceof InfluxDbTelemetryManager &&
94 getPropertyValueAsBoolean(componentConfigService.getProperties(
95 InfluxDbTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +090096 invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +090097 }
98
Jian Li6c92b3c2018-08-03 11:26:55 +090099 if (service instanceof KafkaTelemetryManager &&
100 getPropertyValueAsBoolean(componentConfigService.getProperties(
101 KafkaTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900102 invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900103 }
104
Jian Li6c92b3c2018-08-03 11:26:55 +0900105 if (service instanceof RestTelemetryManager &&
106 getPropertyValueAsBoolean(componentConfigService.getProperties(
107 RestTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) {
Jian Li0bbbb1c2018-06-22 22:01:17 +0900108 invokeRestPublisher((RestTelemetryService) service, flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900109 }
Jian Lia4947682018-07-07 14:53:32 +0900110
111 log.trace("Publishing Flow Infos {}", flowInfos);
Jian Lid1ce10a2018-06-12 13:47:23 +0900112 });
113 }
114
Jian Liae3fcff2018-07-30 11:55:44 +0900115 @Override
116 public Set<TelemetryService> telemetryServices() {
117 return ImmutableSet.copyOf(telemetryServices);
118 }
119
Jian Li0bbbb1c2018-06-22 22:01:17 +0900120 private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900121 // TODO: need provide implementation
122 }
123
Jian Li0bbbb1c2018-06-22 22:01:17 +0900124 private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) {
Boyoung Jeong4d1c9d12018-07-20 17:09:20 +0900125 DefaultInfluxRecord<String, Set<FlowInfo>> influxRecord
126 = new DefaultInfluxRecord<>(DEFAULT_INFLUXDB_MEASUREMENT, flowInfos);
127 service.publish(influxRecord);
Jian Lid1ce10a2018-06-12 13:47:23 +0900128 }
129
Jian Li0bbbb1c2018-06-22 22:01:17 +0900130 private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) {
131 TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec();
132 ByteBuffer buffer = codec.encode(flowInfos);
133 service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array()));
Jian Lid1ce10a2018-06-12 13:47:23 +0900134 }
135
Jian Li0bbbb1c2018-06-22 22:01:17 +0900136 private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) {
Jian Lid1ce10a2018-06-12 13:47:23 +0900137 // TODO: need provide implementation
138 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900139}