blob: f5f95509d30583f879ef132a8c7909891c37c432 [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"
Girish Kumardef46fc2020-08-05 18:20:11 +000020 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
21 grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040023 "google.golang.org/grpc"
Scott Bakeree6a0872019-10-29 15:59:52 -070024 "google.golang.org/grpc/codes"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040025 "google.golang.org/grpc/credentials"
Scott Bakeree6a0872019-10-29 15:59:52 -070026 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040027 "net"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040028)
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)
khenaidoobf6e7bb2018-08-14 22:27:29 -040056*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040057
Scott Bakeree6a0872019-10-29 15:59:52 -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
Stephane Barbarie7dfae952018-08-13 11:43:17 -040064type GrpcServer struct {
65 gs *grpc.Server
66 address string
Stephane Barbarie7dfae952018-08-13 11:43:17 -040067 secure bool
68 services []func(*grpc.Server)
Scott Bakeree6a0872019-10-29 15:59:52 -070069 probe ReadyProbe // optional
Stephane Barbarie7dfae952018-08-13 11:43:17 -040070
71 *GrpcSecurity
72}
73
74/*
75Instantiate a GRPC server data structure
76*/
77func NewGrpcServer(
78 address string,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040079 certs *GrpcSecurity,
80 secure bool,
Scott Bakeree6a0872019-10-29 15:59:52 -070081 probe ReadyProbe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040082) *GrpcServer {
83 server := &GrpcServer{
84 address: address,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040085 secure: secure,
86 GrpcSecurity: certs,
Scott Bakeree6a0872019-10-29 15:59:52 -070087 probe: probe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040088 }
89 return server
90}
91
92/*
93Start prepares the GRPC server and starts servicing requests
94*/
95func (s *GrpcServer) Start(ctx context.Context) {
khenaidoo5aadea02018-11-07 14:30:11 -050096
Neha Sharmad1387da2020-05-07 20:07:28 +000097 lis, err := net.Listen("tcp", s.address)
Stephane Barbarie7dfae952018-08-13 11:43:17 -040098 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000099 logger.Fatalf(ctx, "failed to listen: %v", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400100 }
101
Girish Kumardef46fc2020-08-05 18:20:11 +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 Kumarf8d4f8d2020-08-18 11:45:30 +0000105 grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumardef46fc2020-08-05 18:20:11 +0000106 )),
107 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000108 grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumardef46fc2020-08-05 18:20:11 +0000109 mkServerInterceptor(s),
110 ))}
111
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400112 if s.secure && s.GrpcSecurity != nil {
113 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
114 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000115 logger.Fatalf(ctx, "could not load TLS keys: %s", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400116 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400117
Girish Kumardef46fc2020-08-05 18:20:11 +0000118 serverOptions = append(serverOptions, grpc.Creds(creds))
119 s.gs = grpc.NewServer(serverOptions...)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400120 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000121 logger.Info(ctx, "starting-insecure-grpc-server")
Girish Kumardef46fc2020-08-05 18:20:11 +0000122 s.gs = grpc.NewServer(serverOptions...)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400123 }
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 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131 logger.Fatalf(ctx, "failed to serve: %v\n", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400132 }
133}
134
Scott Bakeree6a0872019-10-29 15:59:52 -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()) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000150 logger.Warnf(ctx, "Grpc request received while not ready %v", req)
Scott Bakeree6a0872019-10-29 15:59:52 -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
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400161/*
162Stop servicing GRPC requests
163*/
164func (s *GrpcServer) Stop() {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700165 if s.gs != nil {
166 s.gs.Stop()
167 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400168}
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}