blob: 27614085dba228297ea71f38873ea20eaea1e10e [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"
108 if device.Desc != nil {
109 serialNum = device.Desc.SerialNum
110 }
111 vpc := vpa.clientMap[device.Id]
112 if vpc == nil {
113 vpa.VPClientAgent.AddNewDevice(&intf.VPClientCfg{
114 DeviceID: device.Id,
115 SerialNum: serialNum,
116 SouthBoundID: device.RootDeviceId,
117 VolthaClient: vpa.volthaClient,
118 PacketOutChannel: vpa.packetOutChannel,
119 })
120
121 }
122 logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": device.Id})
123 return vpc
124}
125
126//AddClientToClientMap - called by controller once device obj is created
127func (vpa *VPAgent) AddClientToClientMap(deviceID string, vpc intf.IVPClient) {
128 vpa.mapLock.Lock()
129 defer vpa.mapLock.Unlock()
130
131 if vpc != nil {
132 vpa.clientMap[deviceID] = vpc
133 }
134}
135
136func (vpa *VPAgent) getVPClient(deviceID string) intf.IVPClient {
137 if vpc, ok := vpa.clientMap[deviceID]; ok {
138 return vpc
139 }
140 return nil
141}