blob: 71e33ade7a220b72a14349150e1b28b161880654 [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
24 "github.com/golang/protobuf/ptypes/empty"
Tinoj Joseph1d108322022-07-13 10:07:39 +053025 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053026 "github.com/opencord/voltha-protos/v5/go/voltha"
27)
28
29func (vpa *VPAgent) synchronizeDeviceList(ctx context.Context) {
30 // Send reconnection indication to the devices already known
31 for _, vpc := range vpa.clientMap {
32 vpc.ConnectInd(context.TODO(), intf.DeviceReDisc)
33 }
34
35 // Refresh once to get everything started
Tinoj Joseph07cc5372022-07-18 22:53:51 +053036 vpa.refreshDeviceList(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +053037
38 tick := time.NewTicker(vpa.DeviceListRefreshInterval)
39loop:
40 for {
41 select {
42 case <-ctx.Done():
43 logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx})
44 break loop
45 case <-tick.C:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053046 vpa.refreshDeviceList(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +053047 }
48 }
49 tick.Stop()
50}
51
Tinoj Joseph07cc5372022-07-18 22:53:51 +053052func (vpa *VPAgent) refreshDeviceList(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +053053 // If we exit, assume disconnected
54 if vpa.volthaClient == nil {
55 logger.Error(ctx, "no-voltha-connection")
56 vpa.events <- vpaEventVolthaDisconnected
57 return
58 }
59 deviceList, err := vpa.volthaClient.Get().ListLogicalDevices(context.Background(), &empty.Empty{})
60 if err != nil {
61 logger.Errorw(ctx, "vpagent failed to query device list from voltha",
62 log.Fields{"error": err})
63 vpa.events <- vpaEventVolthaDisconnected
64 return
65 }
66
67 var toAdd []int
68 var toDel []string
69 var deviceIDMap = make(map[string]string)
70 for index, d := range deviceList.Items {
71 deviceID := d.Id
72 deviceIDMap[deviceID] = deviceID
73 if vpa.clientMap[deviceID] == nil {
74 toAdd = append(toAdd, index)
75 }
76 }
77 for key := range vpa.clientMap {
78 deviceID, ok := deviceIDMap[key]
79 if !ok || (ok && deviceID == "") {
80 toDel = append(toDel, key)
81 }
82 }
83 logger.Debugw(ctx, "Device Refresh", log.Fields{"ToAdd": toAdd, "ToDel": toDel})
84 for i := 0; i < len(toAdd); i++ {
85 device := deviceList.Items[toAdd[i]]
86 serialNum := device.Desc.SerialNum
87 // If the blocked device list contain device serial number, do not add VPClient.
88 if vpa.VPClientAgent.IsBlockedDevice(serialNum) {
89 logger.Debugw(ctx, "Device Serial Number is present in the blocked device list", log.Fields{"device-serial-number": serialNum})
90 } else {
91 vpa.addVPClient(device) // client is started in addVPClient
92 }
93 }
94
95 for i := 0; i < len(toDel); i++ {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053096 vpa.VPClientAgent.DelDevice(cntx, toDel[i])
Naveen Sampath04696f72022-06-13 15:19:14 +053097 vpa.mapLock.Lock()
98 delete(vpa.clientMap, toDel[i])
99 vpa.mapLock.Unlock()
100 }
101}
102
103func (vpa *VPAgent) addVPClient(device *voltha.LogicalDevice) intf.IVPClient {
104 logger.Warnw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": device.Id})
105 vpa.mapLock.Lock()
106 defer vpa.mapLock.Unlock()
107 var serialNum = "Unknown"
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530108 var mfrDesc = "Unknown"
109 var hwDesc = "Unknown"
110 var swDesc = "Unknown"
Naveen Sampath04696f72022-06-13 15:19:14 +0530111 if device.Desc != nil {
112 serialNum = device.Desc.SerialNum
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530113 mfrDesc = device.Desc.MfrDesc
114 hwDesc = device.Desc.HwDesc
115 swDesc = device.Desc.SwDesc
Naveen Sampath04696f72022-06-13 15:19:14 +0530116 }
117 vpc := vpa.clientMap[device.Id]
118 if vpc == nil {
119 vpa.VPClientAgent.AddNewDevice(&intf.VPClientCfg{
120 DeviceID: device.Id,
121 SerialNum: serialNum,
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530122 MfrDesc: mfrDesc,
123 HwDesc: hwDesc,
124 SwDesc: swDesc,
Naveen Sampath04696f72022-06-13 15:19:14 +0530125 SouthBoundID: device.RootDeviceId,
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530126 TimeStamp: time.Now(),
Naveen Sampath04696f72022-06-13 15:19:14 +0530127 VolthaClient: vpa.volthaClient,
128 PacketOutChannel: vpa.packetOutChannel,
129 })
130
131 }
132 logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": device.Id})
133 return vpc
134}
135
136//AddClientToClientMap - called by controller once device obj is created
137func (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}