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" |
| 20 | "fmt" |
Scott Baker | ce76700 | 2019-10-23 13:30:24 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc/credentials" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 25 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 26 | "net" |
| 27 | ) |
| 28 | |
| 29 | /* |
| 30 | To add a GRPC server to your existing component simply follow these steps: |
| 31 | |
| 32 | 1. 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 | |
| 37 | 2. 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 | |
| 47 | 3. Add the service to the server |
| 48 | |
| 49 | e.g. |
| 50 | s.server.AddService(f) |
| 51 | |
| 52 | 4. Start the server |
| 53 | |
| 54 | s.server.Start(ctx) |
| 55 | */ |
| 56 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 57 | // Interface allows probes to be attached to server |
| 58 | // A probe must support the IsReady() method |
| 59 | type ReadyProbe interface { |
| 60 | IsReady() bool |
| 61 | } |
| 62 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 63 | type GrpcServer struct { |
| 64 | gs *grpc.Server |
| 65 | address string |
| 66 | port int |
| 67 | secure bool |
| 68 | services []func(*grpc.Server) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 69 | probe ReadyProbe // optional |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | |
| 71 | *GrpcSecurity |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | Instantiate a GRPC server data structure |
| 76 | */ |
| 77 | func NewGrpcServer( |
| 78 | address string, |
| 79 | port int, |
| 80 | certs *GrpcSecurity, |
| 81 | secure bool, |
| 82 | ) *GrpcServer { |
| 83 | server := &GrpcServer{ |
| 84 | address: address, |
| 85 | port: port, |
| 86 | secure: secure, |
| 87 | GrpcSecurity: certs, |
| 88 | } |
| 89 | return server |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | Start prepares the GRPC server and starts servicing requests |
| 94 | */ |
| 95 | func (s *GrpcServer) Start(ctx context.Context) { |
| 96 | |
| 97 | host := fmt.Sprintf("%s:%d", s.address, s.port) |
| 98 | |
| 99 | lis, err := net.Listen("tcp", host) |
| 100 | if err != nil { |
| 101 | log.Fatalf("failed to listen: %v", err) |
| 102 | } |
| 103 | |
| 104 | if s.secure && s.GrpcSecurity != nil { |
| 105 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 106 | if err != nil { |
| 107 | log.Fatalf("could not load TLS keys: %s", err) |
| 108 | } |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 109 | s.gs = grpc.NewServer(grpc.Creds(creds), |
| 110 | withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 111 | |
| 112 | } else { |
| 113 | log.Info("starting-insecure-grpc-server") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 114 | s.gs = grpc.NewServer(withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Register all required services |
| 118 | for _, service := range s.services { |
| 119 | service(s.gs) |
| 120 | } |
| 121 | |
| 122 | if err := s.gs.Serve(lis); err != nil { |
| 123 | log.Fatalf("failed to serve: %v\n", err) |
| 124 | } |
| 125 | } |
| 126 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 127 | // Attach a readiness probe to the server. |
| 128 | // If the probe returns NotReady, the server will return UNAVAILABLE |
| 129 | func (s *GrpcServer) AttachReadyProbe(p ReadyProbe) { |
| 130 | s.probe = p |
| 131 | } |
| 132 | |
| 133 | func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption { |
| 134 | return grpc.UnaryInterceptor(mkServerInterceptor(s)) |
| 135 | } |
| 136 | |
| 137 | // Make a serverInterceptor for the given GrpcServer |
| 138 | // This interceptor will check whether there is an attached probe, |
| 139 | // and if that probe indicates NotReady, then an UNAVAILABLE |
| 140 | // response will be returned. |
| 141 | func mkServerInterceptor(s *GrpcServer) func(ctx context.Context, |
| 142 | req interface{}, |
| 143 | info *grpc.UnaryServerInfo, |
| 144 | handler grpc.UnaryHandler) (interface{}, error) { |
| 145 | |
| 146 | return func(ctx context.Context, |
| 147 | req interface{}, |
| 148 | info *grpc.UnaryServerInfo, |
| 149 | handler grpc.UnaryHandler) (interface{}, error) { |
| 150 | |
| 151 | if (s.probe != nil) && (!s.probe.IsReady()) { |
| 152 | log.Warnf("Grpc request received while not ready %v", req) |
| 153 | return nil, status.Error(codes.Unavailable, "system is not ready") |
| 154 | } |
| 155 | |
| 156 | // Calls the handler |
| 157 | h, err := handler(ctx, req) |
| 158 | |
| 159 | return h, err |
| 160 | } |
| 161 | } |
| 162 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 163 | /* |
| 164 | Stop servicing GRPC requests |
| 165 | */ |
| 166 | func (s *GrpcServer) Stop() { |
| 167 | if s.gs != nil { |
| 168 | s.gs.Stop() |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | AddService appends a generic service request function |
| 174 | */ |
| 175 | func (s *GrpcServer) AddService( |
| 176 | registerFunction func(*grpc.Server), |
| 177 | ) { |
| 178 | s.services = append(s.services, registerFunction) |
| 179 | } |