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