Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.apache.kafka.clients.producer.ProducerRecord; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 25 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryService; |
| 26 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService; |
| 27 | import org.onosproject.openstacktelemetry.api.KafkaTelemetryService; |
| 28 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
| 29 | import org.onosproject.openstacktelemetry.api.RestTelemetryService; |
| 30 | import org.onosproject.openstacktelemetry.api.TelemetryService; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.nio.ByteBuffer; |
| 36 | import java.util.List; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 37 | import java.util.Set; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 38 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 40 | import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_KEY; |
| 41 | import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_TOPIC; |
| 42 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 43 | /** |
| 44 | * Openstack telemetry manager. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class OpenstackTelemetryManager implements OpenstackTelemetryService { |
| 49 | |
| 50 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 51 | |
| 52 | private List<TelemetryService> telemetryServices = Lists.newArrayList(); |
| 53 | |
| 54 | @Activate |
| 55 | protected void activate() { |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | protected void deactivate() { |
| 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void addTelemetryService(TelemetryService telemetryService) { |
| 66 | telemetryServices.add(telemetryService); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void removeTelemetryService(TelemetryService telemetryService) { |
| 71 | telemetryServices.remove(telemetryService); |
| 72 | } |
| 73 | |
| 74 | @Override |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 75 | public void publish(Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 76 | telemetryServices.forEach(service -> { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 77 | if (service instanceof GrpcTelemetryManager) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 78 | invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (service instanceof InfluxDbTelemetryManager) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 82 | invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (service instanceof KafkaTelemetryManager) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 86 | invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (service instanceof RestTelemetryManager) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 90 | invokeRestPublisher((RestTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 91 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 92 | |
| 93 | log.trace("Publishing Flow Infos {}", flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 94 | }); |
| 95 | } |
| 96 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 97 | private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 98 | // TODO: need provide implementation |
| 99 | } |
| 100 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 101 | private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) { |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame^] | 102 | DefaultInfluxRecord<String, Set<FlowInfo>> influxRecord |
| 103 | = new DefaultInfluxRecord<>(DEFAULT_INFLUXDB_MEASUREMENT, flowInfos); |
| 104 | service.publish(influxRecord); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 107 | private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) { |
| 108 | TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec(); |
| 109 | ByteBuffer buffer = codec.encode(flowInfos); |
| 110 | service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array())); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 111 | } |
| 112 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 113 | private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 114 | // TODO: need provide implementation |
| 115 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 116 | } |