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 application |
| 17 | |
| 18 | import ( |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 19 | "context" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 20 | "errors" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 21 | "net" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 22 | |
| 23 | "strings" |
| 24 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | "voltha-go-controller/database" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 26 | common "voltha-go-controller/internal/pkg/types" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 27 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 28 | |
| 29 | "github.com/google/gopacket/layers" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 30 | ) |
| 31 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 32 | type paramsUpdationFunc func(cntx context.Context, hash string, value interface{}) error |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 33 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 34 | // map to store conversion functions |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | var updationMap = map[string]paramsUpdationFunc{ |
| 36 | database.VnetPath: updateVnets, |
| 37 | database.VpvPath: updateVpvs, |
| 38 | database.ServicePath: updateServices, |
| 39 | database.MvlanPath: updateMvlans, |
| 40 | database.IgmpGroupPath: updateIgmpGroups, |
| 41 | database.IgmpDevicePath: updateIgmpDevices, |
| 42 | database.IgmpProfPath: updateIgmpProfiles, |
| 43 | } |
| 44 | |
| 45 | // UpdateDbData to update database data |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 46 | func UpdateDbData(cntx context.Context, dbPath, hash string, value interface{}) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 47 | logger.Debugw(ctx, "Update Db Data", log.Fields{"DbPath": dbPath, "Hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 48 | if migrationFunc, ok := updationMap[dbPath]; ok { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 49 | err := migrationFunc(cntx, hash, value) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 50 | if err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 51 | return errors.New("Error-in-migration") |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 57 | // This function modifyies the old data as per current version requirement and also |
| 58 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 59 | func updateServices(cntx context.Context, hash string, value interface{}) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 60 | logger.Debugw(ctx, "Update Services", log.Fields{"Hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 61 | param := value.(*VoltService) |
| 62 | param.VnetID = VnetKey(param.SVlan, param.CVlan, param.UniVlan) |
| 63 | return nil |
| 64 | } |
| 65 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 66 | // This function modifyies the old data as per current version requirement and also |
| 67 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 68 | func updateVnets(cntx context.Context, hash string, value interface{}) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 69 | logger.Debugw(ctx, "Update Vnets", log.Fields{"Hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 70 | param := value.(*VoltVnet) |
| 71 | newKey := VnetKey(param.SVlan, param.CVlan, param.UniVlan) |
| 72 | if newKey != hash { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 73 | // Delete the older key |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 74 | _ = db.DelVnet(cntx, hash) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 75 | } else { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 76 | // Update SVlan Tag Protocol id param with default valud if not present |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 77 | if param.SVlanTpid == 0 { |
| 78 | param.SVlanTpid = layers.EthernetTypeDot1Q |
| 79 | } |
| 80 | } |
| 81 | param.Name = newKey |
| 82 | if param.DevicesList == nil || len(param.DevicesList) == 0 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 83 | param.DevicesList = append(param.DevicesList, "") // Empty OLT serial number as of now since submgr won't have proper serial num |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 88 | // This function modifyies the old data as per current version requirement and also |
| 89 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 90 | func updateVpvs(cntx context.Context, hash string, value interface{}) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 91 | logger.Debugw(ctx, "Update Vpvs", log.Fields{"Hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | //var param VoltPortVnet |
| 93 | param := value.(*VoltPortVnet) |
| 94 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 95 | // Update SVlan Tag Protocol id param with default valud if not present |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 96 | if param.SVlanTpid == 0 { |
| 97 | param.SVlanTpid = layers.EthernetTypeDot1Q |
| 98 | } |
| 99 | |
| 100 | if strings.Count(hash, "-") > 1 { |
| 101 | logger.Info(ctx, "Already upgraded") |
| 102 | return nil |
| 103 | } |
| 104 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 105 | // Add the vpv under new path |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 106 | param.WriteToDb(cntx) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 107 | // delete the older path |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 108 | fullPath := database.BasePath + database.VpvPath + hash |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 109 | if err := db.Del(cntx, fullPath); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 110 | logger.Errorw(ctx, "Vpv Delete from DB failed", log.Fields{"Error": err, "key": fullPath}) |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 115 | func updateMvlans(cntx context.Context, hash string, value interface{}) error { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 116 | logger.Debugw(ctx, "Update Mvlans", log.Fields{"Hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 117 | param := value.(*MvlanProfile) |
| 118 | if param.DevicesList == nil || len(param.DevicesList) == 0 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 119 | param.DevicesList = make(map[string]OperInProgress) // Empty OLT serial number as of now since submgr won't have proper serial num |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 120 | if err := param.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 121 | logger.Errorw(ctx, "Mvlan profile write to DB failed", log.Fields{"ProfileName": param.Name}) |
| 122 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 123 | } |
| 124 | if _, ok := param.Groups[common.StaticGroup]; ok { |
| 125 | param.Groups[common.StaticGroup].IsStatic = true |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 130 | // This function modifyies the old Igmp Group data as per current version requirement and also |
| 131 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 132 | func updateIgmpGroups(cntx context.Context, hash string, value interface{}) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 133 | ig := value.(*IgmpGroup) |
| 134 | logger.Infow(ctx, "Group Data Migration", log.Fields{"ig": ig, "GroupAddr": ig.GroupAddr, "hash": hash}) |
| 135 | if ig.GroupAddr == nil { |
| 136 | ig.GroupAddr = net.ParseIP("0.0.0.0") |
| 137 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 138 | if err := ig.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 139 | logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName}) |
| 140 | } |
| 141 | |
| 142 | return nil |
| 143 | } |
| 144 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 145 | // This function modifyies the old Igmp Device data as per current version requirement and also |
| 146 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 147 | func updateIgmpDevices(cntx context.Context, hash string, value interface{}) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 148 | igd := value.(*IgmpGroupDevice) |
| 149 | logger.Infow(ctx, "Group Device Migration", log.Fields{"igd": igd, "GroupAddr": igd.GroupAddr, "hash": hash}) |
| 150 | if igd.GroupAddr == nil { |
| 151 | igd.GroupAddr = net.ParseIP("0.0.0.0") |
| 152 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 153 | if err := igd.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 154 | logger.Errorw(ctx, "Igmp group device Write to DB failed", log.Fields{"Device": igd.Device, |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 155 | "GroupName": igd.GroupName, "GroupAddr": igd.GroupAddr.String()}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return nil |
| 159 | } |
| 160 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 161 | // This function modifyies the old Igmp Profile data as per current version requirement and also |
| 162 | // returns the new path on which the modified data has to be written |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 163 | func updateIgmpProfiles(cntx context.Context, hash string, value interface{}) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 164 | igmpProfile := value.(*IgmpProfile) |
| 165 | logger.Infow(ctx, "IGMP Profile Migration", log.Fields{"igmpProfile": igmpProfile, "hash": hash}) |
| 166 | return nil |
| 167 | } |
| 168 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 169 | func (ig *IgmpGroup) migrateIgmpDevices(cntx context.Context) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 170 | devices, _ := db.GetPrevIgmpDevices(cntx, ig.Mvlan, ig.GroupName) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 171 | logger.Infow(ctx, "Migratable Devices", log.Fields{"Devices": devices}) |
| 172 | for _, device := range devices { |
| 173 | b, ok := device.Value.([]byte) |
| 174 | if !ok { |
| 175 | logger.Warn(ctx, "The value type is not []byte") |
| 176 | continue |
| 177 | } |
| 178 | if igd, err := NewIgmpGroupDeviceFromBytes(b); err == nil { |
| 179 | key := database.BasePath + database.IgmpDevicePath + igd.Mvlan.String() + "/" + igd.GroupName + "/" + igd.Device |
| 180 | logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igd": igd}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 181 | if err := db.Del(cntx, key); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 182 | logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 183 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 184 | if err := UpdateDbData(cntx, database.IgmpDevicePath, key, igd); err != nil { |
Hitesh Chhabra | 2b2347d | 2023-07-31 17:36:48 +0530 | [diff] [blame^] | 185 | logger.Errorw(ctx, "Group Device Migration failed", log.Fields{"IGD": igd, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 186 | } else { |
| 187 | logger.Infow(ctx, "Group Device Migrated", log.Fields{"IGD": igd}) |
| 188 | } |
| 189 | } else { |
| 190 | logger.Warnw(ctx, "Unable to decode device from database", log.Fields{"str": string(b)}) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 195 | func (igd *IgmpGroupDevice) migrateIgmpChannels(cntx context.Context) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 196 | channels, _ := db.GetPrevIgmpChannels(cntx, igd.GroupName, igd.Device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 197 | logger.Infow(ctx, "Migratable Channels", log.Fields{"Channels": channels}) |
| 198 | for _, channel := range channels { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 199 | b, ok := channel.Value.([]byte) |
| 200 | if !ok { |
| 201 | logger.Warn(ctx, "The value type is not []byte") |
| 202 | continue |
| 203 | } |
| 204 | if igc, err := NewIgmpGroupChannelFromBytes(b); err == nil { |
| 205 | key := database.BasePath + database.IgmpChannelPath + igc.GroupName + "/" + igc.Device + "/" + igc.GroupAddr.String() |
| 206 | logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igc": igc}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 207 | if err := db.Del(cntx, key); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 208 | logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 209 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 210 | if err := igc.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 211 | logger.Errorw(ctx, "Igmp group channel Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr}) |
| 212 | } |
| 213 | |
| 214 | logger.Infow(ctx, "Group Channel Migrated", log.Fields{"IGD": igc}) |
| 215 | } else { |
| 216 | logger.Warnw(ctx, "Unable to decode channel from database", log.Fields{"str": string(b)}) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 221 | func (igc *IgmpGroupChannel) migrateIgmpPorts(cntx context.Context) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 222 | ports, _ := db.GetPrevIgmpRcvrs(cntx, igc.GroupAddr, igc.Device) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 223 | logger.Infow(ctx, "Migratable Ports", log.Fields{"Ports": ports}) |
| 224 | for _, port := range ports { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 225 | b, ok := port.Value.([]byte) |
| 226 | if !ok { |
| 227 | logger.Warn(ctx, "The value type is not []byte") |
| 228 | continue |
| 229 | } |
| 230 | if igp, err := NewIgmpGroupPortFromBytes(b); err == nil { |
| 231 | key := database.BasePath + database.IgmpPortPath + igc.GroupAddr.String() + "/" + igc.Device + "/" + igp.Port |
| 232 | logger.Infow(ctx, "Deleting old entry", log.Fields{"Key": key, "Igp": igp}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 233 | if err := db.Del(cntx, key); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 234 | logger.Errorw(ctx, "Igmp Group port Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 235 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 236 | if err := igp.WriteToDb(cntx, igc.Mvlan, igc.GroupAddr, igc.Device); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 237 | logger.Errorw(ctx, "Igmp group port Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr}) |
| 238 | } |
| 239 | |
| 240 | logger.Infow(ctx, "Group Port Migrated", log.Fields{"IGD": igp}) |
| 241 | } else { |
| 242 | logger.Warnw(ctx, "Unable to decode port from database", log.Fields{"str": string(b)}) |
| 243 | } |
| 244 | } |
| 245 | } |