amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 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 dmiserver |
| 18 | |
| 19 | import ( |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 20 | "context" |
Elia Battiston | e8d1fa4 | 2022-04-01 10:47:37 +0200 | [diff] [blame] | 21 | "fmt" |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 22 | "net" |
| 23 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 24 | "github.com/opencord/bbsim/api/bbsim" |
Elia Battiston | e8d1fa4 | 2022-04-01 10:47:37 +0200 | [diff] [blame] | 25 | "github.com/opencord/bbsim/internal/bbsim/devices" |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 26 | "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 | |
| 34 | var logger = log.WithFields(log.Fields{ |
| 35 | "module": "DmiServer", |
| 36 | }) |
| 37 | |
Abhay Kumar | c5723cc | 2023-06-08 12:09:30 +0530 | [diff] [blame] | 38 | // DmiAPIServer has the attributes for the Server handling the Device Management Interface |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 39 | type DmiAPIServer struct { |
Elia Battiston | e8d1fa4 | 2022-04-01 10:47:37 +0200 | [diff] [blame] | 40 | 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | var dmiServ DmiAPIServer |
| 53 | |
Abhay Kumar | c5723cc | 2023-06-08 12:09:30 +0530 | [diff] [blame] | 54 | // StartDmiAPIServer starts a new grpc server for the Device Manager Interface |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 55 | func StartDmiAPIServer() (*grpc.Server, error) { |
| 56 | dmiServ = DmiAPIServer{} |
| 57 | |
Elia Battiston | e8d1fa4 | 2022-04-01 10:47:37 +0200 | [diff] [blame] | 58 | // 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 69 | return dmiServ.newDmiAPIServer() |
| 70 | } |
| 71 | |
Elia Battiston | e8d1fa4 | 2022-04-01 10:47:37 +0200 | [diff] [blame] | 72 | func 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 79 | // newDmiAPIServer launches a new grpc server for the Device Manager Interface |
| 80 | func (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) |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 93 | bbsim.RegisterBBsimDmiServer(grpcServer, dms) |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 94 | |
| 95 | reflection.Register(grpcServer) |
| 96 | |
| 97 | go func() { _ = grpcServer.Serve(lis) }() |
| 98 | logger.Debugf("DMI grpc Server listening on %v", address) |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 99 | |
| 100 | return grpcServer, nil |
| 101 | } |