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 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 20 | import org.apache.kafka.clients.producer.ProducerRecord; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 21 | import org.onosproject.cfg.ComponentConfigService; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 23 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryService; |
| 24 | import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService; |
| 25 | import org.onosproject.openstacktelemetry.api.KafkaTelemetryService; |
| 26 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacktelemetry.api.PrometheusTelemetryService; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 28 | import org.onosproject.openstacktelemetry.api.RestTelemetryService; |
| 29 | import org.onosproject.openstacktelemetry.api.TelemetryService; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 30 | import org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.nio.ByteBuffer; |
| 40 | import java.util.List; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 41 | import java.util.Set; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 42 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 43 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_MEASUREMENT; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 44 | import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_KEY; |
| 45 | import static org.onosproject.openstacktelemetry.codec.TinaMessageByteBufferCodec.KAFKA_TOPIC; |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 46 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getPropertyValueAsBoolean; |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 47 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 48 | /** |
| 49 | * Openstack telemetry manager. |
| 50 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | @Component(immediate = true, service = OpenstackTelemetryService.class) |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 52 | public class OpenstackTelemetryManager implements OpenstackTelemetryService { |
| 53 | |
| 54 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 55 | |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 56 | private static final String ENABLE_SERVICE = "enableService"; |
| 57 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 59 | protected ComponentConfigService componentConfigService; |
| 60 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 61 | private List<TelemetryService> telemetryServices = Lists.newArrayList(); |
| 62 | |
| 63 | @Activate |
| 64 | protected void activate() { |
| 65 | log.info("Started"); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | protected void deactivate() { |
| 70 | log.info("Stopped"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void addTelemetryService(TelemetryService telemetryService) { |
| 75 | telemetryServices.add(telemetryService); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void removeTelemetryService(TelemetryService telemetryService) { |
| 80 | telemetryServices.remove(telemetryService); |
| 81 | } |
| 82 | |
| 83 | @Override |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 84 | public void publish(Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 85 | telemetryServices.forEach(service -> { |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 86 | if (service instanceof GrpcTelemetryManager && |
| 87 | getPropertyValueAsBoolean(componentConfigService.getProperties( |
| 88 | GrpcTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 89 | invokeGrpcPublisher((GrpcTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 90 | } |
| 91 | |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 92 | if (service instanceof InfluxDbTelemetryManager && |
| 93 | getPropertyValueAsBoolean(componentConfigService.getProperties( |
| 94 | InfluxDbTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 95 | invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 96 | } |
| 97 | |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 98 | if (service instanceof PrometheusTelemetryManager && |
| 99 | getPropertyValueAsBoolean(componentConfigService.getProperties( |
| 100 | PrometheusTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) { |
| 101 | invokePrometheusPublisher((PrometheusTelemetryService) service, flowInfos); |
| 102 | } |
| 103 | |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 104 | if (service instanceof KafkaTelemetryManager && |
| 105 | getPropertyValueAsBoolean(componentConfigService.getProperties( |
| 106 | KafkaTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 107 | invokeKafkaPublisher((KafkaTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 110 | if (service instanceof RestTelemetryManager && |
| 111 | getPropertyValueAsBoolean(componentConfigService.getProperties( |
| 112 | RestTelemetryConfigManager.class.getName()), ENABLE_SERVICE)) { |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 113 | invokeRestPublisher((RestTelemetryService) service, flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 114 | } |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 115 | |
| 116 | log.trace("Publishing Flow Infos {}", flowInfos); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 117 | }); |
| 118 | } |
| 119 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 120 | @Override |
| 121 | public Set<TelemetryService> telemetryServices() { |
| 122 | return ImmutableSet.copyOf(telemetryServices); |
| 123 | } |
| 124 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 125 | private void invokeGrpcPublisher(GrpcTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 126 | // TODO: need provide implementation |
| 127 | } |
| 128 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 129 | private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, Set<FlowInfo> flowInfos) { |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 130 | DefaultInfluxRecord<String, Set<FlowInfo>> influxRecord |
| 131 | = new DefaultInfluxRecord<>(DEFAULT_INFLUXDB_MEASUREMENT, flowInfos); |
| 132 | service.publish(influxRecord); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 133 | } |
| 134 | |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 135 | private void invokePrometheusPublisher(PrometheusTelemetryService service, Set<FlowInfo> flowInfos) { |
| 136 | service.publish(flowInfos); |
| 137 | } |
| 138 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 139 | private void invokeKafkaPublisher(KafkaTelemetryService service, Set<FlowInfo> flowInfos) { |
| 140 | TinaMessageByteBufferCodec codec = new TinaMessageByteBufferCodec(); |
| 141 | ByteBuffer buffer = codec.encode(flowInfos); |
| 142 | service.publish(new ProducerRecord<>(KAFKA_TOPIC, KAFKA_KEY, buffer.array())); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 145 | private void invokeRestPublisher(RestTelemetryService service, Set<FlowInfo> flowInfos) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 146 | // TODO: need provide implementation |
| 147 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 148 | } |