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