blob: 0ea76dcc2b0ecaa190547e6122bb4fb6d4c50a42 [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.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Jian Lid1ce10a2018-06-12 13:47:23 +090023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li6803ccd2018-06-08 09:26:09 +090025import org.apache.felix.scr.annotations.Service;
26import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService;
Jian Lid1ce10a2018-06-12 13:47:23 +090027import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
Jian Liae3fcff2018-07-30 11:55:44 +090028import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig;
Jian Li6803ccd2018-06-08 09:26:09 +090029import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33/**
34 * gRPC telemetry manager.
35 */
36@Component(immediate = true)
37@Service
38public class GrpcTelemetryManager implements GrpcTelemetryAdminService {
39
40 private final Logger log = LoggerFactory.getLogger(getClass());
41
Jian Lid1ce10a2018-06-12 13:47:23 +090042 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected OpenstackTelemetryService openstackTelemetryService;
44
Jian Li6803ccd2018-06-08 09:26:09 +090045 private ManagedChannel channel = null;
46
47 @Activate
48 protected void activate() {
Jian Lid1ce10a2018-06-12 13:47:23 +090049
50 openstackTelemetryService.addTelemetryService(this);
51
Jian Li6803ccd2018-06-08 09:26:09 +090052 log.info("Started");
53 }
54
55 @Deactivate
56 protected void deactivate() {
57 stop();
Jian Lid1ce10a2018-06-12 13:47:23 +090058
59 openstackTelemetryService.removeTelemetryService(this);
60
Jian Li6803ccd2018-06-08 09:26:09 +090061 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 Liae3fcff2018-07-30 11:55:44 +090071 GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config;
72 channel = ManagedChannelBuilder
73 .forAddress(grpcConfig.address(), grpcConfig.port())
74 .maxInboundMessageSize(grpcConfig.maxInboundMsgSize())
75 .usePlaintext(grpcConfig.usePlaintext())
76 .build();
Jian Li6803ccd2018-06-08 09:26:09 +090077
78 log.info("gRPC producer has Started");
79 }
80
81 @Override
82 public void stop() {
Jian Liae3fcff2018-07-30 11:55:44 +090083 if (channel != null) {
84 channel.shutdown();
85 channel = null;
86 }
Jian Li6803ccd2018-06-08 09:26:09 +090087
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 Lid1ce10a2018-06-12 13:47:23 +0900100
101 if (channel == null) {
102 log.warn("gRPC telemetry service has not been enabled!");
103 }
104
Jian Li6803ccd2018-06-08 09:26:09 +0900105 return null;
106 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900107
108 @Override
109 public boolean isRunning() {
110 return channel != null;
111 }
Jian Li6803ccd2018-06-08 09:26:09 +0900112}