blob: 0441fcfb66c558d0e6133d9f53bbd84763e30c70 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbarie7dfae952018-08-13 11:43:17 -040016package grpc
17
18import (
Stephane Barbarie7dfae952018-08-13 11:43:17 -040019 "context"
khenaidoobf6e7bb2018-08-14 22:27:29 -040020 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040021 "google.golang.org/grpc"
22 "google.golang.org/grpc/credentials"
khenaidoobf6e7bb2018-08-14 22:27:29 -040023 "net"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040024 "strconv"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040025 "strings"
26)
27
28/*
29To add a GRPC server to your existing component simply follow these steps:
30
311. 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
362. 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
463. Add the service to the server
47
48 e.g.
49 s.server.AddService(f)
50
514. Start the server
52
53 s.server.Start(ctx)
khenaidoobf6e7bb2018-08-14 22:27:29 -040054*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040055
56type 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/*
67Instantiate a GRPC server data structure
68*/
69func 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/*
85Start prepares the GRPC server and starts servicing requests
86*/
87func (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/*
121Stop servicing GRPC requests
122*/
123func (s *GrpcServer) Stop() {
124 s.gs.Stop()
125}
126
127/*
128AddService appends a generic service request function
129*/
130func (s *GrpcServer) AddService(
131 registerFunction func(*grpc.Server),
132) {
133 s.services = append(s.services, registerFunction)
134}