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; |
| 19 | import io.grpc.ManagedChannelBuilder; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService; |
| 25 | import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig; |
| 26 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * gRPC telemetry manager. |
| 32 | */ |
| 33 | @Component(immediate = true) |
| 34 | @Service |
| 35 | public class GrpcTelemetryManager implements GrpcTelemetryAdminService { |
| 36 | |
| 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
| 39 | private ManagedChannel channel = null; |
| 40 | |
| 41 | @Activate |
| 42 | protected void activate() { |
| 43 | log.info("Started"); |
| 44 | } |
| 45 | |
| 46 | @Deactivate |
| 47 | protected void deactivate() { |
| 48 | stop(); |
| 49 | log.info("Stopped"); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void start(TelemetryConfig config) { |
| 54 | if (channel != null) { |
| 55 | log.info("gRPC producer has already been started"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config; |
| 60 | channel = ManagedChannelBuilder |
| 61 | .forAddress(grpcConfig.address(), grpcConfig.port()) |
| 62 | .maxInboundMessageSize(grpcConfig.maxInboundMsgSize()) |
| 63 | .usePlaintext(grpcConfig.usePlaintext()) |
| 64 | .build(); |
| 65 | |
| 66 | log.info("gRPC producer has Started"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void stop() { |
| 71 | if (channel != null) { |
| 72 | channel.shutdown(); |
| 73 | channel = null; |
| 74 | } |
| 75 | |
| 76 | log.info("gRPC producer has Stopped"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void restart(TelemetryConfig config) { |
| 81 | stop(); |
| 82 | start(config); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Object publish(Object record) { |
| 87 | // TODO: need to find a way to invoke gRPC endpoint using channel |
| 88 | return null; |
| 89 | } |
| 90 | } |