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