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 | |
| 39 | /** |
| 40 | * Openstack telemetry manager. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public class OpenstackTelemetryManager implements OpenstackTelemetryService { |
| 45 | |
| 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 48 | private static final String KAFKA_TOPIC = "sona.flow"; |
| 49 | private static final String KAFKA_KEY = "flowdata"; |
| 50 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 51 | 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 Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 74 | public void publish(Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 75 | telemetryServices.forEach(service -> { |
| 76 | |
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 | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 92 | }); |
| 93 | } |
| 94 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 95 | private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 96 | // TODO: need provide implementation |
| 97 | } |
| 98 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 99 | private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 100 | // TODO: need provide implementation |
| 101 | } |
| 102 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 103 | 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 109 | private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 110 | // TODO: need provide implementation |
| 111 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 112 | } |