Jian Li | 6803ccd | 2018-06-08 09:26:09 +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 | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 19 | import io.grpc.ManagedChannel; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 20 | import io.grpc.ManagedChannelBuilder; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacktelemetry.api.TelemetryConfigService; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 33 | import java.util.Set; |
| 34 | |
| 35 | import static org.onosproject.openstacktelemetry.api.Constants.GRPC_SCHEME; |
| 36 | import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.GRPC; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.fromTelemetryConfig; |
| 38 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 39 | /** |
| 40 | * gRPC telemetry manager. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Component(immediate = true, service = GrpcTelemetryAdminService.class) |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 43 | public class GrpcTelemetryManager implements GrpcTelemetryAdminService { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 48 | protected OpenstackTelemetryService openstackTelemetryService; |
| 49 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 51 | protected TelemetryConfigService telemetryConfigService; |
| 52 | |
| 53 | private Set<ManagedChannel> channels = Sets.newConcurrentHashSet(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 54 | |
| 55 | @Activate |
| 56 | protected void activate() { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 57 | |
| 58 | openstackTelemetryService.addTelemetryService(this); |
| 59 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | protected void deactivate() { |
| 65 | stop(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 66 | |
| 67 | openstackTelemetryService.removeTelemetryService(this); |
| 68 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 69 | log.info("Stopped"); |
| 70 | } |
| 71 | |
| 72 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 73 | public void start() { |
| 74 | telemetryConfigService.getConfigsByType(GRPC).forEach(c -> { |
| 75 | GrpcTelemetryConfig grpcConfig = fromTelemetryConfig(c); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 76 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 77 | if (grpcConfig != null && !c.name().equals(GRPC_SCHEME) && c.enabled()) { |
| 78 | ManagedChannel channel = ManagedChannelBuilder |
| 79 | .forAddress(grpcConfig.address(), grpcConfig.port()) |
| 80 | .maxInboundMessageSize(grpcConfig.maxInboundMsgSize()) |
| 81 | .usePlaintext(grpcConfig.usePlaintext()) |
| 82 | .build(); |
| 83 | |
| 84 | channels.add(channel); |
| 85 | } |
| 86 | }); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 87 | |
| 88 | log.info("gRPC producer has Started"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void stop() { |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 93 | channels.forEach(ManagedChannel::shutdown); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 94 | log.info("gRPC producer has Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 98 | public void restart() { |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 99 | stop(); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 100 | start(); |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Object publish(Object record) { |
| 105 | // TODO: need to find a way to invoke gRPC endpoint using channel |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 106 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 107 | if (channels.isEmpty()) { |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 108 | log.debug("gRPC telemetry service has not been enabled!"); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 111 | return null; |
| 112 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 113 | |
| 114 | @Override |
| 115 | public boolean isRunning() { |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 116 | return !channels.isEmpty(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 117 | } |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 118 | } |