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