blob: bee418dee3013c1a7c4fb001d6c9af7248d47ec1 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 grpc
17
18import (
19 "context"
David K. Bainbridgebc381072021-03-19 20:56:04 +000020 "net"
21
Girish Kumar74240652020-07-10 11:54:28 +000022 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
23 grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
khenaidoo26721882021-08-11 17:42:52 -040024 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070026 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070027 "google.golang.org/grpc/credentials"
David K. Bainbridgebc381072021-03-19 20:56:04 +000028 "google.golang.org/grpc/reflection"
Scott Baker104b67d2019-10-29 15:56:27 -070029 "google.golang.org/grpc/status"
Scott Baker2c1c4822019-10-16 11:02:41 -070030)
31
32/*
33To add a GRPC server to your existing component simply follow these steps:
34
351. Create a server instance by passing the host and port where it should run and optionally add certificate information
36
37 e.g.
38 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
39
402. Create a function that will register your service with the GRPC server
41
42 e.g.
43 f := func(gs *grpc.Server) {
44 voltha.RegisterVolthaReadOnlyServiceServer(
45 gs,
46 core.NewReadOnlyServiceHandler(s.root),
47 )
48 }
49
503. Add the service to the server
51
52 e.g.
53 s.server.AddService(f)
54
554. Start the server
56
57 s.server.Start(ctx)
58*/
59
Scott Baker104b67d2019-10-29 15:56:27 -070060// Interface allows probes to be attached to server
61// A probe must support the IsReady() method
62type ReadyProbe interface {
63 IsReady() bool
64}
65
Scott Baker2c1c4822019-10-16 11:02:41 -070066type GrpcServer struct {
67 gs *grpc.Server
68 address string
Scott Baker2c1c4822019-10-16 11:02:41 -070069 secure bool
70 services []func(*grpc.Server)
Scott Baker104b67d2019-10-29 15:56:27 -070071 probe ReadyProbe // optional
Scott Baker2c1c4822019-10-16 11:02:41 -070072
73 *GrpcSecurity
74}
75
76/*
77Instantiate a GRPC server data structure
78*/
79func NewGrpcServer(
80 address string,
Scott Baker2c1c4822019-10-16 11:02:41 -070081 certs *GrpcSecurity,
82 secure bool,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080083 probe ReadyProbe,
Scott Baker2c1c4822019-10-16 11:02:41 -070084) *GrpcServer {
85 server := &GrpcServer{
86 address: address,
Scott Baker2c1c4822019-10-16 11:02:41 -070087 secure: secure,
88 GrpcSecurity: certs,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080089 probe: probe,
Scott Baker2c1c4822019-10-16 11:02:41 -070090 }
91 return server
92}
93
94/*
95Start prepares the GRPC server and starts servicing requests
96*/
97func (s *GrpcServer) Start(ctx context.Context) {
98
Neha Sharmadd9af392020-04-28 09:03:57 +000099 lis, err := net.Listen("tcp", s.address)
Scott Baker2c1c4822019-10-16 11:02:41 -0700100 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000101 logger.Fatalf(ctx, "failed to listen: %v", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700102 }
103
Girish Kumar74240652020-07-10 11:54:28 +0000104 // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC server
105 serverOptions := []grpc.ServerOption{
106 grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
Girish Kumare9d35bb2020-08-18 06:47:59 +0000107 grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar74240652020-07-10 11:54:28 +0000108 )),
109 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
Girish Kumare9d35bb2020-08-18 06:47:59 +0000110 grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar74240652020-07-10 11:54:28 +0000111 mkServerInterceptor(s),
112 ))}
113
Scott Baker2c1c4822019-10-16 11:02:41 -0700114 if s.secure && s.GrpcSecurity != nil {
115 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
116 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000117 logger.Fatalf(ctx, "could not load TLS keys: %s", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700118 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700119
Girish Kumar74240652020-07-10 11:54:28 +0000120 serverOptions = append(serverOptions, grpc.Creds(creds))
121 s.gs = grpc.NewServer(serverOptions...)
Scott Baker2c1c4822019-10-16 11:02:41 -0700122 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000123 logger.Info(ctx, "starting-insecure-grpc-server")
Girish Kumar74240652020-07-10 11:54:28 +0000124 s.gs = grpc.NewServer(serverOptions...)
Scott Baker2c1c4822019-10-16 11:02:41 -0700125 }
126
127 // Register all required services
128 for _, service := range s.services {
129 service(s.gs)
130 }
David K. Bainbridgebc381072021-03-19 20:56:04 +0000131 reflection.Register(s.gs)
Scott Baker2c1c4822019-10-16 11:02:41 -0700132
133 if err := s.gs.Serve(lis); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000134 logger.Fatalf(ctx, "failed to serve: %v\n", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700135 }
136}
137
Scott Baker104b67d2019-10-29 15:56:27 -0700138// Make a serverInterceptor for the given GrpcServer
139// This interceptor will check whether there is an attached probe,
140// and if that probe indicates NotReady, then an UNAVAILABLE
141// response will be returned.
142func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
143 req interface{},
144 info *grpc.UnaryServerInfo,
145 handler grpc.UnaryHandler) (interface{}, error) {
146
147 return func(ctx context.Context,
148 req interface{},
149 info *grpc.UnaryServerInfo,
150 handler grpc.UnaryHandler) (interface{}, error) {
151
152 if (s.probe != nil) && (!s.probe.IsReady()) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000153 logger.Warnf(ctx, "Grpc request received while not ready %v", req)
Scott Baker104b67d2019-10-29 15:56:27 -0700154 return nil, status.Error(codes.Unavailable, "system is not ready")
155 }
156
157 // Calls the handler
158 h, err := handler(ctx, req)
159
160 return h, err
161 }
162}
163
Scott Baker2c1c4822019-10-16 11:02:41 -0700164/*
165Stop servicing GRPC requests
166*/
167func (s *GrpcServer) Stop() {
168 if s.gs != nil {
169 s.gs.Stop()
170 }
171}
172
173/*
174AddService appends a generic service request function
175*/
176func (s *GrpcServer) AddService(
177 registerFunction func(*grpc.Server),
178) {
179 s.services = append(s.services, registerFunction)
180}