blob: 38dc3080813e64916bd9db831fb89e862d896ec3 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
Stephane Barbarie7dfae952018-08-13 11:43:17 -040016package grpc
17
18import (
Stephane Barbarie7dfae952018-08-13 11:43:17 -040019 "context"
Himani Chawla27f81212021-04-07 11:37:47 +053020 "net"
21
Girish Kumardef46fc2020-08-05 18:20:11 +000022 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
23 grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
yasin sapli5458a1c2021-06-14 22:24:38 +000024 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040025 "google.golang.org/grpc"
Scott Bakeree6a0872019-10-29 15:59:52 -070026 "google.golang.org/grpc/codes"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040027 "google.golang.org/grpc/credentials"
Himani Chawla27f81212021-04-07 11:37:47 +053028 "google.golang.org/grpc/reflection"
Scott Bakeree6a0872019-10-29 15:59:52 -070029 "google.golang.org/grpc/status"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040030)
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)
khenaidoobf6e7bb2018-08-14 22:27:29 -040058*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040059
Scott Bakeree6a0872019-10-29 15:59:52 -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
Stephane Barbarie7dfae952018-08-13 11:43:17 -040066type GrpcServer struct {
67 gs *grpc.Server
68 address string
Stephane Barbarie7dfae952018-08-13 11:43:17 -040069 secure bool
70 services []func(*grpc.Server)
Scott Bakeree6a0872019-10-29 15:59:52 -070071 probe ReadyProbe // optional
Stephane Barbarie7dfae952018-08-13 11:43:17 -040072
73 *GrpcSecurity
74}
75
76/*
77Instantiate a GRPC server data structure
78*/
79func NewGrpcServer(
80 address string,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040081 certs *GrpcSecurity,
82 secure bool,
Scott Bakeree6a0872019-10-29 15:59:52 -070083 probe ReadyProbe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040084) *GrpcServer {
85 server := &GrpcServer{
86 address: address,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040087 secure: secure,
88 GrpcSecurity: certs,
Scott Bakeree6a0872019-10-29 15:59:52 -070089 probe: probe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040090 }
91 return server
92}
93
94/*
95Start prepares the GRPC server and starts servicing requests
96*/
97func (s *GrpcServer) Start(ctx context.Context) {
khenaidoo5aadea02018-11-07 14:30:11 -050098
Neha Sharmad1387da2020-05-07 20:07:28 +000099 lis, err := net.Listen("tcp", s.address)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400100 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000101 logger.Fatalf(ctx, "failed to listen: %v", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400102 }
103
Girish Kumardef46fc2020-08-05 18:20:11 +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 Kumarf8d4f8d2020-08-18 11:45:30 +0000107 grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumardef46fc2020-08-05 18:20:11 +0000108 )),
109 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000110 grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumardef46fc2020-08-05 18:20:11 +0000111 mkServerInterceptor(s),
112 ))}
113
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400114 if s.secure && s.GrpcSecurity != nil {
115 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
116 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000117 logger.Fatalf(ctx, "could not load TLS keys: %s", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400118 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400119
Girish Kumardef46fc2020-08-05 18:20:11 +0000120 serverOptions = append(serverOptions, grpc.Creds(creds))
121 s.gs = grpc.NewServer(serverOptions...)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400122 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000123 logger.Info(ctx, "starting-insecure-grpc-server")
Girish Kumardef46fc2020-08-05 18:20:11 +0000124 s.gs = grpc.NewServer(serverOptions...)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400125 }
126
127 // Register all required services
128 for _, service := range s.services {
129 service(s.gs)
130 }
Himani Chawla27f81212021-04-07 11:37:47 +0530131 reflection.Register(s.gs)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400132
133 if err := s.gs.Serve(lis); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000134 logger.Fatalf(ctx, "failed to serve: %v\n", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400135 }
136}
137
Scott Bakeree6a0872019-10-29 15:59:52 -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()) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000153 logger.Warnf(ctx, "Grpc request received while not ready %v", req)
Scott Bakeree6a0872019-10-29 15:59:52 -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
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400164/*
165Stop servicing GRPC requests
166*/
167func (s *GrpcServer) Stop() {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700168 if s.gs != nil {
169 s.gs.Stop()
170 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400171}
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}