blob: 33c9b762201947fa0034e092b3d94486cb73dac0 [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"
khenaidoo5aadea02018-11-07 14:30:11 -050020 "fmt"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080021 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040022 "google.golang.org/grpc"
Scott Bakeree6a0872019-10-29 15:59:52 -070023 "google.golang.org/grpc/codes"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040024 "google.golang.org/grpc/credentials"
Scott Bakeree6a0872019-10-29 15:59:52 -070025 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040026 "net"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040027)
28
29/*
30To add a GRPC server to your existing component simply follow these steps:
31
321. Create a server instance by passing the host and port where it should run and optionally add certificate information
33
34 e.g.
35 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
36
372. Create a function that will register your service with the GRPC server
38
39 e.g.
40 f := func(gs *grpc.Server) {
41 voltha.RegisterVolthaReadOnlyServiceServer(
42 gs,
43 core.NewReadOnlyServiceHandler(s.root),
44 )
45 }
46
473. Add the service to the server
48
49 e.g.
50 s.server.AddService(f)
51
524. Start the server
53
54 s.server.Start(ctx)
khenaidoobf6e7bb2018-08-14 22:27:29 -040055*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040056
Scott Bakeree6a0872019-10-29 15:59:52 -070057// Interface allows probes to be attached to server
58// A probe must support the IsReady() method
59type ReadyProbe interface {
60 IsReady() bool
61}
62
Stephane Barbarie7dfae952018-08-13 11:43:17 -040063type GrpcServer struct {
64 gs *grpc.Server
65 address string
66 port int
67 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,
79 port int,
80 certs *GrpcSecurity,
81 secure bool,
Scott Bakeree6a0872019-10-29 15:59:52 -070082 probe ReadyProbe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040083) *GrpcServer {
84 server := &GrpcServer{
85 address: address,
86 port: port,
87 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
99 host := fmt.Sprintf("%s:%d", s.address, s.port)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400100
101 lis, err := net.Listen("tcp", host)
102 if err != nil {
103 log.Fatalf("failed to listen: %v", err)
104 }
105
106 if s.secure && s.GrpcSecurity != nil {
107 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
108 if err != nil {
109 log.Fatalf("could not load TLS keys: %s", err)
110 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700111 s.gs = grpc.NewServer(grpc.Creds(creds),
112 withServerUnaryInterceptor(s))
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400113
114 } else {
115 log.Info("starting-insecure-grpc-server")
Scott Bakeree6a0872019-10-29 15:59:52 -0700116 s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400117 }
118
119 // Register all required services
120 for _, service := range s.services {
121 service(s.gs)
122 }
123
124 if err := s.gs.Serve(lis); err != nil {
125 log.Fatalf("failed to serve: %v\n", err)
126 }
127}
128
Scott Bakeree6a0872019-10-29 15:59:52 -0700129func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption {
130 return grpc.UnaryInterceptor(mkServerInterceptor(s))
131}
132
133// Make a serverInterceptor for the given GrpcServer
134// This interceptor will check whether there is an attached probe,
135// and if that probe indicates NotReady, then an UNAVAILABLE
136// response will be returned.
137func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
138 req interface{},
139 info *grpc.UnaryServerInfo,
140 handler grpc.UnaryHandler) (interface{}, error) {
141
142 return func(ctx context.Context,
143 req interface{},
144 info *grpc.UnaryServerInfo,
145 handler grpc.UnaryHandler) (interface{}, error) {
146
147 if (s.probe != nil) && (!s.probe.IsReady()) {
148 log.Warnf("Grpc request received while not ready %v", req)
149 return nil, status.Error(codes.Unavailable, "system is not ready")
150 }
151
152 // Calls the handler
153 h, err := handler(ctx, req)
154
155 return h, err
156 }
157}
158
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400159/*
160Stop servicing GRPC requests
161*/
162func (s *GrpcServer) Stop() {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700163 if s.gs != nil {
164 s.gs.Stop()
165 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400166}
167
168/*
169AddService appends a generic service request function
170*/
171func (s *GrpcServer) AddService(
172 registerFunction func(*grpc.Server),
173) {
174 s.services = append(s.services, registerFunction)
175}