Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package vpagent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | "voltha-go-controller/internal/pkg/intf" |
| 23 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 24 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 25 | |
| 26 | "github.com/golang/protobuf/ptypes/empty" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 28 | ) |
| 29 | |
| 30 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 37 | vpa.refreshDeviceList(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 38 | |
| 39 | tick := time.NewTicker(vpa.DeviceListRefreshInterval) |
| 40 | loop: |
| 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 47 | vpa.refreshDeviceList(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | tick.Stop() |
| 51 | } |
| 52 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 53 | func (vpa *VPAgent) refreshDeviceList(cntx context.Context) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 54 | // If we exit, assume disconnected |
| 55 | if vpa.volthaClient == nil { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 56 | logger.Fatal(ctx, "no-voltha-connection") |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 57 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 97 | vpa.VPClientAgent.DelDevice(cntx, toDel[i]) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | vpa.mapLock.Lock() |
| 99 | delete(vpa.clientMap, toDel[i]) |
| 100 | vpa.mapLock.Unlock() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func (vpa *VPAgent) addVPClient(device *voltha.LogicalDevice) intf.IVPClient { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 105 | logger.Debugw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": device.Id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 106 | vpa.mapLock.Lock() |
| 107 | defer vpa.mapLock.Unlock() |
| 108 | var serialNum = "Unknown" |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 109 | var mfrDesc = "Unknown" |
| 110 | var hwDesc = "Unknown" |
| 111 | var swDesc = "Unknown" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 112 | if device.Desc != nil { |
| 113 | serialNum = device.Desc.SerialNum |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 114 | mfrDesc = device.Desc.MfrDesc |
| 115 | hwDesc = device.Desc.HwDesc |
| 116 | swDesc = device.Desc.SwDesc |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 117 | } |
| 118 | vpc := vpa.clientMap[device.Id] |
| 119 | if vpc == nil { |
| 120 | vpa.VPClientAgent.AddNewDevice(&intf.VPClientCfg{ |
| 121 | DeviceID: device.Id, |
| 122 | SerialNum: serialNum, |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 123 | MfrDesc: mfrDesc, |
| 124 | HwDesc: hwDesc, |
| 125 | SwDesc: swDesc, |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 126 | SouthBoundID: device.RootDeviceId, |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 127 | TimeStamp: time.Now(), |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 128 | VolthaClient: vpa.volthaClient, |
| 129 | PacketOutChannel: vpa.packetOutChannel, |
| 130 | }) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 131 | } |
| 132 | logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": device.Id}) |
| 133 | return vpc |
| 134 | } |
| 135 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 136 | // AddClientToClientMap - called by controller once device obj is created |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 137 | func (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 | |
| 146 | func (vpa *VPAgent) getVPClient(deviceID string) intf.IVPClient { |
| 147 | if vpc, ok := vpa.clientMap[deviceID]; ok { |
| 148 | return vpc |
| 149 | } |
| 150 | return nil |
| 151 | } |