blob: f5f95509d30583f879ef132a8c7909891c37c432 [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"
Girish Kumar74240652020-07-10 11:54:28 +000020 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
21 grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
Girish Kumare9d35bb2020-08-18 06:47:59 +000022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070023 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070024 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "google.golang.org/grpc/credentials"
Scott Baker104b67d2019-10-29 15:56:27 -070026 "google.golang.org/grpc/status"
Scott Baker2c1c4822019-10-16 11:02:41 -070027 "net"
28)
29
30/*
31To add a GRPC server to your existing component simply follow these steps:
32
331. Create a server instance by passing the host and port where it should run and optionally add certificate information
34
35 e.g.
36 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
37
382. Create a function that will register your service with the GRPC server
39
40 e.g.
41 f := func(gs *grpc.Server) {
42 voltha.RegisterVolthaReadOnlyServiceServer(
43 gs,
44 core.NewReadOnlyServiceHandler(s.root),
45 )
46 }
47
483. Add the service to the server
49
50 e.g.
51 s.server.AddService(f)
52
534. Start the server
54
55 s.server.Start(ctx)
56*/
57
Scott Baker104b67d2019-10-29 15:56:27 -070058// Interface allows probes to be attached to server
59// A probe must support the IsReady() method
60type ReadyProbe interface {
61 IsReady() bool
62}
63
Scott Baker2c1c4822019-10-16 11:02:41 -070064type GrpcServer struct {
65 gs *grpc.Server
66 address string
Scott Baker2c1c4822019-10-16 11:02:41 -070067 secure bool
68 services []func(*grpc.Server)
Scott Baker104b67d2019-10-29 15:56:27 -070069 probe ReadyProbe // optional
Scott Baker2c1c4822019-10-16 11:02:41 -070070
71 *GrpcSecurity
72}
73
74/*
75Instantiate a GRPC server data structure
76*/
77func NewGrpcServer(
78 address string,
Scott Baker2c1c4822019-10-16 11:02:41 -070079 certs *GrpcSecurity,
80 secure bool,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080081 probe ReadyProbe,
Scott Baker2c1c4822019-10-16 11:02:41 -070082) *GrpcServer {
83 server := &GrpcServer{
84 address: address,
Scott Baker2c1c4822019-10-16 11:02:41 -070085 secure: secure,
86 GrpcSecurity: certs,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080087 probe: probe,
Scott Baker2c1c4822019-10-16 11:02:41 -070088 }
89 return server
90}
91
92/*
93Start prepares the GRPC server and starts servicing requests
94*/
95func (s *GrpcServer) Start(ctx context.Context) {
96
Neha Sharmadd9af392020-04-28 09:03:57 +000097 lis, err := net.Listen("tcp", s.address)
Scott Baker2c1c4822019-10-16 11:02:41 -070098 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000099 logger.Fatalf(ctx, "failed to listen: %v", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700100 }
101
Girish Kumar74240652020-07-10 11:54:28 +0000102 // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC server
103 serverOptions := []grpc.ServerOption{
104 grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
Girish Kumare9d35bb2020-08-18 06:47:59 +0000105 grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar74240652020-07-10 11:54:28 +0000106 )),
107 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
Girish Kumare9d35bb2020-08-18 06:47:59 +0000108 grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar74240652020-07-10 11:54:28 +0000109 mkServerInterceptor(s),
110 ))}
111
Scott Baker2c1c4822019-10-16 11:02:41 -0700112 if s.secure && s.GrpcSecurity != nil {
113 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
114 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000115 logger.Fatalf(ctx, "could not load TLS keys: %s", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700116 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700117
Girish Kumar74240652020-07-10 11:54:28 +0000118 serverOptions = append(serverOptions, grpc.Creds(creds))
119 s.gs = grpc.NewServer(serverOptions...)
Scott Baker2c1c4822019-10-16 11:02:41 -0700120 } else {
Neha Sharma94f16a92020-06-26 04:17:55 +0000121 logger.Info(ctx, "starting-insecure-grpc-server")
Girish Kumar74240652020-07-10 11:54:28 +0000122 s.gs = grpc.NewServer(serverOptions...)
Scott Baker2c1c4822019-10-16 11:02:41 -0700123 }
124
125 // Register all required services
126 for _, service := range s.services {
127 service(s.gs)
128 }
129
130 if err := s.gs.Serve(lis); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000131 logger.Fatalf(ctx, "failed to serve: %v\n", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700132 }
133}
134
Scott Baker104b67d2019-10-29 15:56:27 -0700135// Make a serverInterceptor for the given GrpcServer
136// This interceptor will check whether there is an attached probe,
137// and if that probe indicates NotReady, then an UNAVAILABLE
138// response will be returned.
139func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
140 req interface{},
141 info *grpc.UnaryServerInfo,
142 handler grpc.UnaryHandler) (interface{}, error) {
143
144 return func(ctx context.Context,
145 req interface{},
146 info *grpc.UnaryServerInfo,
147 handler grpc.UnaryHandler) (interface{}, error) {
148
149 if (s.probe != nil) && (!s.probe.IsReady()) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000150 logger.Warnf(ctx, "Grpc request received while not ready %v", req)
Scott Baker104b67d2019-10-29 15:56:27 -0700151 return nil, status.Error(codes.Unavailable, "system is not ready")
152 }
153
154 // Calls the handler
155 h, err := handler(ctx, req)
156
157 return h, err
158 }
159}
160
Scott Baker2c1c4822019-10-16 11:02:41 -0700161/*
162Stop servicing GRPC requests
163*/
164func (s *GrpcServer) Stop() {
165 if s.gs != nil {
166 s.gs.Stop()
167 }
168}
169
170/*
171AddService appends a generic service request function
172*/
173func (s *GrpcServer) AddService(
174 registerFunction func(*grpc.Server),
175) {
176 s.services = append(s.services, registerFunction)
177}