blob: 4f80e8c9213008369ccf28273c7233ae505e4ddb [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
2 * Copyright 2020-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
17// Package nbi holds rpc server apis implemented
18package nbi
19
20import (
21 "context"
22 "net"
23 "os"
24
25 "github.com/opencord/opendevice-manager/pkg/config"
26
27 "github.com/opencord/device-management-interface/go/dmi"
28
29 "github.com/opencord/voltha-lib-go/v4/pkg/log"
30 g "google.golang.org/grpc"
31 "google.golang.org/grpc/credentials"
32 "google.golang.org/grpc/reflection"
33)
34
35// logger represents the log object
36var logger log.CLogger
37
38// grpcServer refers to object which holds grpc serevr
39var grpcServer *g.Server
40
41// init function for the package
42func init() {
43 logger = config.Initlog()
44 initConnectMap()
45}
46
47func registerServers(grpcServer *g.Server) {
48 hwMgmtSvc := new(NativeHwManagementService)
49 dmi.RegisterNativeHWManagementServiceServer(grpcServer, hwMgmtSvc)
50 reflection.Register(grpcServer)
51}
52
53// StartGrpcServer starts the grpc server for listening to NEM requests
54func StartGrpcServer(ctx context.Context) {
55 coreFlags := config.GetCoreFlags()
56 lis, err := net.Listen("tcp", coreFlags.GrpcEndPoint)
57 if err != nil {
58 logger.Error(ctx, "Failed-to-listen-on-Grpc-Port", log.Fields{"grpc-flags": coreFlags.GrpcFlags, "error": err})
59 os.Exit(1)
60 }
61
62 if coreFlags.SecureConnection {
63 creds, err := credentials.NewServerTLSFromFile(coreFlags.ServerCrt, coreFlags.ServerKey)
64 if err != nil {
65 logger.Error(ctx, "could-not-process-the-credentials", log.Fields{"error": err})
66 }
67 grpcServer = g.NewServer(g.Creds(creds))
68 } else {
69 grpcServer = g.NewServer()
70 }
71
72 registerServers(grpcServer)
73
74 logger.Infow(ctx, "Grpc-server-starting", log.Fields{"grpc-server-info": grpcServer, "grpc-env-info": coreFlags.GrpcFlags, "is-secure-conn": coreFlags.SecureConnection})
75
76 // Starting the server
77 if err := grpcServer.Serve(lis); err != nil {
78 logger.Errorw(ctx, "Failed-to-start-Grpc-Server", log.Fields{"server": coreFlags.GrpcFlags, "error": err})
79 os.Exit(1)
80 }
81
82 logger.Infow(ctx, "grpc-server-stopped-successfully", log.Fields{"grpc-server-info": grpcServer, "grpc-env-info": coreFlags.GrpcFlags})
83
84}
85
86// StopGrpcServer tear down the gRPC connection from opendevice manager to NEM
87func StopGrpcServer(ctx context.Context) {
88 if grpcServer != nil {
89 grpcServer.GracefulStop()
90 }
91 logger.Infow(ctx, "grpc-server-teardown-success", log.Fields{"grpc-server-info": grpcServer})
92}