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; |
| 24 | import org.onosproject.openstacktelemetry.api.ByteBufferCodec; |
| 25 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 26 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryService; |
| 27 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService; |
| 28 | import org.onosproject.openstacktelemetry.api.KafkaTelemetryService; |
| 29 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
| 30 | import org.onosproject.openstacktelemetry.api.RestTelemetryService; |
| 31 | import org.onosproject.openstacktelemetry.api.TelemetryService; |
| 32 | import org.onosproject.openstacktelemetry.codec.TinaFlowInfoByteBufferCodec; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.nio.ByteBuffer; |
| 37 | import java.util.List; |
| 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 | |
| 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 | } |