Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 20 | "net" |
| 21 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 22 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 23 | grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 26 | "google.golang.org/grpc/codes" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc/credentials" |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 28 | "google.golang.org/grpc/reflection" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 29 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | /* |
| 33 | To add a GRPC server to your existing component simply follow these steps: |
| 34 | |
| 35 | 1. 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 | |
| 40 | 2. 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 | |
| 50 | 3. Add the service to the server |
| 51 | |
| 52 | e.g. |
| 53 | s.server.AddService(f) |
| 54 | |
| 55 | 4. Start the server |
| 56 | |
| 57 | s.server.Start(ctx) |
| 58 | */ |
| 59 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 60 | // Interface allows probes to be attached to server |
| 61 | // A probe must support the IsReady() method |
| 62 | type ReadyProbe interface { |
| 63 | IsReady() bool |
| 64 | } |
| 65 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | type GrpcServer struct { |
| 67 | gs *grpc.Server |
| 68 | address string |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 69 | secure bool |
| 70 | services []func(*grpc.Server) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 71 | probe ReadyProbe // optional |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 72 | |
| 73 | *GrpcSecurity |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | Instantiate a GRPC server data structure |
| 78 | */ |
| 79 | func NewGrpcServer( |
| 80 | address string, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 81 | certs *GrpcSecurity, |
| 82 | secure bool, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 83 | probe ReadyProbe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 84 | ) *GrpcServer { |
| 85 | server := &GrpcServer{ |
| 86 | address: address, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 87 | secure: secure, |
| 88 | GrpcSecurity: certs, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 89 | probe: probe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 90 | } |
| 91 | return server |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | Start prepares the GRPC server and starts servicing requests |
| 96 | */ |
| 97 | func (s *GrpcServer) Start(ctx context.Context) { |
| 98 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 99 | lis, err := net.Listen("tcp", s.address) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 100 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 101 | logger.Fatalf(ctx, "failed to listen: %v", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 104 | // 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 Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 107 | grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 108 | )), |
| 109 | grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 110 | grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 111 | mkServerInterceptor(s), |
| 112 | ))} |
| 113 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 114 | if s.secure && s.GrpcSecurity != nil { |
| 115 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 116 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 117 | logger.Fatalf(ctx, "could not load TLS keys: %s", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 118 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 119 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 120 | serverOptions = append(serverOptions, grpc.Creds(creds)) |
| 121 | s.gs = grpc.NewServer(serverOptions...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 122 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 123 | logger.Info(ctx, "starting-insecure-grpc-server") |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 124 | s.gs = grpc.NewServer(serverOptions...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Register all required services |
| 128 | for _, service := range s.services { |
| 129 | service(s.gs) |
| 130 | } |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 131 | reflection.Register(s.gs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 132 | |
| 133 | if err := s.gs.Serve(lis); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 134 | logger.Fatalf(ctx, "failed to serve: %v\n", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 138 | // 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. |
| 142 | func 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()) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 153 | logger.Warnf(ctx, "Grpc request received while not ready %v", req) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 154 | 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 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 164 | /* |
| 165 | Stop servicing GRPC requests |
| 166 | */ |
| 167 | func (s *GrpcServer) Stop() { |
| 168 | if s.gs != nil { |
| 169 | s.gs.Stop() |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | AddService appends a generic service request function |
| 175 | */ |
| 176 | func (s *GrpcServer) AddService( |
| 177 | registerFunction func(*grpc.Server), |
| 178 | ) { |
| 179 | s.services = append(s.services, registerFunction) |
| 180 | } |