khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | f850dca | 2024-01-18 18:25:46 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 3 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 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 |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 7 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 9 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 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. |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 15 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "net" |
| 21 | |
| 22 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 23 | grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" |
| 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 25 | "google.golang.org/grpc" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/credentials" |
| 28 | "google.golang.org/grpc/reflection" |
| 29 | "google.golang.org/grpc/status" |
| 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 | |
| 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 | |
| 66 | type GrpcServer struct { |
| 67 | gs *grpc.Server |
| 68 | address string |
| 69 | secure bool |
| 70 | services []func(*grpc.Server) |
| 71 | probe ReadyProbe // optional |
| 72 | |
| 73 | *GrpcSecurity |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | Instantiate a GRPC server data structure |
| 78 | */ |
| 79 | func NewGrpcServer( |
| 80 | address string, |
| 81 | certs *GrpcSecurity, |
| 82 | secure bool, |
| 83 | probe ReadyProbe, |
| 84 | ) *GrpcServer { |
| 85 | server := &GrpcServer{ |
| 86 | address: address, |
| 87 | secure: secure, |
| 88 | GrpcSecurity: certs, |
| 89 | probe: probe, |
| 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 | |
| 99 | lis, err := net.Listen("tcp", s.address) |
| 100 | if err != nil { |
| 101 | logger.Fatalf(ctx, "failed to listen: %v", err) |
| 102 | } |
| 103 | |
| 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( |
| 107 | grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| 108 | )), |
| 109 | grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( |
| 110 | grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
| 111 | mkServerInterceptor(s), |
| 112 | ))} |
| 113 | |
| 114 | if s.secure && s.GrpcSecurity != nil { |
| 115 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 116 | if err != nil { |
| 117 | logger.Fatalf(ctx, "could not load TLS keys: %s", err) |
| 118 | } |
| 119 | |
| 120 | serverOptions = append(serverOptions, grpc.Creds(creds)) |
| 121 | s.gs = grpc.NewServer(serverOptions...) |
| 122 | } else { |
| 123 | logger.Info(ctx, "starting-insecure-grpc-server") |
| 124 | s.gs = grpc.NewServer(serverOptions...) |
| 125 | } |
| 126 | |
| 127 | // Register all required services |
| 128 | for _, service := range s.services { |
| 129 | service(s.gs) |
| 130 | } |
| 131 | reflection.Register(s.gs) |
| 132 | |
| 133 | if err := s.gs.Serve(lis); err != nil { |
| 134 | logger.Fatalf(ctx, "failed to serve: %v\n", err) |
| 135 | } |
| 136 | } |
| 137 | |
| 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()) { |
| 153 | logger.Warnf(ctx, "Grpc request received while not ready %v", req) |
| 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 | |
| 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 | } |