blob: 43f2912e028771f9ae0329fc38529ea8f6182ef7 [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"
20 "fmt"
Scott Baker2c1c4822019-10-16 11:02:41 -070021 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070022 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070023 "google.golang.org/grpc/credentials"
Scott Baker104b67d2019-10-29 15:56:27 -070024 "google.golang.org/grpc/status"
Scott Baker2c1c4822019-10-16 11:02:41 -070025 "net"
26)
27
28/*
29To add a GRPC server to your existing component simply follow these steps:
30
311. Create a server instance by passing the host and port where it should run and optionally add certificate information
32
33 e.g.
34 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
35
362. Create a function that will register your service with the GRPC server
37
38 e.g.
39 f := func(gs *grpc.Server) {
40 voltha.RegisterVolthaReadOnlyServiceServer(
41 gs,
42 core.NewReadOnlyServiceHandler(s.root),
43 )
44 }
45
463. Add the service to the server
47
48 e.g.
49 s.server.AddService(f)
50
514. Start the server
52
53 s.server.Start(ctx)
54*/
55
Scott Baker104b67d2019-10-29 15:56:27 -070056// Interface allows probes to be attached to server
57// A probe must support the IsReady() method
58type ReadyProbe interface {
59 IsReady() bool
60}
61
Scott Baker2c1c4822019-10-16 11:02:41 -070062type GrpcServer struct {
63 gs *grpc.Server
64 address string
65 port int
66 secure bool
67 services []func(*grpc.Server)
Scott Baker104b67d2019-10-29 15:56:27 -070068 probe ReadyProbe // optional
Scott Baker2c1c4822019-10-16 11:02:41 -070069
70 *GrpcSecurity
71}
72
73/*
74Instantiate a GRPC server data structure
75*/
76func NewGrpcServer(
77 address string,
78 port int,
79 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,
85 port: port,
86 secure: secure,
87 GrpcSecurity: certs,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080088 probe: probe,
Scott Baker2c1c4822019-10-16 11:02:41 -070089 }
90 return server
91}
92
93/*
94Start prepares the GRPC server and starts servicing requests
95*/
96func (s *GrpcServer) Start(ctx context.Context) {
97
98 host := fmt.Sprintf("%s:%d", s.address, s.port)
99
100 lis, err := net.Listen("tcp", host)
101 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000102 logger.Fatalf("failed to listen: %v", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700103 }
104
105 if s.secure && s.GrpcSecurity != nil {
106 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
107 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000108 logger.Fatalf("could not load TLS keys: %s", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700109 }
Scott Baker104b67d2019-10-29 15:56:27 -0700110 s.gs = grpc.NewServer(grpc.Creds(creds),
111 withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700112
113 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000114 logger.Info("starting-insecure-grpc-server")
Scott Baker104b67d2019-10-29 15:56:27 -0700115 s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700116 }
117
118 // Register all required services
119 for _, service := range s.services {
120 service(s.gs)
121 }
122
123 if err := s.gs.Serve(lis); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000124 logger.Fatalf("failed to serve: %v\n", err)
Scott Baker2c1c4822019-10-16 11:02:41 -0700125 }
126}
127
Scott Baker104b67d2019-10-29 15:56:27 -0700128func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption {
129 return grpc.UnaryInterceptor(mkServerInterceptor(s))
130}
131
132// Make a serverInterceptor for the given GrpcServer
133// This interceptor will check whether there is an attached probe,
134// and if that probe indicates NotReady, then an UNAVAILABLE
135// response will be returned.
136func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
137 req interface{},
138 info *grpc.UnaryServerInfo,
139 handler grpc.UnaryHandler) (interface{}, error) {
140
141 return func(ctx context.Context,
142 req interface{},
143 info *grpc.UnaryServerInfo,
144 handler grpc.UnaryHandler) (interface{}, error) {
145
146 if (s.probe != nil) && (!s.probe.IsReady()) {
Girish Kumare6f45e82020-03-20 10:46:54 +0000147 logger.Warnf("Grpc request received while not ready %v", req)
Scott Baker104b67d2019-10-29 15:56:27 -0700148 return nil, status.Error(codes.Unavailable, "system is not ready")
149 }
150
151 // Calls the handler
152 h, err := handler(ctx, req)
153
154 return h, err
155 }
156}
157
Scott Baker2c1c4822019-10-16 11:02:41 -0700158/*
159Stop servicing GRPC requests
160*/
161func (s *GrpcServer) Stop() {
162 if s.gs != nil {
163 s.gs.Stop()
164 }
165}
166
167/*
168AddService appends a generic service request function
169*/
170func (s *GrpcServer) AddService(
171 registerFunction func(*grpc.Server),
172) {
173 s.services = append(s.services, registerFunction)
174}