blob: 2cd488ce355d15094d4bb8153b26113f1225ecc2 [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"
Naveen Sampath04696f72022-06-13 15:19:14 +053022 "sync"
vinokuma926cb3e2023-03-29 11:41:06 +053023 common "voltha-go-controller/internal/pkg/types"
Naveen Sampath04696f72022-06-13 15:19:14 +053024
25 "github.com/google/gopacket/layers"
26
27 "voltha-go-controller/database"
Tinoj Joseph1d108322022-07-13 10:07:39 +053028 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053029)
30
31const (
vinokuma926cb3e2023-03-29 11:41:06 +053032 // MigrationComplete Represents the Migration Complete
Naveen Sampath04696f72022-06-13 15:19:14 +053033 MigrationComplete = "Completed"
vinokuma926cb3e2023-03-29 11:41:06 +053034 // MigrationInProgress Represents the Migration Inprogress
Naveen Sampath04696f72022-06-13 15:19:14 +053035 MigrationInProgress = "InProgress"
vinokuma926cb3e2023-03-29 11:41:06 +053036 // MigrationFailed Represents the Migration Failed
Naveen Sampath04696f72022-06-13 15:19:14 +053037 MigrationFailed = "Failed"
38 // StatusNone for no operations
39 StatusNone = "NONE"
vinokuma926cb3e2023-03-29 11:41:06 +053040 // ModuleToBeDeleted - module where old version is deleted
Naveen Sampath04696f72022-06-13 15:19:14 +053041 ModuleToBeDeleted = "ModuleToBeDeleted"
42)
43
vinokuma926cb3e2023-03-29 11:41:06 +053044// DataMigration represents the Version and Status info for Major Version Upgrade.
Naveen Sampath04696f72022-06-13 15:19:14 +053045type DataMigration struct {
vinokuma926cb3e2023-03-29 11:41:06 +053046 ModuleVer map[string]string // eg. "service": "v1"
Naveen Sampath04696f72022-06-13 15:19:14 +053047 Version string
48 Status string
Naveen Sampath04696f72022-06-13 15:19:14 +053049}
50
Tinoj Joseph07cc5372022-07-18 22:53:51 +053051type paramsMigrationFunc func(context.Context, []byte) string
Naveen Sampath04696f72022-06-13 15:19:14 +053052
vinokuma926cb3e2023-03-29 11:41:06 +053053// map to store conversion functions
Naveen Sampath04696f72022-06-13 15:19:14 +053054var 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 Joseph07cc5372022-07-18 22:53:51 +053082func (md *DataMigration) WriteToDb(cntx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053083 b, err := json.Marshal(md)
84 if err != nil {
85 return err
86 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +053087 if err1 := db.PutMigrationInfo(cntx, string(b)); err1 != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053088 return err1
89 }
90 return nil
91}
92
93// DelFromDb delete a meter profile from DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +053094func (md *DataMigration) DelFromDb(cntx context.Context) {
95 if err := db.DelMigrationInfo(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053096 logger.Warnw(ctx, "DelMigrationInfo Failed", log.Fields{"Error": err})
97 }
98}
99
100// GetMigrationInfo to get data migration info
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530101func GetMigrationInfo(cntx context.Context, dmInfo *DataMigration) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530102 var migrationInfo string
103 var err error
104 if db == nil {
105 db = database.GetDatabase()
106 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530107 if migrationInfo, err = db.GetMigrationInfo(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530108 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
122func CheckIfMigrationRequired(ctx context.Context) bool {
123 Migrate := new(DataMigration)
124 var NoDataInDB bool
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530125 err := GetMigrationInfo(ctx, Migrate)
Tinoj Joseph1d108322022-07-13 10:07:39 +0530126 logger.Debugw(ctx, "Migration data", log.Fields{"DataMigration": Migrate})
vinokuma926cb3e2023-03-29 11:41:06 +0530127 // No DB entry represents N version Bring Up for the First time
Naveen Sampath04696f72022-06-13 15:19:14 +0530128 if err != nil {
129 NoDataInDB = true
130 logger.Error(ctx, "Failed to read the Migration Data from DB ")
131 }
vinokuma926cb3e2023-03-29 11:41:06 +0530132 // Covers N version bringup and Reboot Scenarios
Naveen Sampath04696f72022-06-13 15:19:14 +0530133 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 Joseph07cc5372022-07-18 22:53:51 +0530138 if err := Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530139 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530140 }
vinokuma926cb3e2023-03-29 11:41:06 +0530141 // MigrateProbestatus has to be Updated to Complete when No Migration is Required
Tinoj Joseph1d108322022-07-13 10:07:39 +0530142 logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status})
vinokuma926cb3e2023-03-29 11:41:06 +0530143 // probe.UpdateDBMigrationStatus(ctx, true)
Naveen Sampath04696f72022-06-13 15:19:14 +0530144 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 Joseph1d108322022-07-13 10:07:39 +0530176 logger.Debugw(ctx, "Migration Probe Status", log.Fields{"Migration Probe": Migrate.Status})
Naveen Sampath04696f72022-06-13 15:19:14 +0530177 //probe.UpdateDBMigrationStatus(ctx, true)
178 return false
179}
180
181// InitiateDataMigration Migrates the DB data
182// depending on the bool value returned by CheckIfMigrationDone
183func InitiateDataMigration(ctx context.Context) {
184 var err error
185 Migrate := new(DataMigration)
186 var migrationWG sync.WaitGroup
187
vinokuma926cb3e2023-03-29 11:41:06 +0530188 // Keeping it outside to avoid race condition where the
Naveen Sampath04696f72022-06-13 15:19:14 +0530189 // 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 Joseph07cc5372022-07-18 22:53:51 +0530194 err = MigrateDBData(ctx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530195 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530196 logger.Errorw(ctx, "Failed to Migrate the Data", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530197 Migrate.Status = MigrationFailed
vinokuma926cb3e2023-03-29 11:41:06 +0530198 if err = Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530199 logger.Errorw(ctx, "DB Write failed to Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530200 }
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 Joseph07cc5372022-07-18 22:53:51 +0530208 if err = Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530209 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530210 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 {
vinokuma926cb3e2023-03-29 11:41:06 +0530216 logger.Errorw(ctx, "Migration failure due to Exception happened", log.Fields{"reason": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530217 Migrate.Status = MigrationFailed
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530218 if err := Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530219 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530220 }
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 Joseph07cc5372022-07-18 22:53:51 +0530229 if err := Migrate.WriteToDb(ctx); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530230 logger.Errorw(ctx, "DB Write failed for Migration Path", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530231 }
Tinoj Joseph1d108322022-07-13 10:07:39 +0530232 logger.Infow(ctx, "Migration completed successfully", log.Fields{"Status": Migrate.Status})
Naveen Sampath04696f72022-06-13 15:19:14 +0530233}
234
235// MigrateDBData to migrate database data
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530236func MigrateDBData(cntx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530237 var err error
238 for module, currentVersion := range database.PresentVersionMap {
239 if currentVersion == database.DBVersionMap[module] {
240 logger.Infow(ctx, "No Data Migration required for module", log.Fields{"Table": module, "Version": currentVersion})
241 continue
242 }
243
244 if _, ok := migrationMap[module]; ok {
245 switch module {
246 case database.DeviceFlowPath,
247 database.DevicePortPath,
248 database.DeviceMeterPath,
249 database.DeviceGroupPath,
250 database.DeviceFlowHashPath:
251 err = FetchAndMigrateDeviceDBData(module)
252 default:
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530253 err = FetchAndMigrateDBData(cntx, module)
Naveen Sampath04696f72022-06-13 15:19:14 +0530254 }
255 } else {
256 logger.Infow(ctx, "No Data Migration handling found for module", log.Fields{"Table": module, "Version": currentVersion})
257 }
258
259 if err != nil {
260 logger.Errorw(ctx, "Error in data migration", log.Fields{"Module": module})
261 return err
262 }
263 }
264 return nil
265}
266
vinokuma926cb3e2023-03-29 11:41:06 +0530267// 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 +0530268func FetchAndMigrateDeviceDBData(module string) error {
269 logger.Error(ctx, "Data Migration not implemented for Device DB Data")
270 return nil
271}
272
vinokuma926cb3e2023-03-29 11:41:06 +0530273// 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 +0530274func FetchAndMigrateDBData(cntx context.Context, module string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530275 previousPath := database.GetModuleKeypath(module, database.PreviousVersionMap[module])
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530276 dbPathKeysValueMap, err := db.List(cntx, previousPath)
Naveen Sampath04696f72022-06-13 15:19:14 +0530277 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530278 logger.Errorw(ctx, "failed to Fetch the Keys from Redis", log.Fields{"error": err})
vinokuma926cb3e2023-03-29 11:41:06 +0530279 // No return required, Data might not be present in DB
Naveen Sampath04696f72022-06-13 15:19:14 +0530280 return nil
281 }
282 if len(dbPathKeysValueMap) == 0 {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530283 logger.Debugw(ctx, "No data present in DB for the path", log.Fields{"dbPath": module})
Naveen Sampath04696f72022-06-13 15:19:14 +0530284 return nil
285 }
286
287 // Fetch each Path from previous version and store to present version after data migration changes
288 for hash, value := range dbPathKeysValueMap {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530289 logger.Debugw(ctx, "DB path", log.Fields{"hash": hash})
vinokuma926cb3e2023-03-29 11:41:06 +0530290 // convert the value to a specific type based on the dbPath
Naveen Sampath04696f72022-06-13 15:19:14 +0530291 b, ok := value.Value.([]byte)
292 if !ok {
293 logger.Error(ctx, "The value type is not []byte")
294 return errors.New("Error-in-migration")
295 }
296
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530297 presentParams := migrationMap[module](cntx, b)
Naveen Sampath04696f72022-06-13 15:19:14 +0530298 logger.Infow(ctx, "Migrated data", log.Fields{"presentParams": presentParams})
299 if "" == presentParams {
300 logger.Error(ctx, "Error in migrating data\n")
301 return errors.New("Error-in-migration")
302 } else if ModuleToBeDeleted == presentParams {
303 return nil
304 }
305 presentPath := database.GetKeyPath(module) + hash
306 logger.Infow(ctx, "Before writing to DB", log.Fields{"presentParams": presentParams})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530307 if err := db.Put(cntx, presentPath, presentParams); err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530308 logger.Errorw(ctx, "Update Params failed", log.Fields{"key": presentPath, "presentparams": presentParams})
Naveen Sampath04696f72022-06-13 15:19:14 +0530309 return err
310 }
311 }
312 return nil
313}
314
vinokuma926cb3e2023-03-29 11:41:06 +0530315// MigrateServices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530316func MigrateServices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530317 var vs VoltService
318 var updatedData, updatedData1 []byte
319 var vsmap map[string]interface{}
320 var err1 error
321
322 err := json.Unmarshal(data, &vsmap)
323 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530324 logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530325 return ""
326 }
327 // changes to handle change in data type of MacLearning parameter
328 if updatedData1, err1 = json.Marshal(&vsmap); err1 != nil {
329 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()})
330 return ""
331 }
332
333 if err2 := json.Unmarshal(updatedData1, &vs); err != nil {
334 logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2})
335 return ""
336 }
337
338 if vsmap["MacLearning"] == true {
339 vs.MacLearning = Learn
Naveen Sampath04696f72022-06-13 15:19:14 +0530340 }
341
vinokuma926cb3e2023-03-29 11:41:06 +0530342 // Migration
Naveen Sampath04696f72022-06-13 15:19:14 +0530343 vs.PendingFlows = make(map[string]bool)
344 vs.AssociatedFlows = make(map[string]bool)
345 vs.DeleteInProgress = false
346 vs.PonPort = 0xFF
vinokuma926cb3e2023-03-29 11:41:06 +0530347 if updatedData, err = vs.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530348 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err.Error()})
349 return ""
350 }
351 logger.Infow(ctx, "Service Migrated", log.Fields{"Service": vs.Name, "PresentVersion": database.PresentVersionMap[database.ServicePath]})
352 return string(updatedData)
353}
354
vinokuma926cb3e2023-03-29 11:41:06 +0530355// MigrateDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530356func MigrateDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530357 logger.Error(ctx, "Data Migration not implemented for Devices")
358 return ""
359}
360
vinokuma926cb3e2023-03-29 11:41:06 +0530361// MigrateDevicePorts modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530362func MigrateDevicePorts(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530363 logger.Error(ctx, "Data Migration not implemented for Ports")
364 return ""
365}
366
vinokuma926cb3e2023-03-29 11:41:06 +0530367// MigrateDeviceFlows modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530368func MigrateDeviceFlows(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530369 logger.Error(ctx, "Data Migration not implemented for Flows")
370 return ""
371}
372
vinokuma926cb3e2023-03-29 11:41:06 +0530373// MigrateDeviceGroups modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530374func MigrateDeviceGroups(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530375 logger.Error(ctx, "Data Migration not implemented for Groups")
376 return ""
377}
378
vinokuma926cb3e2023-03-29 11:41:06 +0530379// MigrateDeviceMeters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530380func MigrateDeviceMeters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530381 logger.Error(ctx, "Data Migration not implemented for Meters")
382 return ""
383}
384
vinokuma926cb3e2023-03-29 11:41:06 +0530385// MigrateDeviceFlowHash modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530386func MigrateDeviceFlowHash(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530387 logger.Error(ctx, "Data Migration not implemented for FlowHash")
388 return ""
389}
390
vinokuma926cb3e2023-03-29 11:41:06 +0530391// MigrateVnets modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530392func MigrateVnets(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530393 var vnet VoltVnet
394 var updatedData []byte
395
396 err := json.Unmarshal(data, &vnet)
397 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530398 logger.Warnw(ctx, "Unmarshal of VNET failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530399 return ""
400 }
401
402 if vnet.SVlanTpid == 0 {
403 vnet.SVlanTpid = layers.EthernetTypeDot1Q
404 }
405 // MacLeanring parameter was not stored in vnets in 2.7 release.
406 if vnet.DhcpRelay || vnet.ArpLearning {
407 vnet.MacLearning = Learn
408 } else if !vnet.DhcpRelay && !vnet.ArpLearning {
409 vnet.MacLearning = MacLearningNone
410 }
411 vnet.PendingDeleteFlow = make(map[string]map[string]bool)
412 vnet.DeleteInProgress = false
vinokuma926cb3e2023-03-29 11:41:06 +0530413 if updatedData, err = vnet.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530414 logger.Warnw(ctx, "Marshal of Vnet failed", log.Fields{"Error": err.Error()})
415 return ""
416 }
417 logger.Infow(ctx, "Vnet Migrated", log.Fields{"Vnet Name": vnet.Name, "PresentVersion": database.PresentVersionMap[database.VnetPath]})
418 return string(updatedData)
419}
420
vinokuma926cb3e2023-03-29 11:41:06 +0530421// MigrateVpvs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530422func MigrateVpvs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530423 var vpv VoltPortVnet
424 var updatedData, updatedData1 []byte
425 var vpvmap map[string]interface{}
426 var err1 error
427 var usFlowsApplied, dsFlowsApplied bool
428
429 err := json.Unmarshal(data, &vpvmap)
430 if err != nil {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530431 logger.Warnw(ctx, "Unmarshal of VPV failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530432 return ""
433 }
434 // changes to handle change in data type of MacLearning parameter
435 if updatedData1, err1 = json.Marshal(&vpvmap); err1 != nil {
436 logger.Warnw(ctx, "Marshal of Service failed", log.Fields{"Error": err1.Error()})
437 return ""
438 }
439
440 if err2 := json.Unmarshal(updatedData1, &vpv); err != nil {
441 logger.Warnw(ctx, "Unmarshal-failed", log.Fields{"err": err2})
Naveen Sampath04696f72022-06-13 15:19:14 +0530442 }
443
444 if vpvmap["MacLearning"] == true {
445 vpv.MacLearning = Learn
Naveen Sampath04696f72022-06-13 15:19:14 +0530446 }
447 if vpvmap["UsFlowsApplied"] == true {
448 usFlowsApplied = true
449 }
450
451 if vpvmap["DsFlowsApplied"] == true {
452 dsFlowsApplied = true
453 }
454
455 if usFlowsApplied && dsFlowsApplied {
456 vpv.FlowsApplied = true
457 }
vinokuma926cb3e2023-03-29 11:41:06 +0530458 // Migration
Naveen Sampath04696f72022-06-13 15:19:14 +0530459 if vpv.SVlanTpid == 0 {
460 vpv.SVlanTpid = layers.EthernetTypeDot1Q
461 }
462 vpv.VnetName = VnetKey(vpv.SVlan, vpv.CVlan, vpv.UniVlan)
463 vpv.PendingDeleteFlow = make(map[string]bool)
464 vpv.PonPort = 0xFF
465
vinokuma926cb3e2023-03-29 11:41:06 +0530466 if updatedData, err = vpv.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530467 logger.Warnw(ctx, "Marshal of VPV failed", log.Fields{"Error": err.Error()})
468 return ""
469 }
470 logger.Infow(ctx, "VPV Migrated", log.Fields{"Device": vpv.Device, "port": vpv.Port, "SVlan": vpv.SVlan,
vinokuma926cb3e2023-03-29 11:41:06 +0530471 "CVlan": vpv.CVlan, "UniVlan": vpv.UniVlan, "PresentVersion": database.PresentVersionMap[database.VpvPath]})
Naveen Sampath04696f72022-06-13 15:19:14 +0530472 return string(updatedData)
473}
474
vinokuma926cb3e2023-03-29 11:41:06 +0530475// MigrateMvlans modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530476func MigrateMvlans(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530477 var mvp MvlanProfile
478 var updatedData []byte
479
480 err := json.Unmarshal(data, &mvp)
481 if err != nil {
482 logger.Warn(ctx, "Unmarshal of VPV failed")
483 return ""
484 }
485 // Mvlan Migration
486 mvp.IgmpServVersion = make(map[string]*uint8)
487 for srNo := range mvp.DevicesList {
488 var servVersion uint8
489 mvp.IgmpServVersion[srNo] = &servVersion
490 }
491
vinokuma926cb3e2023-03-29 11:41:06 +0530492 if updatedData, err = mvp.JSONMarshal(); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530493 logger.Warnw(ctx, "Marshal of Mvlan Profile failed", log.Fields{"Error": err.Error()})
494 return ""
495 }
496 logger.Infow(ctx, "Mvlan Profile Migrated", log.Fields{"MvlanProfileName": mvp.Name, "PresentVersion": database.PresentVersionMap[database.MvlanPath]})
497 return string(updatedData)
498}
499
vinokuma926cb3e2023-03-29 11:41:06 +0530500// MigrateMeters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530501func MigrateMeters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530502 logger.Error(ctx, "Data Migration not implemented for Meters")
503 return ""
504}
505
vinokuma926cb3e2023-03-29 11:41:06 +0530506// MigrateIgmpConfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530507func MigrateIgmpConfs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530508 var igmpProfile IgmpProfile
509
510 err := json.Unmarshal(data, &igmpProfile)
511 if err != nil {
512 logger.Warn(ctx, "Unmarshal of IGMP failed")
513 return ""
514 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530515 if err := igmpProfile.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530516 logger.Errorw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProfile.ProfileID})
517 }
518
519 logger.Infow(ctx, "Igmp Conf Migrated", log.Fields{"Profile": igmpProfile, "PresentVersion": database.PresentVersionMap[database.VpvPath]})
520 return ModuleToBeDeleted
521}
522
vinokuma926cb3e2023-03-29 11:41:06 +0530523// MigrateIgmpGroups modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530524func MigrateIgmpGroups(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530525 logger.Error(ctx, "Data Migration not implemented for IGMP Groups")
526 return ""
527}
528
vinokuma926cb3e2023-03-29 11:41:06 +0530529// MigrateIgmpDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530530func MigrateIgmpDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530531 logger.Error(ctx, "Data Migration not implemented for IGMP Device")
532 return ""
533}
534
vinokuma926cb3e2023-03-29 11:41:06 +0530535// MigrateIgmpChannels modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530536func MigrateIgmpChannels(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530537 logger.Error(ctx, "Data Migration not implemented for IGMP Channels")
538 return ""
539}
540
vinokuma926cb3e2023-03-29 11:41:06 +0530541// MigrateIgmpPorts modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530542func MigrateIgmpPorts(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530543 logger.Error(ctx, "Data Migration not implemented for IGMP Ports")
544 return ""
545}
546
vinokuma926cb3e2023-03-29 11:41:06 +0530547// MigrateIgmpProfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530548func MigrateIgmpProfs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530549 logger.Error(ctx, "Data Migration not implemented for IGMP Profs")
550 return ""
551}
552
vinokuma926cb3e2023-03-29 11:41:06 +0530553// MigrateMcastConfs modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530554func MigrateMcastConfs(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530555 logger.Error(ctx, "Data Migration not implemented for Mcast Confs")
556 return ""
557}
558
vinokuma926cb3e2023-03-29 11:41:06 +0530559// MigrateLogLevels modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530560func MigrateLogLevels(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530561 logger.Error(ctx, "Data Migration not implemented for Log Levels")
562 return ""
563}
564
vinokuma926cb3e2023-03-29 11:41:06 +0530565// MigrateHealth modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530566func MigrateHealth(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530567 logger.Error(ctx, "Data Migration not implemented for Health")
568 return ""
569}
570
vinokuma926cb3e2023-03-29 11:41:06 +0530571// MigratePonCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530572func MigratePonCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530573 logger.Error(ctx, "Data Migration not implemented for Pon Counters")
574 return ""
575}
576
vinokuma926cb3e2023-03-29 11:41:06 +0530577// MigrateChannelCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530578func MigrateChannelCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530579 logger.Error(ctx, "Data Migration not implemented for Channel Counters")
580 return ""
581}
582
vinokuma926cb3e2023-03-29 11:41:06 +0530583// MigrateServiceCounters modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530584func MigrateServiceCounters(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530585 logger.Error(ctx, "Data Migration not implemented for Service Counters")
586 return ""
587}
588
vinokuma926cb3e2023-03-29 11:41:06 +0530589// MigrateNbDevices modifyies the old data as per current version requirement and updates the database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530590func MigrateNbDevices(cntx context.Context, data []byte) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530591 logger.Error(ctx, "Data Migration not implemented for NB Devices")
592 return ""
593}
594
vinokuma926cb3e2023-03-29 11:41:06 +0530595// MigrateFlowHash modifyies the old data as per current version requirement and updates the database
Naveen Sampath04696f72022-06-13 15:19:14 +0530596func MigrateFlowHash(data []byte) string {
597 logger.Error(ctx, "Data Migration not implemented for FLow Hash")
598 return ""
599}
600
vinokuma926cb3e2023-03-29 11:41:06 +0530601// DeleteDbPathKeys Deleted the paths from DB
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530602func DeleteDbPathKeys(cntx context.Context, keyPath string) error {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530603 logger.Debugw(ctx, "Deleting paths for version", log.Fields{"Path": keyPath})
Naveen Sampath04696f72022-06-13 15:19:14 +0530604
605 // Delete all the keys
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530606 err := db.DeleteAll(cntx, keyPath)
Naveen Sampath04696f72022-06-13 15:19:14 +0530607 if err != nil && err.Error() != common.ErrEntryNotFound.Error() {
Tinoj Joseph1d108322022-07-13 10:07:39 +0530608 logger.Errorw(ctx, "Delete Key failed", log.Fields{"error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530609 return err
610 }
611 return nil
612}