blob: 391b1d827764195913401df071520a635709536a [file] [log] [blame]
Jian Li6803ccd2018-06-08 09:26:09 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import io.grpc.ManagedChannel;
Jian Liae3fcff2018-07-30 11:55:44 +090019import io.grpc.ManagedChannelBuilder;
Jian Li6803ccd2018-06-08 09:26:09 +090020import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService;
Jian Lid1ce10a2018-06-12 13:47:23 +090021import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
Jian Liae3fcff2018-07-30 11:55:44 +090022import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig;
Jian Li6803ccd2018-06-08 09:26:09 +090023import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.osgi.service.component.annotations.Activate;
25import org.osgi.service.component.annotations.Component;
26import org.osgi.service.component.annotations.Deactivate;
27import org.osgi.service.component.annotations.Reference;
28import org.osgi.service.component.annotations.ReferenceCardinality;
Jian Li6803ccd2018-06-08 09:26:09 +090029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * gRPC telemetry manager.
34 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Component(immediate = true, service = GrpcTelemetryAdminService.class)
Jian Li6803ccd2018-06-08 09:26:09 +090036public class GrpcTelemetryManager implements GrpcTelemetryAdminService {
37
38 private final Logger log = LoggerFactory.getLogger(getClass());
39
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Lid1ce10a2018-06-12 13:47:23 +090041 protected OpenstackTelemetryService openstackTelemetryService;
42
Jian Li6803ccd2018-06-08 09:26:09 +090043 private ManagedChannel channel = null;
44
45 @Activate
46 protected void activate() {
Jian Lid1ce10a2018-06-12 13:47:23 +090047
48 openstackTelemetryService.addTelemetryService(this);
49
Jian Li6803ccd2018-06-08 09:26:09 +090050 log.info("Started");
51 }
52
53 @Deactivate
54 protected void deactivate() {
55 stop();
Jian Lid1ce10a2018-06-12 13:47:23 +090056
57 openstackTelemetryService.removeTelemetryService(this);
58
Jian Li6803ccd2018-06-08 09:26:09 +090059 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 Liae3fcff2018-07-30 11:55:44 +090069 GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config;
70 channel = ManagedChannelBuilder
71 .forAddress(grpcConfig.address(), grpcConfig.port())
72 .maxInboundMessageSize(grpcConfig.maxInboundMsgSize())
73 .usePlaintext(grpcConfig.usePlaintext())
74 .build();
Jian Li6803ccd2018-06-08 09:26:09 +090075
76 log.info("gRPC producer has Started");
77 }
78
79 @Override
80 public void stop() {
Jian Liae3fcff2018-07-30 11:55:44 +090081 if (channel != null) {
82 channel.shutdown();
83 channel = null;
84 }
Jian Li6803ccd2018-06-08 09:26:09 +090085
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 Lid1ce10a2018-06-12 13:47:23 +090098
99 if (channel == null) {
Jian Li6c92b3c2018-08-03 11:26:55 +0900100 log.debug("gRPC telemetry service has not been enabled!");
Jian Lid1ce10a2018-06-12 13:47:23 +0900101 }
102
Jian Li6803ccd2018-06-08 09:26:09 +0900103 return null;
104 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900105
106 @Override
107 public boolean isRunning() {
108 return channel != null;
109 }
Jian Li6803ccd2018-06-08 09:26:09 +0900110}