blob: ae94999be69471e0786c0af3a2761630e49d2d57 [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 "sync"
23
24 dev "github.com/opencord/opendevice-manager/pkg/models/device"
25 "github.com/opencord/opendevice-manager/pkg/sbi"
26 "github.com/opencord/voltha-lib-go/v4/pkg/log"
27)
28
29// connectMap store all device connections established
30type connectMap struct {
31 nameToAdapter map[string]sbi.Adapter // key is name and value is adapter
32 mutex *sync.RWMutex // mutex is used to lock when accessing
33}
34
35var connections *connectMap
36
37// initConnectMap initialises map for storing connections
38func initConnectMap() {
39 connections = new(connectMap)
40 connections.nameToAdapter = make(map[string]sbi.Adapter)
41 connections.mutex = &sync.RWMutex{}
42}
43
44// DeInitConnectMap clears all stored connections
45func DeInitConnectMap(ctx context.Context) {
46 connections.nameToAdapter = nil
47 connections.mutex = nil
48 connections = nil
49}
50
51// getConnection retrieves connection object from map using name
52func (conn *connectMap) getConnection(ctx context.Context, devRec *dev.DeviceRecord) (sbi.Adapter, error) {
53 conn.mutex.Lock()
54 defer conn.mutex.Unlock()
55 if val, ok := conn.nameToAdapter[devRec.Name]; ok {
56 return val, nil
57 }
58
59 // Get the right adapter
60 adapter := sbi.GetHwMgmtSvcClient(devRec)
61 if err := adapter.Connect(ctx); err != nil {
62 return nil, err
63 }
64
65 conn.nameToAdapter[devRec.Name] = adapter
66 logger.Infow(ctx, "getConnection-completed", log.Fields{"name": devRec.Name, "adapter": adapter})
67
68 return adapter, nil
69}
70
71// // storeConn stores connection object in map using uuid and name
72// func (conn *connectMap) storeConnWithName(ctx context.Context, name string, adapter sbi.Adapter) {
73// conn.mutex.Lock()
74// defer conn.mutex.Unlock()
75// conn.nameToAdapter[name] = adapter
76// logger.Infow(ctx, "storeConnWithName-completed", log.Fields{"name": name, "adapter": adapter})
77// }
78
79// delConn deletes connection object from map using uuid and name
80func (conn *connectMap) delConn(ctx context.Context, name string) {
81 conn.mutex.Lock()
82 defer conn.mutex.Unlock()
83
84 if name != "" {
85 delete(conn.nameToAdapter, name)
86 }
87 logger.Infow(ctx, "delConn-completed", log.Fields{"name": name})
88}