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 | "context" |
| 20 | "encoding/json" |
| 21 | "errors" |
| 22 | "voltha-go-controller/internal/pkg/types" |
| 23 | "sync" |
| 24 | |
| 25 | "github.com/google/gopacket/layers" |
| 26 | |
| 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 | |
| 31 | const ( |
| 32 | //MigrationComplete Represents the Migration Complete |
| 33 | MigrationComplete = "Completed" |
| 34 | //MigrationInProgress Represents the Migration Inprogress |
| 35 | MigrationInProgress = "InProgress" |
| 36 | //MigrationFailed Represents the Migration Failed |
| 37 | MigrationFailed = "Failed" |
| 38 | // StatusNone for no operations |
| 39 | StatusNone = "NONE" |
| 40 | //ModuleToBeDeleted - module where old version is deleted |
| 41 | ModuleToBeDeleted = "ModuleToBeDeleted" |
| 42 | ) |
| 43 | |
| 44 | //DataMigration represents the Verison and Status info for Major Version Upgrade. |
| 45 | type DataMigration struct { |
| 46 | Version string |
| 47 | Status string |
| 48 | ModuleVer map[string]string // eg. "service": "v1" |
| 49 | } |
| 50 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 51 | type paramsMigrationFunc func(context.Context, []byte) string |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 52 | |
| 53 | //map to store conversion functions |
| 54 | var migrationMap = map[string]paramsMigrationFunc{ |
| 55 | database.ServicePath: MigrateServices, |
| 56 | database.DevicePath: MigrateDevices, |
| 57 | database.DevicePortPath: MigrateDevicePorts, |
| 58 | database.DeviceFlowPath: MigrateDeviceFlows, |
| 59 | database.DeviceGroupPath: MigrateDeviceGroups, |
| 60 | database.DeviceMeterPath: MigrateDeviceMeters, |
| 61 | database.VnetPath: MigrateVnets, |
| 62 | database.VpvPath: MigrateVpvs, |
| 63 | database.MvlanPath: MigrateMvlans, |
| 64 | database.MeterPath: MigrateMeters, |
| 65 | database.IgmpConfPath: MigrateIgmpConfs, |
| 66 | database.IgmpGroupPath: MigrateIgmpGroups, |
| 67 | database.IgmpDevicePath: MigrateIgmpDevices, |
| 68 | database.IgmpChannelPath: MigrateIgmpChannels, |
| 69 | database.IgmpPortPath: MigrateIgmpPorts, |
| 70 | database.IgmpProfPath: MigrateIgmpProfs, |
| 71 | database.McastConfigPath: MigrateMcastConfs, |
| 72 | database.LogLevelPath: MigrateLogLevels, |
| 73 | database.HealthPath: MigrateHealth, |
| 74 | database.PonCounterPath: MigratePonCounters, |
| 75 | database.ChannelCounterPath: MigrateChannelCounters, |
| 76 | database.ServiceCounterPath: MigrateServiceCounters, |
| 77 | database.NbDevicePath: MigrateNbDevices, |
| 78 | database.DeviceFlowHashPath: MigrateDeviceFlowHash, |
| 79 | } |
| 80 | |
| 81 | // WriteToDb write a meter profile to DB |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 82 | func (md *DataMigration) WriteToDb(cntx context.Context) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 83 | b, err := json.Marshal(md) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 87 | if err1 := db.PutMigrationInfo(cntx, string(b)); err1 != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 88 | return err1 |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // DelFromDb delete a meter profile from DB |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 94 | func (md *DataMigration) DelFromDb(cntx context.Context) { |
| 95 | if err := db.DelMigrationInfo(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 96 | logger.Warnw(ctx, "DelMigrationInfo Failed", log.Fields{"Error": err}) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // GetMigrationInfo to get data migration info |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 101 | func GetMigrationInfo(cntx context.Context, dmInfo *DataMigration) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 102 | var migrationInfo string |
| 103 | var err error |
| 104 | if db == nil { |
| 105 | db = database.GetDatabase() |
| 106 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 107 | if migrationInfo, err = db.GetMigrationInfo(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 108 | return err |
| 109 | } |
| 110 | err = json.Unmarshal([]byte(migrationInfo), &dmInfo) |
| 111 | if err != nil { |
| 112 | logger.Warn(ctx, "Unmarshal of migrationinfo failed") |
| 113 | return err |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // CheckIfMigrationRequired Checks if Migration is Completed |
| 119 | // Only Data Migration and Reboot would be handled in the Below function |
| 120 | // When Roll back happens just Delete of DB keys has to happen |
| 121 | // which will be done once delete key request is received from MSM |
| 122 | func CheckIfMigrationRequired(ctx context.Context) bool { |
| 123 | Migrate := new(DataMigration) |
| 124 | var NoDataInDB bool |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 125 | err := GetMigrationInfo(ctx, Migrate) |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 126 | logger.Debugw(ctx, "Migration data", log.Fields{"DataMigration": Migrate}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 127 | // No DB entry represents N verison Bring Up for the First time |
| 128 | if err != nil { |
| 129 | NoDataInDB = true |
| 130 | logger.Error(ctx, "Failed to read the Migration Data from DB ") |
| 131 | } |
| 132 | // Covers N verison bringup and Reboot Senarios |
| 133 | if NoDataInDB { |
| 134 | logger.Info(ctx, "Data Migration Not Required") |
| 135 | Migrate.Version = database.PresentVersion |
| 136 | Migrate.Status = MigrationComplete |
| 137 | Migrate.ModuleVer = database.PresentVersionMap |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 138 | if err := Migrate.WriteToDb(ctx); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 139 | logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 140 | } |
| 141 | //MigrateProbestatus has to be Updated to Complete when No Migration is Required |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 142 | logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 143 | //probe.UpdateDBMigrationStatus(ctx, true) |
| 144 | return false |
| 145 | // Migration required when vgc moves to Higher Versions |
| 146 | } else if Migrate.ModuleVer == nil { |
| 147 | // This case will hit when DataMigration is present with old schema |
| 148 | // and DataMigration schema has changed. |
| 149 | // In this case compare previous and current version configured in the models. |
| 150 | for key, currVer := range database.PresentVersionMap { |
| 151 | if currVer > database.PreviousVersionMap[key] { |
| 152 | logger.Infow(ctx, "DB Migration needed for", log.Fields{"comp": key}) |
| 153 | return true |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | var isVersionChanged bool |
| 158 | // Compare the current version with previous version present in DB. |
| 159 | // This case will also hit in case of POD restart. |
| 160 | for key, currVer := range database.PresentVersionMap { |
| 161 | if dbVer := Migrate.ModuleVer[key]; dbVer != "" { |
| 162 | if currVer > dbVer { |
| 163 | logger.Infow(ctx, "DB Migration needed for", log.Fields{"comp": key}) |
| 164 | isVersionChanged = true |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | database.DBVersionMap = Migrate.ModuleVer // Store DB data |
| 169 | |
| 170 | if isVersionChanged { |
| 171 | return true |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // In case Service Reboots/Rolls Back then Probe Success to MSM |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 176 | logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 177 | //probe.UpdateDBMigrationStatus(ctx, true) |
| 178 | return false |
| 179 | } |
| 180 | |
| 181 | // InitiateDataMigration Migrates the DB data |
| 182 | // depending on the bool value returned by CheckIfMigrationDone |
| 183 | func InitiateDataMigration(ctx context.Context) { |
| 184 | var err error |
| 185 | Migrate := new(DataMigration) |
| 186 | var migrationWG sync.WaitGroup |
| 187 | |
| 188 | //Keeping it outside to avoid race condition where the |
| 189 | // wait check is reached before the go toutine for data migraiton is triggered |
| 190 | migrationWG.Add(1) |
| 191 | |
| 192 | go func() { |
| 193 | logger.Debug(ctx, "Started Go Routine for data migration") |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 194 | err = MigrateDBData(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 195 | if err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 196 | logger.Errorw(ctx, "Failed to Migrate the Data", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 197 | Migrate.Status = MigrationFailed |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 198 | if err := Migrate.WriteToDb(ctx); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 199 | logger.Errorw(ctx, "DB Write failed to Migration Path", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | logger.Debug(ctx, "Completed Go Routine for data migration") |
| 203 | migrationWG.Done() |
| 204 | |
| 205 | Migrate.Version = database.PresentVersion |
| 206 | Migrate.Status = MigrationInProgress |
| 207 | Migrate.ModuleVer = database.PresentVersionMap |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 208 | if err = Migrate.WriteToDb(ctx); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 209 | logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 210 | return |
| 211 | } |
| 212 | }() |
| 213 | // Failure Senario can be Exceptions, incase of panic Update the status as failed |
| 214 | defer func() { |
| 215 | if err := recover(); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 216 | logger.Errorw(ctx, "Migration failure due to Exception happend", log.Fields{"reason": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 217 | Migrate.Status = MigrationFailed |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 218 | if err := Migrate.WriteToDb(ctx); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 219 | logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 220 | } |
| 221 | //probe.UpdateDBMigrationStatus(ctx, false) |
| 222 | return |
| 223 | } |
| 224 | }() |
| 225 | // Wait for all the Db data migration to complete |
| 226 | migrationWG.Wait() |
| 227 | //probe.UpdateDBMigrationStatus(ctx, true) |
| 228 | Migrate.Status = MigrationComplete |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 229 | if err := Migrate.WriteToDb(ctx); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 230 | logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 231 | } |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 232 | logger.Infow(ctx, "Migration completed successfully", log.Fields{"Status": Migrate.Status}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // MigrateDBData to migrate database data |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 236 | func MigrateDBData(cntx context.Context) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 237 | |
| 238 | var err error |
| 239 | for module, currentVersion := range database.PresentVersionMap { |
| 240 | if currentVersion == database.DBVersionMap[module] { |
| 241 | logger.Infow(ctx, "No Data Migration required for module", log.Fields{"Table": module, "Version": currentVersion}) |
| 242 | continue |
| 243 | } |
| 244 | |
| 245 | if _, ok := migrationMap[module]; ok { |
| 246 | switch module { |
| 247 | case database.DeviceFlowPath, |
| 248 | database.DevicePortPath, |
| 249 | database.DeviceMeterPath, |
| 250 | database.DeviceGroupPath, |
| 251 | database.DeviceFlowHashPath: |
| 252 | err = FetchAndMigrateDeviceDBData(module) |
| 253 | default: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 254 | err = FetchAndMigrateDBData(cntx, module) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 255 | } |
| 256 | } else { |
| 257 | logger.Infow(ctx, "No Data Migration handling found for module", log.Fields{"Table": module, "Version": currentVersion}) |
| 258 | } |
| 259 | |
| 260 | if err != nil { |
| 261 | logger.Errorw(ctx, "Error in data migration", log.Fields{"Module": module}) |
| 262 | return err |
| 263 | } |
| 264 | } |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | //FetchAndMigrateDeviceDBData fetchs the data from database and migrte the same to latest versions and store ot back ot database |
| 269 | func FetchAndMigrateDeviceDBData(module string) error { |
| 270 | logger.Error(ctx, "Data Migration not implemented for Device DB Data") |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | //FetchAndMigrateDBData fetchs the data from database and migrte the same to latest versions and store ot back ot database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 275 | func FetchAndMigrateDBData(cntx context.Context, module string) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 276 | |
| 277 | previousPath := database.GetModuleKeypath(module, database.PreviousVersionMap[module]) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 278 | dbPathKeysValueMap, err := db.List(cntx, previousPath) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 279 | if err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 280 | logger.Errorw(ctx, "failed to Fetch the Keys from Redis", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 281 | //No return required, Data might not be present in DB |
| 282 | return nil |
| 283 | } |
| 284 | if len(dbPathKeysValueMap) == 0 { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 285 | logger.Debugw(ctx, "No data present in DB for the path", log.Fields{"dbPath": module}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 286 | return nil |
| 287 | } |
| 288 | |
| 289 | // Fetch each Path from previous version and store to present version after data migration changes |
| 290 | for hash, value := range dbPathKeysValueMap { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 291 | logger.Debugw(ctx, "DB path", log.Fields{"hash": hash}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 292 | //convert the value to a specific type based on the dbPath |
| 293 | b, ok := value.Value.([]byte) |
| 294 | if !ok { |
| 295 | logger.Error(ctx, "The value type is not []byte") |
| 296 | return errors.New("Error-in-migration") |
| 297 | } |
| 298 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 299 | presentParams := migrationMap[module](cntx, b) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 300 | logger.Infow(ctx, "Migrated data", log.Fields{"presentParams": presentParams}) |
| 301 | if "" == presentParams { |
| 302 | logger.Error(ctx, "Error in migrating data\n") |
| 303 | return errors.New("Error-in-migration") |
| 304 | } else if ModuleToBeDeleted == presentParams { |
| 305 | return nil |
| 306 | } |
| 307 | presentPath := database.GetKeyPath(module) + hash |
| 308 | logger.Infow(ctx, "Before writing to DB", log.Fields{"presentParams": presentParams}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 309 | if err := db.Put(cntx, presentPath, presentParams); err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 310 | logger.Errorw(ctx, "Update Params failed", log.Fields{"key": presentPath, "presentparams": presentParams}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 311 | return err |
| 312 | } |
| 313 | } |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | //MigrateServices modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 318 | func MigrateServices(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 319 | var vs VoltService |
| 320 | var updatedData, updatedData1 []byte |
| 321 | var vsmap map[string]interface{} |
| 322 | var err1 error |
| 323 | |
| 324 | err := json.Unmarshal(data, &vsmap) |
| 325 | if err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 326 | logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 327 | return "" |
| 328 | } |
| 329 | // changes to handle change in data type of MacLearning parameter |
| 330 | if updatedData1, err1 = json.Marshal(&vsmap); err1 != nil { |
| 331 | logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()}) |
| 332 | return "" |
| 333 | } |
| 334 | |
| 335 | if err2 := json.Unmarshal(updatedData1, &vs); err != nil { |
| 336 | logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2}) |
| 337 | return "" |
| 338 | } |
| 339 | |
| 340 | if vsmap["MacLearning"] == true { |
| 341 | vs.MacLearning = Learn |
| 342 | |
| 343 | } |
| 344 | |
| 345 | //Migration |
| 346 | vs.PendingFlows = make(map[string]bool) |
| 347 | vs.AssociatedFlows = make(map[string]bool) |
| 348 | vs.DeleteInProgress = false |
| 349 | vs.PonPort = 0xFF |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 350 | if updatedData, err = vs.JsonMarshal(); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 351 | logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err.Error()}) |
| 352 | return "" |
| 353 | } |
| 354 | logger.Infow(ctx, "Service Migrated", log.Fields{"Service": vs.Name, "PresentVersion": database.PresentVersionMap[database.ServicePath]}) |
| 355 | return string(updatedData) |
| 356 | } |
| 357 | |
| 358 | //MigrateDevices modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 359 | func MigrateDevices(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 360 | logger.Error(ctx, "Data Migration not implemented for Devices") |
| 361 | return "" |
| 362 | } |
| 363 | |
| 364 | //MigrateDevicePorts modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 365 | func MigrateDevicePorts(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 366 | logger.Error(ctx, "Data Migration not implemented for Ports") |
| 367 | return "" |
| 368 | } |
| 369 | |
| 370 | //MigrateDeviceFlows modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 371 | func MigrateDeviceFlows(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 372 | logger.Error(ctx, "Data Migration not implemented for Flows") |
| 373 | return "" |
| 374 | } |
| 375 | |
| 376 | //MigrateDeviceGroups modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 377 | func MigrateDeviceGroups(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 378 | logger.Error(ctx, "Data Migration not implemented for Groups") |
| 379 | return "" |
| 380 | } |
| 381 | |
| 382 | //MigrateDeviceMeters modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 383 | func MigrateDeviceMeters(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 384 | logger.Error(ctx, "Data Migration not implemented for Meters") |
| 385 | return "" |
| 386 | } |
| 387 | |
| 388 | //MigrateDeviceFlowHash modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 389 | func MigrateDeviceFlowHash(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 390 | logger.Error(ctx, "Data Migration not implemented for FlowHash") |
| 391 | return "" |
| 392 | } |
| 393 | |
| 394 | //MigrateVnets modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 395 | func MigrateVnets(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 396 | |
| 397 | var vnet VoltVnet |
| 398 | var updatedData []byte |
| 399 | |
| 400 | err := json.Unmarshal(data, &vnet) |
| 401 | if err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 402 | logger.Warnw(ctx, "Unmarshal of VNET failed", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 403 | return "" |
| 404 | } |
| 405 | |
| 406 | if vnet.SVlanTpid == 0 { |
| 407 | vnet.SVlanTpid = layers.EthernetTypeDot1Q |
| 408 | } |
| 409 | // MacLeanring parameter was not stored in vnets in 2.7 release. |
| 410 | if vnet.DhcpRelay || vnet.ArpLearning { |
| 411 | vnet.MacLearning = Learn |
| 412 | } else if !vnet.DhcpRelay && !vnet.ArpLearning { |
| 413 | vnet.MacLearning = MacLearningNone |
| 414 | } |
| 415 | vnet.PendingDeleteFlow = make(map[string]map[string]bool) |
| 416 | vnet.DeleteInProgress = false |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 417 | if updatedData, err = vnet.JsonMarshal(); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 418 | logger.Warnw(ctx, "Marshal of Vnet failed", log.Fields{"Error": err.Error()}) |
| 419 | return "" |
| 420 | } |
| 421 | logger.Infow(ctx, "Vnet Migrated", log.Fields{"Vnet Name": vnet.Name, "PresentVersion": database.PresentVersionMap[database.VnetPath]}) |
| 422 | return string(updatedData) |
| 423 | } |
| 424 | |
| 425 | //MigrateVpvs modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 426 | func MigrateVpvs(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 427 | var vpv VoltPortVnet |
| 428 | var updatedData, updatedData1 []byte |
| 429 | var vpvmap map[string]interface{} |
| 430 | var err1 error |
| 431 | var usFlowsApplied, dsFlowsApplied bool |
| 432 | |
| 433 | err := json.Unmarshal(data, &vpvmap) |
| 434 | if err != nil { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 435 | logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 436 | return "" |
| 437 | } |
| 438 | // changes to handle change in data type of MacLearning parameter |
| 439 | if updatedData1, err1 = json.Marshal(&vpvmap); err1 != nil { |
| 440 | logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()}) |
| 441 | return "" |
| 442 | } |
| 443 | |
| 444 | if err2 := json.Unmarshal(updatedData1, &vpv); err != nil { |
| 445 | logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2}) |
| 446 | |
| 447 | } |
| 448 | |
| 449 | if vpvmap["MacLearning"] == true { |
| 450 | vpv.MacLearning = Learn |
| 451 | |
| 452 | } |
| 453 | if vpvmap["UsFlowsApplied"] == true { |
| 454 | usFlowsApplied = true |
| 455 | } |
| 456 | |
| 457 | if vpvmap["DsFlowsApplied"] == true { |
| 458 | dsFlowsApplied = true |
| 459 | } |
| 460 | |
| 461 | if usFlowsApplied && dsFlowsApplied { |
| 462 | vpv.FlowsApplied = true |
| 463 | } |
| 464 | //Migration |
| 465 | if vpv.SVlanTpid == 0 { |
| 466 | vpv.SVlanTpid = layers.EthernetTypeDot1Q |
| 467 | } |
| 468 | vpv.VnetName = VnetKey(vpv.SVlan, vpv.CVlan, vpv.UniVlan) |
| 469 | vpv.PendingDeleteFlow = make(map[string]bool) |
| 470 | vpv.PonPort = 0xFF |
| 471 | |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 472 | if updatedData, err = vpv.JsonMarshal(); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 473 | logger.Warnw(ctx, "Marshal of VPV failed", log.Fields{"Error": err.Error()}) |
| 474 | return "" |
| 475 | } |
| 476 | logger.Infow(ctx, "VPV Migrated", log.Fields{"Device": vpv.Device, "port": vpv.Port, "SVlan": vpv.SVlan, |
| 477 | "CVlan": vpv.CVlan, "UniVlan": vpv.UniVlan, "PresentVersion": database.PresentVersionMap[database.VpvPath]}) |
| 478 | return string(updatedData) |
| 479 | } |
| 480 | |
| 481 | //MigrateMvlans modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 482 | func MigrateMvlans(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 483 | var mvp MvlanProfile |
| 484 | var updatedData []byte |
| 485 | |
| 486 | err := json.Unmarshal(data, &mvp) |
| 487 | if err != nil { |
| 488 | logger.Warn(ctx, "Unmarshal of VPV failed") |
| 489 | return "" |
| 490 | } |
| 491 | // Mvlan Migration |
| 492 | mvp.IgmpServVersion = make(map[string]*uint8) |
| 493 | for srNo := range mvp.DevicesList { |
| 494 | var servVersion uint8 |
| 495 | mvp.IgmpServVersion[srNo] = &servVersion |
| 496 | } |
| 497 | |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 498 | if updatedData, err = mvp.JsonMarshal(); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 499 | logger.Warnw(ctx, "Marshal of Mvlan Profile failed", log.Fields{"Error": err.Error()}) |
| 500 | return "" |
| 501 | } |
| 502 | logger.Infow(ctx, "Mvlan Profile Migrated", log.Fields{"MvlanProfileName": mvp.Name, "PresentVersion": database.PresentVersionMap[database.MvlanPath]}) |
| 503 | return string(updatedData) |
| 504 | } |
| 505 | |
| 506 | //MigrateMeters modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 507 | func MigrateMeters(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 508 | logger.Error(ctx, "Data Migration not implemented for Meters") |
| 509 | return "" |
| 510 | } |
| 511 | |
| 512 | //MigrateIgmpConfs modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 513 | func MigrateIgmpConfs(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 514 | var igmpProfile IgmpProfile |
| 515 | |
| 516 | err := json.Unmarshal(data, &igmpProfile) |
| 517 | if err != nil { |
| 518 | logger.Warn(ctx, "Unmarshal of IGMP failed") |
| 519 | return "" |
| 520 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 521 | if err := igmpProfile.WriteToDb(cntx); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 522 | logger.Errorw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProfile.ProfileID}) |
| 523 | } |
| 524 | |
| 525 | logger.Infow(ctx, "Igmp Conf Migrated", log.Fields{"Profile": igmpProfile, "PresentVersion": database.PresentVersionMap[database.VpvPath]}) |
| 526 | return ModuleToBeDeleted |
| 527 | } |
| 528 | |
| 529 | //MigrateIgmpGroups modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 530 | func MigrateIgmpGroups(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 531 | logger.Error(ctx, "Data Migration not implemented for IGMP Groups") |
| 532 | return "" |
| 533 | } |
| 534 | |
| 535 | //MigrateIgmpDevices modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 536 | func MigrateIgmpDevices(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 537 | logger.Error(ctx, "Data Migration not implemented for IGMP Device") |
| 538 | return "" |
| 539 | } |
| 540 | |
| 541 | //MigrateIgmpChannels modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 542 | func MigrateIgmpChannels(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 543 | logger.Error(ctx, "Data Migration not implemented for IGMP Channels") |
| 544 | return "" |
| 545 | } |
| 546 | |
| 547 | //MigrateIgmpPorts modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 548 | func MigrateIgmpPorts(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 549 | logger.Error(ctx, "Data Migration not implemented for IGMP Ports") |
| 550 | return "" |
| 551 | } |
| 552 | |
| 553 | //MigrateIgmpProfs modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 554 | func MigrateIgmpProfs(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 555 | logger.Error(ctx, "Data Migration not implemented for IGMP Profs") |
| 556 | return "" |
| 557 | } |
| 558 | |
| 559 | //MigrateMcastConfs modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 560 | func MigrateMcastConfs(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 561 | logger.Error(ctx, "Data Migration not implemented for Mcast Confs") |
| 562 | return "" |
| 563 | } |
| 564 | |
| 565 | //MigrateLogLevels modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 566 | func MigrateLogLevels(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 567 | logger.Error(ctx, "Data Migration not implemented for Log Levels") |
| 568 | return "" |
| 569 | } |
| 570 | |
| 571 | //MigrateHealth modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 572 | func MigrateHealth(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 573 | logger.Error(ctx, "Data Migration not implemented for Health") |
| 574 | return "" |
| 575 | } |
| 576 | |
| 577 | //MigratePonCounters modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 578 | func MigratePonCounters(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 579 | logger.Error(ctx, "Data Migration not implemented for Pon Counters") |
| 580 | return "" |
| 581 | } |
| 582 | |
| 583 | //MigrateChannelCounters modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 584 | func MigrateChannelCounters(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 585 | logger.Error(ctx, "Data Migration not implemented for Channel Counters") |
| 586 | return "" |
| 587 | } |
| 588 | |
| 589 | //MigrateServiceCounters modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 590 | func MigrateServiceCounters(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 591 | logger.Error(ctx, "Data Migration not implemented for Service Counters") |
| 592 | return "" |
| 593 | } |
| 594 | |
| 595 | //MigrateNbDevices modifyies the old data as per current version requirement and updates the database |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 596 | func MigrateNbDevices(cntx context.Context, data []byte) string { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 597 | logger.Error(ctx, "Data Migration not implemented for NB Devices") |
| 598 | return "" |
| 599 | } |
| 600 | |
| 601 | //MigrateFlowHash modifyies the old data as per current version requirement and updates the database |
| 602 | func MigrateFlowHash(data []byte) string { |
| 603 | logger.Error(ctx, "Data Migration not implemented for FLow Hash") |
| 604 | return "" |
| 605 | } |
| 606 | |
| 607 | //DeleteDbPathKeys Deleted the paths from DB |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 608 | func DeleteDbPathKeys(cntx context.Context, keyPath string) error { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 609 | logger.Debugw(ctx, "Deleting paths for version", log.Fields{"Path": keyPath}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 610 | |
| 611 | // Delete all the keys |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 612 | err := db.DeleteAll(cntx, keyPath) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 613 | if err != nil && err.Error() != common.ErrEntryNotFound.Error() { |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 614 | logger.Errorw(ctx, "Delete Key failed", log.Fields{"error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 615 | return err |
| 616 | } |
| 617 | return nil |
| 618 | } |