blob: 66408cad3d93d48fa1f7ffc9de73087413610520 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
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
16package application
17
18import (
19 "context"
20 "encoding/json"
21 "errors"
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053022 "fmt"
Naveen Sampath04696f72022-06-13 15:19:14 +053023 "sync"
vinokuma926cb3e2023-03-29 11:41:06 +053024 common "voltha-go-controller/internal/pkg/types"
Naveen Sampath04696f72022-06-13 15:19:14 +053025
26 "github.com/google/gopacket/layers"
27
28 "voltha-go-controller/database"
Tinoj Joseph1d108322022-07-13 10:07:39 +053029 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053030)
31
32const (
vinokuma926cb3e2023-03-29 11:41:06 +053033 // MigrationComplete Represents the Migration Complete
Naveen Sampath04696f72022-06-13 15:19:14 +053034 MigrationComplete = "Completed"
vinokuma926cb3e2023-03-29 11:41:06 +053035 // MigrationInProgress Represents the Migration Inprogress
Naveen Sampath04696f72022-06-13 15:19:14 +053036 MigrationInProgress = "InProgress"
vinokuma926cb3e2023-03-29 11:41:06 +053037 // MigrationFailed Represents the Migration Failed
Naveen Sampath04696f72022-06-13 15:19:14 +053038 MigrationFailed = "Failed"
39 // StatusNone for no operations
40 StatusNone = "NONE"
vinokuma926cb3e2023-03-29 11:41:06 +053041 // ModuleToBeDeleted - module where old version is deleted
Naveen Sampath04696f72022-06-13 15:19:14 +053042 ModuleToBeDeleted = "ModuleToBeDeleted"
43)
44
vinokuma926cb3e2023-03-29 11:41:06 +053045// DataMigration represents the Version and Status info for Major Version Upgrade.
Naveen Sampath04696f72022-06-13 15:19:14 +053046type DataMigration struct {
vinokuma926cb3e2023-03-29 11:41:06 +053047 ModuleVer map[string]string // eg. "service": "v1"
Naveen Sampath04696f72022-06-13 15:19:14 +053048 Version string
49 Status string
Naveen Sampath04696f72022-06-13 15:19:14 +053050}
51
Tinoj Joseph07cc5372022-07-18 22:53:51 +053052type paramsMigrationFunc func(context.Context, []byte) string
Naveen Sampath04696f72022-06-13 15:19:14 +053053
vinokuma926cb3e2023-03-29 11:41:06 +053054// map to store conversion functions
Naveen Sampath04696f72022-06-13 15:19:14 +053055var migrationMap = map[string]paramsMigrationFunc{
56 database.ServicePath: MigrateServices,
57 database.DevicePath: MigrateDevices,
58 database.DevicePortPath: MigrateDevicePorts,
59 database.DeviceFlowPath: MigrateDeviceFlows,
60 database.DeviceGroupPath: MigrateDeviceGroups,
61 database.DeviceMeterPath: MigrateDeviceMeters,
62 database.VnetPath: MigrateVnets,
63 database.VpvPath: MigrateVpvs,
64 database.MvlanPath: MigrateMvlans,
65 database.MeterPath: MigrateMeters,
66 database.IgmpConfPath: MigrateIgmpConfs,
67 database.IgmpGroupPath: MigrateIgmpGroups,
68 database.IgmpDevicePath: MigrateIgmpDevices,
69 database.IgmpChannelPath: MigrateIgmpChannels,
70 database.IgmpPortPath: MigrateIgmpPorts,
71 database.IgmpProfPath: MigrateIgmpProfs,
72 database.McastConfigPath: MigrateMcastConfs,
73 database.LogLevelPath: MigrateLogLevels,
74 database.HealthPath: MigrateHealth,
75 database.PonCounterPath: MigratePonCounters,
76 database.ChannelCounterPath: MigrateChannelCounters,
77 database.ServiceCounterPath: MigrateServiceCounters,
78 database.NbDevicePath: MigrateNbDevices,
79 database.DeviceFlowHashPath: MigrateDeviceFlowHash,
80}
81
82// WriteToDb write a meter profile to DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +053083func (md *DataMigration) WriteToDb(cntx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053084 b, err := json.Marshal(md)
85 if err != nil {
86 return err
87 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +053088 if err1 := db.PutMigrationInfo(cntx, string(b)); err1 != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053089 return err1
90 }
91 return nil
92}
93
94// DelFromDb delete a meter profile from DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +053095func (md *DataMigration) DelFromDb(cntx context.Context) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053096 logger.Debug(ctx, "Received Delete from Db")
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097 if err := db.DelMigrationInfo(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053098 logger.Warnw(ctx, "DelMigrationInfo Failed", log.Fields{"Error": err})
99 }
100}
101
102// GetMigrationInfo to get data migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530103func GetMigrationInfo(cntx context.Context, dmInfo *DataMigration) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530104 logger.Debug(ctx, "Get Migration Info")
Naveen Sampath04696f72022-06-13 15:19:14 +0530105 var migrationInfo string
106 var err error
107 if db == nil {
108 db = database.GetDatabase()
109 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530110 if migrationInfo, err = db.GetMigrationInfo(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530111 return err
112 }
113 err = json.Unmarshal([]byte(migrationInfo), &dmInfo)
114 if err != nil {
115 logger.Warn(ctx, "Unmarshal of migrationinfo failed")
116 return err
117 }
118 return nil
119}
120
121// CheckIfMigrationRequired Checks if Migration is Completed
122// Only Data Migration and Reboot would be handled in the Below function
123// When Roll back happens just Delete of DB keys has to happen
124// which will be done once delete key request is received from MSM
125func CheckIfMigrationRequired(ctx context.Context) bool {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530126 logger.Debug(ctx, "Check If Migration Required")
Naveen Sampath04696f72022-06-13 15:19:14 +0530127 Migrate := new(DataMigration)
128 var NoDataInDB bool
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530129 err := GetMigrationInfo(ctx, Migrate)
Tinoj Joseph1d108322022-07-13 10:07:39 +0530130 logger.Debugw(ctx, "Migration data", log.Fields{"DataMigration": Migrate})
vinokuma926cb3e2023-03-29 11:41:06 +0530131 // No DB entry represents N version Bring Up for the First time
Naveen Sampath04696f72022-06-13 15:19:14 +0530132 if err != nil {
133 NoDataInDB = true
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530134 logger.Warn(ctx, "Failed to read the Migration Data from DB ")
Naveen Sampath04696f72022-06-13 15:19:14 +0530135 }
vinokuma926cb3e2023-03-29 11:41:06 +0530136 // Covers N version bringup and Reboot Scenarios
Naveen Sampath04696f72022-06-13 15:19:14 +0530137 if NoDataInDB {
138 logger.Info(ctx, "Data Migration Not Required")
139 Migrate.Version = database.PresentVersion
140 Migrate.Status = MigrationComplete
141 Migrate.ModuleVer = database.PresentVersionMap
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530142 if err := Migrate.WriteToDb(ctx); err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530143 logger.Warnw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530144 }
vinokuma926cb3e2023-03-29 11:41:06 +0530145 // MigrateProbestatus has to be Updated to Complete when No Migration is Required
Tinoj Joseph1d108322022-07-13 10:07:39 +0530146 logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status})
vinokuma926cb3e2023-03-29 11:41:06 +0530147 // probe.UpdateDBMigrationStatus(ctx, true)
Naveen Sampath04696f72022-06-13 15:19:14 +0530148 return false
149 // Migration required when vgc moves to Higher Versions
150 } else if Migrate.ModuleVer == nil {
151 // This case will hit when DataMigration is present with old schema
152 // and DataMigration schema has changed.
153 // In this case compare previous and current version configured in the models.
154 for key, currVer := range database.PresentVersionMap {
155 if currVer > database.PreviousVersionMap[key] {
156 logger.Infow(ctx, "DB Migration needed for", log.Fields{"comp": key})
157 return true
158 }
159 }
160 } else {
161 var isVersionChanged bool
162 // Compare the current version with previous version present in DB.
163 // This case will also hit in case of POD restart.
164 for key, currVer := range database.PresentVersionMap {
165 if dbVer := Migrate.ModuleVer[key]; dbVer != "" {
166 if currVer > dbVer {
167 logger.Infow(ctx, "DB Migration needed for", log.Fields{"comp": key})
168 isVersionChanged = true
169 }
170 }
171 }
172 database.DBVersionMap = Migrate.ModuleVer // Store DB data
173
174 if isVersionChanged {
175 return true
176 }
177 }
178
179 // In case Service Reboots/Rolls Back then Probe Success to MSM
Tinoj Joseph1d108322022-07-13 10:07:39 +0530180 logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status})
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 //probe.UpdateDBMigrationStatus(ctx, true)
182 return false
183}
184
185// InitiateDataMigration Migrates the DB data
186// depending on the bool value returned by CheckIfMigrationDone
187func InitiateDataMigration(ctx context.Context) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530188 logger.Debug(ctx, "Initiate Data Migration")
Naveen Sampath04696f72022-06-13 15:19:14 +0530189 var err error
190 Migrate := new(DataMigration)
191 var migrationWG sync.WaitGroup
192
vinokuma926cb3e2023-03-29 11:41:06 +0530193 // Keeping it outside to avoid race condition where the
Naveen Sampath04696f72022-06-13 15:19:14 +0530194 // wait check is reached before the go toutine for data migraiton is triggered
195 migrationWG.Add(1)
196
197 go func() {
198 logger.Debug(ctx, "Started Go Routine for data migration")
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530199 err = MigrateDBData(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530200 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530201 logger.Errorw(ctx, "Failed to Migrate the Data", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530202 Migrate.Status = MigrationFailed
vinokuma926cb3e2023-03-29 11:41:06 +0530203 if err = Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530204 logger.Errorw(ctx, "DB Write failed to Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530205 }
206 }
207 logger.Debug(ctx, "Completed Go Routine for data migration")
208 migrationWG.Done()
209
210 Migrate.Version = database.PresentVersion
211 Migrate.Status = MigrationInProgress
212 Migrate.ModuleVer = database.PresentVersionMap
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530213 if err = Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530214 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530215 return
216 }
217 }()
218 // Failure Senario can be Exceptions, incase of panic Update the status as failed
219 defer func() {
220 if err := recover(); err != nil {
vinokuma926cb3e2023-03-29 11:41:06 +0530221 logger.Errorw(ctx, "Migration failure due to Exception happened", log.Fields{"reason": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530222 Migrate.Status = MigrationFailed
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530223 if err := Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530224 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530225 }
226 //probe.UpdateDBMigrationStatus(ctx, false)
227 return
228 }
229 }()
230 // Wait for all the Db data migration to complete
231 migrationWG.Wait()
232 //probe.UpdateDBMigrationStatus(ctx, true)
233 Migrate.Status = MigrationComplete
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530234 if err := Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530235 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530236 }
Tinoj Joseph1d108322022-07-13 10:07:39 +0530237 logger.Infow(ctx, "Migration completed successfully", log.Fields{"Status": Migrate.Status})
Naveen Sampath04696f72022-06-13 15:19:14 +0530238}
239
240// MigrateDBData to migrate database data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530241func MigrateDBData(cntx context.Context) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530242 logger.Debug(ctx, "Migrate DB Data")
Naveen Sampath04696f72022-06-13 15:19:14 +0530243 var err error
244 for module, currentVersion := range database.PresentVersionMap {
245 if currentVersion == database.DBVersionMap[module] {
246 logger.Infow(ctx, "No Data Migration required for module", log.Fields{"Table": module, "Version": currentVersion})
247 continue
248 }
249
250 if _, ok := migrationMap[module]; ok {
251 switch module {
252 case database.DeviceFlowPath,
253 database.DevicePortPath,
254 database.DeviceMeterPath,
255 database.DeviceGroupPath,
256 database.DeviceFlowHashPath:
257 err = FetchAndMigrateDeviceDBData(module)
258 default:
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530259 err = FetchAndMigrateDBData(cntx, module)
Naveen Sampath04696f72022-06-13 15:19:14 +0530260 }
261 } else {
262 logger.Infow(ctx, "No Data Migration handling found for module", log.Fields{"Table": module, "Version": currentVersion})
263 }
264
265 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530266 return fmt.Errorf("error in data migration for module %s : %w", module, err)
Naveen Sampath04696f72022-06-13 15:19:14 +0530267 }
268 }
269 return nil
270}
271
vinokuma926cb3e2023-03-29 11:41:06 +0530272// FetchAndMigrateDeviceDBData fetchs the data from database and migrte the same to latest versions and store ot back ot database
Naveen Sampath04696f72022-06-13 15:19:14 +0530273func FetchAndMigrateDeviceDBData(module string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530274 logger.Errorw(ctx, "Data Migration not implemented for Device DB Data", log.Fields{"Table": module})
Naveen Sampath04696f72022-06-13 15:19:14 +0530275 return nil
276}
277
vinokuma926cb3e2023-03-29 11:41:06 +0530278// FetchAndMigrateDBData fetchs the data from database and migrte the same to latest versions and store ot back ot database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530279func FetchAndMigrateDBData(cntx context.Context, module string) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530280 logger.Debugw(ctx, "Fetch And Migrate DB Data", log.Fields{"Module": module})
Naveen Sampath04696f72022-06-13 15:19:14 +0530281 previousPath := database.GetModuleKeypath(module, database.PreviousVersionMap[module])
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530282 dbPathKeysValueMap, err := db.List(cntx, previousPath)
Naveen Sampath04696f72022-06-13 15:19:14 +0530283 if err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530284 logger.Warnw(ctx, "failed to Fetch the Keys from Redis", log.Fields{"error": err})
vinokuma926cb3e2023-03-29 11:41:06 +0530285 // No return required, Data might not be present in DB
Naveen Sampath04696f72022-06-13 15:19:14 +0530286 return nil
287 }
288 if len(dbPathKeysValueMap) == 0 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530289 logger.Warnw(ctx, "No data present in DB for the path", log.Fields{"dbPath": module})
Naveen Sampath04696f72022-06-13 15:19:14 +0530290 return nil
291 }
292
293 // Fetch each Path from previous version and store to present version after data migration changes
294 for hash, value := range dbPathKeysValueMap {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530295 logger.Debugw(ctx, "DB path", log.Fields{"hash": hash})
vinokuma926cb3e2023-03-29 11:41:06 +0530296 // convert the value to a specific type based on the dbPath
Naveen Sampath04696f72022-06-13 15:19:14 +0530297 b, ok := value.Value.([]byte)
298 if !ok {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530299 return errors.New("Error-in-migration, The value type is not []byt")
Naveen Sampath04696f72022-06-13 15:19:14 +0530300 }
301
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530302 presentParams := migrationMap[module](cntx, b)
Naveen Sampath04696f72022-06-13 15:19:14 +0530303 logger.Infow(ctx, "Migrated data", log.Fields{"presentParams": presentParams})
304 if "" == presentParams {
Naveen Sampath04696f72022-06-13 15:19:14 +0530305 return errors.New("Error-in-migration")
306 } else if ModuleToBeDeleted == presentParams {
307 return nil
308 }
309 presentPath := database.GetKeyPath(module) + hash
310 logger.Infow(ctx, "Before writing to DB", log.Fields{"presentParams": presentParams})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530311 if err := db.Put(cntx, presentPath, presentParams); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530312 logger.Errorw(ctx, "Update Params failed", log.Fields{"key": presentPath, "presentparams": presentParams})
Naveen Sampath04696f72022-06-13 15:19:14 +0530313 return err
314 }
315 }
316 return nil
317}
318
vinokuma926cb3e2023-03-29 11:41:06 +0530319// MigrateServices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530320func MigrateServices(cntx context.Context, data []byte) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530321 logger.Debug(ctx, "Migrate Services")
Naveen Sampath04696f72022-06-13 15:19:14 +0530322 var vs VoltService
323 var updatedData, updatedData1 []byte
324 var vsmap map[string]interface{}
325 var err1 error
326
327 err := json.Unmarshal(data, &vsmap)
328 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530329 logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530330 return ""
331 }
332 // changes to handle change in data type of MacLearning parameter
333 if updatedData1, err1 = json.Marshal(&vsmap); err1 != nil {
334 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()})
335 return ""
336 }
337
338 if err2 := json.Unmarshal(updatedData1, &vs); err != nil {
339 logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2})
340 return ""
341 }
342
343 if vsmap["MacLearning"] == true {
344 vs.MacLearning = Learn
Naveen Sampath04696f72022-06-13 15:19:14 +0530345 }
346
vinokuma926cb3e2023-03-29 11:41:06 +0530347 // Migration
Naveen Sampath04696f72022-06-13 15:19:14 +0530348 vs.PendingFlows = make(map[string]bool)
349 vs.AssociatedFlows = make(map[string]bool)
350 vs.DeleteInProgress = false
351 vs.PonPort = 0xFF
vinokuma926cb3e2023-03-29 11:41:06 +0530352 if updatedData, err = vs.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530353 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err.Error()})
354 return ""
355 }
356 logger.Infow(ctx, "Service Migrated", log.Fields{"Service": vs.Name, "PresentVersion": database.PresentVersionMap[database.ServicePath]})
357 return string(updatedData)
358}
359
vinokuma926cb3e2023-03-29 11:41:06 +0530360// MigrateDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530361func MigrateDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530362 logger.Error(ctx, "Data Migration not implemented for Devices")
363 return ""
364}
365
vinokuma926cb3e2023-03-29 11:41:06 +0530366// MigrateDevicePorts modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530367func MigrateDevicePorts(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530368 logger.Error(ctx, "Data Migration not implemented for Ports")
369 return ""
370}
371
vinokuma926cb3e2023-03-29 11:41:06 +0530372// MigrateDeviceFlows modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530373func MigrateDeviceFlows(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530374 logger.Error(ctx, "Data Migration not implemented for Flows")
375 return ""
376}
377
vinokuma926cb3e2023-03-29 11:41:06 +0530378// MigrateDeviceGroups modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530379func MigrateDeviceGroups(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530380 logger.Error(ctx, "Data Migration not implemented for Groups")
381 return ""
382}
383
vinokuma926cb3e2023-03-29 11:41:06 +0530384// MigrateDeviceMeters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530385func MigrateDeviceMeters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530386 logger.Error(ctx, "Data Migration not implemented for Meters")
387 return ""
388}
389
vinokuma926cb3e2023-03-29 11:41:06 +0530390// MigrateDeviceFlowHash modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530391func MigrateDeviceFlowHash(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530392 logger.Error(ctx, "Data Migration not implemented for FlowHash")
393 return ""
394}
395
vinokuma926cb3e2023-03-29 11:41:06 +0530396// MigrateVnets modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530397func MigrateVnets(cntx context.Context, data []byte) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530398 logger.Debug(ctx, "Migrate Vnets")
Naveen Sampath04696f72022-06-13 15:19:14 +0530399 var vnet VoltVnet
400 var updatedData []byte
401
402 err := json.Unmarshal(data, &vnet)
403 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530404 logger.Warnw(ctx, "Unmarshal of VNET failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530405 return ""
406 }
407
408 if vnet.SVlanTpid == 0 {
409 vnet.SVlanTpid = layers.EthernetTypeDot1Q
410 }
411 // MacLeanring parameter was not stored in vnets in 2.7 release.
412 if vnet.DhcpRelay || vnet.ArpLearning {
413 vnet.MacLearning = Learn
414 } else if !vnet.DhcpRelay && !vnet.ArpLearning {
415 vnet.MacLearning = MacLearningNone
416 }
417 vnet.PendingDeleteFlow = make(map[string]map[string]bool)
418 vnet.DeleteInProgress = false
vinokuma926cb3e2023-03-29 11:41:06 +0530419 if updatedData, err = vnet.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530420 logger.Warnw(ctx, "Marshal of Vnet failed", log.Fields{"Error": err.Error()})
421 return ""
422 }
423 logger.Infow(ctx, "Vnet Migrated", log.Fields{"Vnet Name": vnet.Name, "PresentVersion": database.PresentVersionMap[database.VnetPath]})
424 return string(updatedData)
425}
426
vinokuma926cb3e2023-03-29 11:41:06 +0530427// MigrateVpvs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530428func MigrateVpvs(cntx context.Context, data []byte) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530429 logger.Debug(ctx, "Migrate Vpvs")
Naveen Sampath04696f72022-06-13 15:19:14 +0530430 var vpv VoltPortVnet
431 var updatedData, updatedData1 []byte
432 var vpvmap map[string]interface{}
433 var err1 error
434 var usFlowsApplied, dsFlowsApplied bool
435
436 err := json.Unmarshal(data, &vpvmap)
437 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530438 logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530439 return ""
440 }
441 // changes to handle change in data type of MacLearning parameter
442 if updatedData1, err1 = json.Marshal(&vpvmap); err1 != nil {
443 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()})
444 return ""
445 }
446
447 if err2 := json.Unmarshal(updatedData1, &vpv); err != nil {
448 logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2})
Naveen Sampath04696f72022-06-13 15:19:14 +0530449 }
450
451 if vpvmap["MacLearning"] == true {
452 vpv.MacLearning = Learn
Naveen Sampath04696f72022-06-13 15:19:14 +0530453 }
454 if vpvmap["UsFlowsApplied"] == true {
455 usFlowsApplied = true
456 }
457
458 if vpvmap["DsFlowsApplied"] == true {
459 dsFlowsApplied = true
460 }
461
462 if usFlowsApplied && dsFlowsApplied {
463 vpv.FlowsApplied = true
464 }
vinokuma926cb3e2023-03-29 11:41:06 +0530465 // Migration
Naveen Sampath04696f72022-06-13 15:19:14 +0530466 if vpv.SVlanTpid == 0 {
467 vpv.SVlanTpid = layers.EthernetTypeDot1Q
468 }
469 vpv.VnetName = VnetKey(vpv.SVlan, vpv.CVlan, vpv.UniVlan)
470 vpv.PendingDeleteFlow = make(map[string]bool)
471 vpv.PonPort = 0xFF
472
vinokuma926cb3e2023-03-29 11:41:06 +0530473 if updatedData, err = vpv.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530474 logger.Warnw(ctx, "Marshal of VPV failed", log.Fields{"Error": err.Error()})
475 return ""
476 }
477 logger.Infow(ctx, "VPV Migrated", log.Fields{"Device": vpv.Device, "port": vpv.Port, "SVlan": vpv.SVlan,
vinokuma926cb3e2023-03-29 11:41:06 +0530478 "CVlan": vpv.CVlan, "UniVlan": vpv.UniVlan, "PresentVersion": database.PresentVersionMap[database.VpvPath]})
Naveen Sampath04696f72022-06-13 15:19:14 +0530479 return string(updatedData)
480}
481
vinokuma926cb3e2023-03-29 11:41:06 +0530482// MigrateMvlans modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530483func MigrateMvlans(cntx context.Context, data []byte) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530484 logger.Debug(ctx, "Migrate Mvlans")
Naveen Sampath04696f72022-06-13 15:19:14 +0530485 var mvp MvlanProfile
486 var updatedData []byte
487
488 err := json.Unmarshal(data, &mvp)
489 if err != nil {
490 logger.Warn(ctx, "Unmarshal of VPV failed")
491 return ""
492 }
493 // Mvlan Migration
494 mvp.IgmpServVersion = make(map[string]*uint8)
495 for srNo := range mvp.DevicesList {
496 var servVersion uint8
497 mvp.IgmpServVersion[srNo] = &servVersion
498 }
499
vinokuma926cb3e2023-03-29 11:41:06 +0530500 if updatedData, err = mvp.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530501 logger.Warnw(ctx, "Marshal of Mvlan Profile failed", log.Fields{"Error": err.Error()})
502 return ""
503 }
504 logger.Infow(ctx, "Mvlan Profile Migrated", log.Fields{"MvlanProfileName": mvp.Name, "PresentVersion": database.PresentVersionMap[database.MvlanPath]})
505 return string(updatedData)
506}
507
vinokuma926cb3e2023-03-29 11:41:06 +0530508// MigrateMeters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530509func MigrateMeters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530510 logger.Error(ctx, "Data Migration not implemented for Meters")
511 return ""
512}
513
vinokuma926cb3e2023-03-29 11:41:06 +0530514// MigrateIgmpConfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530515func MigrateIgmpConfs(cntx context.Context, data []byte) string {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530516 logger.Debug(ctx, "Migrate IgmpConfs")
Naveen Sampath04696f72022-06-13 15:19:14 +0530517 var igmpProfile IgmpProfile
518
519 err := json.Unmarshal(data, &igmpProfile)
520 if err != nil {
521 logger.Warn(ctx, "Unmarshal of IGMP failed")
522 return ""
523 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530524 if err := igmpProfile.WriteToDb(cntx); err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530525 logger.Warnw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProfile.ProfileID})
Naveen Sampath04696f72022-06-13 15:19:14 +0530526 }
527
528 logger.Infow(ctx, "Igmp Conf Migrated", log.Fields{"Profile": igmpProfile, "PresentVersion": database.PresentVersionMap[database.VpvPath]})
529 return ModuleToBeDeleted
530}
531
vinokuma926cb3e2023-03-29 11:41:06 +0530532// MigrateIgmpGroups modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530533func MigrateIgmpGroups(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530534 logger.Error(ctx, "Data Migration not implemented for IGMP Groups")
535 return ""
536}
537
vinokuma926cb3e2023-03-29 11:41:06 +0530538// MigrateIgmpDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530539func MigrateIgmpDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530540 logger.Error(ctx, "Data Migration not implemented for IGMP Device")
541 return ""
542}
543
vinokuma926cb3e2023-03-29 11:41:06 +0530544// MigrateIgmpChannels modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530545func MigrateIgmpChannels(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530546 logger.Error(ctx, "Data Migration not implemented for IGMP Channels")
547 return ""
548}
549
vinokuma926cb3e2023-03-29 11:41:06 +0530550// MigrateIgmpPorts modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530551func MigrateIgmpPorts(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530552 logger.Error(ctx, "Data Migration not implemented for IGMP Ports")
553 return ""
554}
555
vinokuma926cb3e2023-03-29 11:41:06 +0530556// MigrateIgmpProfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530557func MigrateIgmpProfs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530558 logger.Error(ctx, "Data Migration not implemented for IGMP Profs")
559 return ""
560}
561
vinokuma926cb3e2023-03-29 11:41:06 +0530562// MigrateMcastConfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530563func MigrateMcastConfs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530564 logger.Error(ctx, "Data Migration not implemented for Mcast Confs")
565 return ""
566}
567
vinokuma926cb3e2023-03-29 11:41:06 +0530568// MigrateLogLevels modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530569func MigrateLogLevels(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530570 logger.Error(ctx, "Data Migration not implemented for Log Levels")
571 return ""
572}
573
vinokuma926cb3e2023-03-29 11:41:06 +0530574// MigrateHealth modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530575func MigrateHealth(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530576 logger.Error(ctx, "Data Migration not implemented for Health")
577 return ""
578}
579
vinokuma926cb3e2023-03-29 11:41:06 +0530580// MigratePonCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530581func MigratePonCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530582 logger.Error(ctx, "Data Migration not implemented for Pon Counters")
583 return ""
584}
585
vinokuma926cb3e2023-03-29 11:41:06 +0530586// MigrateChannelCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530587func MigrateChannelCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530588 logger.Error(ctx, "Data Migration not implemented for Channel Counters")
589 return ""
590}
591
vinokuma926cb3e2023-03-29 11:41:06 +0530592// MigrateServiceCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530593func MigrateServiceCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530594 logger.Error(ctx, "Data Migration not implemented for Service Counters")
595 return ""
596}
597
vinokuma926cb3e2023-03-29 11:41:06 +0530598// MigrateNbDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530599func MigrateNbDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530600 logger.Error(ctx, "Data Migration not implemented for NB Devices")
601 return ""
602}
603
vinokuma926cb3e2023-03-29 11:41:06 +0530604// MigrateFlowHash modifyies the old data as per current version requirement and updates the database
Naveen Sampath04696f72022-06-13 15:19:14 +0530605func MigrateFlowHash(data []byte) string {
606 logger.Error(ctx, "Data Migration not implemented for FLow Hash")
607 return ""
608}
609
vinokuma926cb3e2023-03-29 11:41:06 +0530610// DeleteDbPathKeys Deleted the paths from DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530611func DeleteDbPathKeys(cntx context.Context, keyPath string) error {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530612 logger.Debugw(ctx, "Deleting paths for version", log.Fields{"Path": keyPath})
Naveen Sampath04696f72022-06-13 15:19:14 +0530613
614 // Delete all the keys
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530615 err := db.DeleteAll(cntx, keyPath)
Naveen Sampath04696f72022-06-13 15:19:14 +0530616 if err != nil && err.Error() != common.ErrEntryNotFound.Error() {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530617 logger.Errorw(ctx, "Delete Key failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530618 return err
619 }
620 return nil
621}