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