blob: 8812cca8c940e56bb25d429c516af2d4076a46cf [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 Li6803ccd2018-06-08 09:26:09 +090019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Jian Lid1ce10a2018-06-12 13:47:23 +090022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li6803ccd2018-06-08 09:26:09 +090024import org.apache.felix.scr.annotations.Service;
25import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService;
Jian Lid1ce10a2018-06-12 13:47:23 +090026import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
Jian Li6803ccd2018-06-08 09:26:09 +090027import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * gRPC telemetry manager.
33 */
34@Component(immediate = true)
35@Service
36public class GrpcTelemetryManager implements GrpcTelemetryAdminService {
37
38 private final Logger log = LoggerFactory.getLogger(getClass());
39
Jian Lid1ce10a2018-06-12 13:47:23 +090040 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 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 Lid1ce10a2018-06-12 13:47:23 +090069 // FIXME do not activate grpc service for now due to deps conflict
70// GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config;
71// channel = ManagedChannelBuilder
72// .forAddress(grpcConfig.address(), grpcConfig.port())
73// .maxInboundMessageSize(grpcConfig.maxInboundMsgSize())
74// .usePlaintext(grpcConfig.usePlaintext())
75// .build();
Jian Li6803ccd2018-06-08 09:26:09 +090076
77 log.info("gRPC producer has Started");
78 }
79
80 @Override
81 public void stop() {
Jian Lid1ce10a2018-06-12 13:47:23 +090082 // FIXME do not activate grpc service for now due to deps conflict
83// 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}