blob: a0514cfc0547811b306880998187c04715fd8c6f [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package vpagent
17
18import (
19 "context"
20 "time"
21
22 "voltha-go-controller/internal/pkg/intf"
23
Tinoj Joseph1d108322022-07-13 10:07:39 +053024 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053025
26 "github.com/golang/protobuf/ptypes/empty"
Naveen Sampath04696f72022-06-13 15:19:14 +053027 "github.com/opencord/voltha-protos/v5/go/voltha"
28)
29
30func (vpa *VPAgent) synchronizeDeviceList(ctx context.Context) {
31 // Send reconnection indication to the devices already known
32 for _, vpc := range vpa.clientMap {
33 vpc.ConnectInd(context.TODO(), intf.DeviceReDisc)
34 }
35
36 // Refresh once to get everything started
Tinoj Joseph07cc5372022-07-18 22:53:51 +053037 vpa.refreshDeviceList(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +053038
39 tick := time.NewTicker(vpa.DeviceListRefreshInterval)
40loop:
41 for {
42 select {
43 case <-ctx.Done():
44 logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx})
45 break loop
46 case <-tick.C:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053047 vpa.refreshDeviceList(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +053048 }
49 }
50 tick.Stop()
51}
52
Tinoj Joseph07cc5372022-07-18 22:53:51 +053053func (vpa *VPAgent) refreshDeviceList(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +053054 // If we exit, assume disconnected
55 if vpa.volthaClient == nil {
Akash Soni6168f312023-05-18 20:57:33 +053056 logger.Fatal(ctx, "no-voltha-connection")
Naveen Sampath04696f72022-06-13 15:19:14 +053057 vpa.events <- vpaEventVolthaDisconnected
58 return
59 }
60 deviceList, err := vpa.volthaClient.Get().ListLogicalDevices(context.Background(), &empty.Empty{})
61 if err != nil {
62 logger.Errorw(ctx, "vpagent failed to query device list from voltha",
63 log.Fields{"error": err})
64 vpa.events <- vpaEventVolthaDisconnected
65 return
66 }
67
68 var toAdd []int
69 var toDel []string
70 var deviceIDMap = make(map[string]string)
71 for index, d := range deviceList.Items {
72 deviceID := d.Id
73 deviceIDMap[deviceID] = deviceID
74 if vpa.clientMap[deviceID] == nil {
75 toAdd = append(toAdd, index)
76 }
77 }
78 for key := range vpa.clientMap {
79 deviceID, ok := deviceIDMap[key]
80 if !ok || (ok && deviceID == "") {
81 toDel = append(toDel, key)
82 }
83 }
84 logger.Debugw(ctx, "Device Refresh", log.Fields{"ToAdd": toAdd, "ToDel": toDel})
85 for i := 0; i < len(toAdd); i++ {
86 device := deviceList.Items[toAdd[i]]
87 serialNum := device.Desc.SerialNum
88 // If the blocked device list contain device serial number, do not add VPClient.
89 if vpa.VPClientAgent.IsBlockedDevice(serialNum) {
90 logger.Debugw(ctx, "Device Serial Number is present in the blocked device list", log.Fields{"device-serial-number": serialNum})
91 } else {
92 vpa.addVPClient(device) // client is started in addVPClient
93 }
94 }
95
96 for i := 0; i < len(toDel); i++ {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097 vpa.VPClientAgent.DelDevice(cntx, toDel[i])
Naveen Sampath04696f72022-06-13 15:19:14 +053098 vpa.mapLock.Lock()
99 delete(vpa.clientMap, toDel[i])
100 vpa.mapLock.Unlock()
101 }
102}
103
104func (vpa *VPAgent) addVPClient(device *voltha.LogicalDevice) intf.IVPClient {
Akash Soni6168f312023-05-18 20:57:33 +0530105 logger.Debugw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": device.Id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530106 vpa.mapLock.Lock()
107 defer vpa.mapLock.Unlock()
108 var serialNum = "Unknown"
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530109 var mfrDesc = "Unknown"
110 var hwDesc = "Unknown"
111 var swDesc = "Unknown"
Naveen Sampath04696f72022-06-13 15:19:14 +0530112 if device.Desc != nil {
113 serialNum = device.Desc.SerialNum
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530114 mfrDesc = device.Desc.MfrDesc
115 hwDesc = device.Desc.HwDesc
116 swDesc = device.Desc.SwDesc
Naveen Sampath04696f72022-06-13 15:19:14 +0530117 }
118 vpc := vpa.clientMap[device.Id]
119 if vpc == nil {
120 vpa.VPClientAgent.AddNewDevice(&intf.VPClientCfg{
121 DeviceID: device.Id,
122 SerialNum: serialNum,
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530123 MfrDesc: mfrDesc,
124 HwDesc: hwDesc,
125 SwDesc: swDesc,
Naveen Sampath04696f72022-06-13 15:19:14 +0530126 SouthBoundID: device.RootDeviceId,
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530127 TimeStamp: time.Now(),
Naveen Sampath04696f72022-06-13 15:19:14 +0530128 VolthaClient: vpa.volthaClient,
129 PacketOutChannel: vpa.packetOutChannel,
130 })
Naveen Sampath04696f72022-06-13 15:19:14 +0530131 }
132 logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": device.Id})
133 return vpc
134}
135
vinokuma926cb3e2023-03-29 11:41:06 +0530136// AddClientToClientMap - called by controller once device obj is created
Naveen Sampath04696f72022-06-13 15:19:14 +0530137func (vpa *VPAgent) AddClientToClientMap(deviceID string, vpc intf.IVPClient) {
138 vpa.mapLock.Lock()
139 defer vpa.mapLock.Unlock()
140
141 if vpc != nil {
142 vpa.clientMap[deviceID] = vpc
143 }
144}
145
146func (vpa *VPAgent) getVPClient(deviceID string) intf.IVPClient {
147 if vpc, ok := vpa.clientMap[deviceID]; ok {
148 return vpc
149 }
150 return nil
151}