khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [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 | */ |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 16 | package grpc |
| 17 | |
| 18 | import ( |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 19 | "context" |
khenaidoo | 5aadea0 | 2018-11-07 14:30:11 -0500 | [diff] [blame] | 20 | "fmt" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 22 | "google.golang.org/grpc" |
| 23 | "google.golang.org/grpc/credentials" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 24 | "net" |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | /* |
| 28 | To add a GRPC server to your existing component simply follow these steps: |
| 29 | |
| 30 | 1. Create a server instance by passing the host and port where it should run and optionally add certificate information |
| 31 | |
| 32 | e.g. |
| 33 | s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false) |
| 34 | |
| 35 | 2. Create a function that will register your service with the GRPC server |
| 36 | |
| 37 | e.g. |
| 38 | f := func(gs *grpc.Server) { |
| 39 | voltha.RegisterVolthaReadOnlyServiceServer( |
| 40 | gs, |
| 41 | core.NewReadOnlyServiceHandler(s.root), |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | 3. Add the service to the server |
| 46 | |
| 47 | e.g. |
| 48 | s.server.AddService(f) |
| 49 | |
| 50 | 4. Start the server |
| 51 | |
| 52 | s.server.Start(ctx) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 53 | */ |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 54 | |
| 55 | type GrpcServer struct { |
| 56 | gs *grpc.Server |
| 57 | address string |
| 58 | port int |
| 59 | secure bool |
| 60 | services []func(*grpc.Server) |
| 61 | |
| 62 | *GrpcSecurity |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | Instantiate a GRPC server data structure |
| 67 | */ |
| 68 | func NewGrpcServer( |
| 69 | address string, |
| 70 | port int, |
| 71 | certs *GrpcSecurity, |
| 72 | secure bool, |
| 73 | ) *GrpcServer { |
| 74 | server := &GrpcServer{ |
| 75 | address: address, |
| 76 | port: port, |
| 77 | secure: secure, |
| 78 | GrpcSecurity: certs, |
| 79 | } |
| 80 | return server |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | Start prepares the GRPC server and starts servicing requests |
| 85 | */ |
| 86 | func (s *GrpcServer) Start(ctx context.Context) { |
khenaidoo | 5aadea0 | 2018-11-07 14:30:11 -0500 | [diff] [blame] | 87 | |
| 88 | host := fmt.Sprintf("%s:%d", s.address, s.port) |
Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame] | 89 | |
| 90 | lis, err := net.Listen("tcp", host) |
| 91 | if err != nil { |
| 92 | log.Fatalf("failed to listen: %v", err) |
| 93 | } |
| 94 | |
| 95 | if s.secure && s.GrpcSecurity != nil { |
| 96 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 97 | if err != nil { |
| 98 | log.Fatalf("could not load TLS keys: %s", err) |
| 99 | } |
| 100 | s.gs = grpc.NewServer(grpc.Creds(creds)) |
| 101 | |
| 102 | } else { |
| 103 | log.Info("starting-insecure-grpc-server") |
| 104 | s.gs = grpc.NewServer() |
| 105 | } |
| 106 | |
| 107 | // Register all required services |
| 108 | for _, service := range s.services { |
| 109 | service(s.gs) |
| 110 | } |
| 111 | |
| 112 | if err := s.gs.Serve(lis); err != nil { |
| 113 | log.Fatalf("failed to serve: %v\n", err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | Stop servicing GRPC requests |
| 119 | */ |
| 120 | func (s *GrpcServer) Stop() { |
| 121 | s.gs.Stop() |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | AddService appends a generic service request function |
| 126 | */ |
| 127 | func (s *GrpcServer) AddService( |
| 128 | registerFunction func(*grpc.Server), |
| 129 | ) { |
| 130 | s.services = append(s.services, registerFunction) |
| 131 | } |