blob: 5aab46756b83b4dd9ebff4462fad046782c84935 [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
amit.ghosh258d14c2020-10-02 15:13:38 +02003
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
17package dmiserver
18
19import (
Humera Kousera4442952020-11-23 23:51:19 +053020 "context"
Elia Battistone8d1fa42022-04-01 10:47:37 +020021 "fmt"
amit.ghosh258d14c2020-10-02 15:13:38 +020022 "net"
23
hkouser24361d42020-12-14 19:21:47 +053024 "github.com/opencord/bbsim/api/bbsim"
Elia Battistone8d1fa42022-04-01 10:47:37 +020025 "github.com/opencord/bbsim/internal/bbsim/devices"
amit.ghosh258d14c2020-10-02 15:13:38 +020026 "github.com/opencord/bbsim/internal/common"
27 dmi "github.com/opencord/device-management-interface/go/dmi"
28 log "github.com/sirupsen/logrus"
29
30 "google.golang.org/grpc"
31 "google.golang.org/grpc/reflection"
32)
33
34var logger = log.WithFields(log.Fields{
35 "module": "DmiServer",
36})
37
Abhay Kumarc5723cc2023-06-08 12:09:30 +053038// DmiAPIServer has the attributes for the Server handling the Device Management Interface
amit.ghosh258d14c2020-10-02 15:13:38 +020039type DmiAPIServer struct {
Elia Battistone8d1fa42022-04-01 10:47:37 +020040 ipAddress string
41 uuid *dmi.Uuid
42 root *dmi.Component
43 Transceivers []*Transceiver
44 metricChannel chan interface{}
45 eventChannel chan interface{}
46 kafkaEndpoint string
47 loggingEndpoint string
48 loggingProtocol string
49 mPublisherCancelFunc context.CancelFunc
amit.ghosh258d14c2020-10-02 15:13:38 +020050}
51
52var dmiServ DmiAPIServer
53
Abhay Kumarc5723cc2023-06-08 12:09:30 +053054// StartDmiAPIServer starts a new grpc server for the Device Manager Interface
amit.ghosh258d14c2020-10-02 15:13:38 +020055func StartDmiAPIServer() (*grpc.Server, error) {
56 dmiServ = DmiAPIServer{}
57
Elia Battistone8d1fa42022-04-01 10:47:37 +020058 // Create the mapping between transceivers and PONS
59 // TODO: at the moment we create one transceiver for each PON,
60 // but in the case of COMBO PON this will not always be the case.
61 // This should be expanded to cover that scenario
62 logger.Debug("Creating transceivers from DMI")
63 dmiServ.Transceivers = []*Transceiver{}
64 for _, pon := range devices.GetOLT().Pons {
65 trans := newTransceiver(pon.ID, []*devices.PonPort{pon})
66 dmiServ.Transceivers = append(dmiServ.Transceivers, trans)
67 }
68
amit.ghosh258d14c2020-10-02 15:13:38 +020069 return dmiServ.newDmiAPIServer()
70}
71
Elia Battistone8d1fa42022-04-01 10:47:37 +020072func getDmiAPIServer() (*DmiAPIServer, error) {
73 if dmiServ.root == nil {
74 return nil, fmt.Errorf("Device management not started")
75 }
76 return &dmiServ, nil
77}
78
amit.ghosh258d14c2020-10-02 15:13:38 +020079// newDmiAPIServer launches a new grpc server for the Device Manager Interface
80func (dms *DmiAPIServer) newDmiAPIServer() (*grpc.Server, error) {
81 address := common.Config.BBSim.DmiServerAddress
82 lis, err := net.Listen("tcp", address)
83 if err != nil {
84 return nil, err
85 }
86
87 grpcServer := grpc.NewServer()
88
89 dmi.RegisterNativeHWManagementServiceServer(grpcServer, dms)
90 dmi.RegisterNativeSoftwareManagementServiceServer(grpcServer, dms)
91 dmi.RegisterNativeEventsManagementServiceServer(grpcServer, dms)
92 dmi.RegisterNativeMetricsManagementServiceServer(grpcServer, dms)
hkouser24361d42020-12-14 19:21:47 +053093 bbsim.RegisterBBsimDmiServer(grpcServer, dms)
amit.ghosh258d14c2020-10-02 15:13:38 +020094
95 reflection.Register(grpcServer)
96
97 go func() { _ = grpcServer.Serve(lis) }()
98 logger.Debugf("DMI grpc Server listening on %v", address)
amit.ghosh258d14c2020-10-02 15:13:38 +020099
100 return grpcServer, nil
101}