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