blob: 4d6b3d65f4050aadd8ecfa68909779ccffc85c75 [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 (
Tinoj Joseph07cc5372022-07-18 22:53:51 +053019 "context"
vinokuma926cb3e2023-03-29 11:41:06 +053020 "errors"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 "net"
Naveen Sampath04696f72022-06-13 15:19:14 +053022
23 "strings"
24
Naveen Sampath04696f72022-06-13 15:19:14 +053025 "voltha-go-controller/database"
vinokuma926cb3e2023-03-29 11:41:06 +053026 common "voltha-go-controller/internal/pkg/types"
Tinoj Joseph1d108322022-07-13 10:07:39 +053027 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053028
29 "github.com/google/gopacket/layers"
Naveen Sampath04696f72022-06-13 15:19:14 +053030)
31
Tinoj Joseph07cc5372022-07-18 22:53:51 +053032type paramsUpdationFunc func(cntx context.Context, hash string, value interface{}) error
Naveen Sampath04696f72022-06-13 15:19:14 +053033
vinokuma926cb3e2023-03-29 11:41:06 +053034// map to store conversion functions
Naveen Sampath04696f72022-06-13 15:19:14 +053035var updationMap = map[string]paramsUpdationFunc{
36 database.VnetPath: updateVnets,
37 database.VpvPath: updateVpvs,
38 database.ServicePath: updateServices,
39 database.MvlanPath: updateMvlans,
40 database.IgmpGroupPath: updateIgmpGroups,
41 database.IgmpDevicePath: updateIgmpDevices,
42 database.IgmpProfPath: updateIgmpProfiles,
43}
44
45// UpdateDbData to update database data
Tinoj Joseph07cc5372022-07-18 22:53:51 +053046func UpdateDbData(cntx context.Context, dbPath, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053047 if migrationFunc, ok := updationMap[dbPath]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053048 err := migrationFunc(cntx, hash, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053049 if err != nil {
50 logger.Error(ctx, "Error in migrating data\n")
51 return errors.New("Error-in-migration")
52 }
53 }
54 return nil
55}
56
vinokuma926cb3e2023-03-29 11:41:06 +053057// This function modifyies the old data as per current version requirement and also
58// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053059func updateServices(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053060 param := value.(*VoltService)
61 param.VnetID = VnetKey(param.SVlan, param.CVlan, param.UniVlan)
62 return nil
63}
64
vinokuma926cb3e2023-03-29 11:41:06 +053065// This function modifyies the old data as per current version requirement and also
66// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053067func updateVnets(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053068 param := value.(*VoltVnet)
69 newKey := VnetKey(param.SVlan, param.CVlan, param.UniVlan)
70 if newKey != hash {
vinokuma926cb3e2023-03-29 11:41:06 +053071 // Delete the older key
Tinoj Joseph07cc5372022-07-18 22:53:51 +053072 _ = db.DelVnet(cntx, hash)
Naveen Sampath04696f72022-06-13 15:19:14 +053073 } else {
vinokuma926cb3e2023-03-29 11:41:06 +053074 // Update SVlan Tag Protocol id param with default valud if not present
Naveen Sampath04696f72022-06-13 15:19:14 +053075 if param.SVlanTpid == 0 {
76 param.SVlanTpid = layers.EthernetTypeDot1Q
77 }
78 }
79 param.Name = newKey
80 if param.DevicesList == nil || len(param.DevicesList) == 0 {
vinokuma926cb3e2023-03-29 11:41:06 +053081 param.DevicesList = append(param.DevicesList, "") // Empty OLT serial number as of now since submgr won't have proper serial num
Naveen Sampath04696f72022-06-13 15:19:14 +053082 }
83 return nil
84}
85
vinokuma926cb3e2023-03-29 11:41:06 +053086// This function modifyies the old data as per current version requirement and also
87// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053088func updateVpvs(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053089 //var param VoltPortVnet
90 param := value.(*VoltPortVnet)
91
vinokuma926cb3e2023-03-29 11:41:06 +053092 // Update SVlan Tag Protocol id param with default valud if not present
Naveen Sampath04696f72022-06-13 15:19:14 +053093 if param.SVlanTpid == 0 {
94 param.SVlanTpid = layers.EthernetTypeDot1Q
95 }
96
97 if strings.Count(hash, "-") > 1 {
98 logger.Info(ctx, "Already upgraded")
99 return nil
100 }
101
vinokuma926cb3e2023-03-29 11:41:06 +0530102 // Add the vpv under new path
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530103 param.WriteToDb(cntx)
vinokuma926cb3e2023-03-29 11:41:06 +0530104 // delete the older path
Naveen Sampath04696f72022-06-13 15:19:14 +0530105 fullPath := database.BasePath + database.VpvPath + hash
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530106 if err := db.Del(cntx, fullPath); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530107 logger.Errorw(ctx, "Vpv Delete from DB failed", log.Fields{"Error": err, "key": fullPath})
108 }
109 return nil
110}
111
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530112func updateMvlans(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530113 param := value.(*MvlanProfile)
114 if param.DevicesList == nil || len(param.DevicesList) == 0 {
vinokuma926cb3e2023-03-29 11:41:06 +0530115 param.DevicesList = make(map[string]OperInProgress) // Empty OLT serial number as of now since submgr won't have proper serial num
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530116 if err := param.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530117 logger.Errorw(ctx, "Mvlan profile write to DB failed", log.Fields{"ProfileName": param.Name})
118 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530119 }
120 if _, ok := param.Groups[common.StaticGroup]; ok {
121 param.Groups[common.StaticGroup].IsStatic = true
122 }
123 return nil
124}
125
vinokuma926cb3e2023-03-29 11:41:06 +0530126// This function modifyies the old Igmp Group data as per current version requirement and also
127// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530128func updateIgmpGroups(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530129 ig := value.(*IgmpGroup)
130 logger.Infow(ctx, "Group Data Migration", log.Fields{"ig": ig, "GroupAddr": ig.GroupAddr, "hash": hash})
131 if ig.GroupAddr == nil {
132 ig.GroupAddr = net.ParseIP("0.0.0.0")
133 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530134 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530135 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
136 }
137
138 return nil
139}
140
vinokuma926cb3e2023-03-29 11:41:06 +0530141// This function modifyies the old Igmp Device data as per current version requirement and also
142// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530143func updateIgmpDevices(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530144 igd := value.(*IgmpGroupDevice)
145 logger.Infow(ctx, "Group Device Migration", log.Fields{"igd": igd, "GroupAddr": igd.GroupAddr, "hash": hash})
146 if igd.GroupAddr == nil {
147 igd.GroupAddr = net.ParseIP("0.0.0.0")
148 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530149 if err := igd.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530150 logger.Errorw(ctx, "Igmp group device Write to DB failed", log.Fields{"Device": igd.Device,
vinokuma926cb3e2023-03-29 11:41:06 +0530151 "GroupName": igd.GroupName, "GroupAddr": igd.GroupAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +0530152 }
153
154 return nil
155}
156
vinokuma926cb3e2023-03-29 11:41:06 +0530157// This function modifyies the old Igmp Profile data as per current version requirement and also
158// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530159func updateIgmpProfiles(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530160 igmpProfile := value.(*IgmpProfile)
161 logger.Infow(ctx, "IGMP Profile Migration", log.Fields{"igmpProfile": igmpProfile, "hash": hash})
162 return nil
163}
164
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530165func (ig *IgmpGroup) migrateIgmpDevices(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530166 devices, _ := db.GetPrevIgmpDevices(cntx, ig.Mvlan, ig.GroupName)
Naveen Sampath04696f72022-06-13 15:19:14 +0530167 logger.Infow(ctx, "Migratable Devices", log.Fields{"Devices": devices})
168 for _, device := range devices {
169 b, ok := device.Value.([]byte)
170 if !ok {
171 logger.Warn(ctx, "The value type is not []byte")
172 continue
173 }
174 if igd, err := NewIgmpGroupDeviceFromBytes(b); err == nil {
175 key := database.BasePath + database.IgmpDevicePath + igd.Mvlan.String() + "/" + igd.GroupName + "/" + igd.Device
176 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igd": igd})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530177 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530178 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
179 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530180 if err := UpdateDbData(cntx, database.IgmpDevicePath, key, igd); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 logger.Warnw(ctx, "Group Device Migration failed", log.Fields{"IGD": igd, "Error": err})
182 } else {
183 logger.Infow(ctx, "Group Device Migrated", log.Fields{"IGD": igd})
184 }
185 } else {
186 logger.Warnw(ctx, "Unable to decode device from database", log.Fields{"str": string(b)})
187 }
188 }
189}
190
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530191func (igd *IgmpGroupDevice) migrateIgmpChannels(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530192 channels, _ := db.GetPrevIgmpChannels(cntx, igd.GroupName, igd.Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530193 logger.Infow(ctx, "Migratable Channels", log.Fields{"Channels": channels})
194 for _, channel := range channels {
Naveen Sampath04696f72022-06-13 15:19:14 +0530195 b, ok := channel.Value.([]byte)
196 if !ok {
197 logger.Warn(ctx, "The value type is not []byte")
198 continue
199 }
200 if igc, err := NewIgmpGroupChannelFromBytes(b); err == nil {
201 key := database.BasePath + database.IgmpChannelPath + igc.GroupName + "/" + igc.Device + "/" + igc.GroupAddr.String()
202 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igc": igc})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530203 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530204 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
205 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530206 if err := igc.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530207 logger.Errorw(ctx, "Igmp group channel Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
208 }
209
210 logger.Infow(ctx, "Group Channel Migrated", log.Fields{"IGD": igc})
211 } else {
212 logger.Warnw(ctx, "Unable to decode channel from database", log.Fields{"str": string(b)})
213 }
214 }
215}
216
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530217func (igc *IgmpGroupChannel) migrateIgmpPorts(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530218 ports, _ := db.GetPrevIgmpRcvrs(cntx, igc.GroupAddr, igc.Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530219 logger.Infow(ctx, "Migratable Ports", log.Fields{"Ports": ports})
220 for _, port := range ports {
Naveen Sampath04696f72022-06-13 15:19:14 +0530221 b, ok := port.Value.([]byte)
222 if !ok {
223 logger.Warn(ctx, "The value type is not []byte")
224 continue
225 }
226 if igp, err := NewIgmpGroupPortFromBytes(b); err == nil {
227 key := database.BasePath + database.IgmpPortPath + igc.GroupAddr.String() + "/" + igc.Device + "/" + igp.Port
228 logger.Infow(ctx, "Deleting old entry", log.Fields{"Key": key, "Igp": igp})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530229 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530230 logger.Errorw(ctx, "Igmp Group port Delete from DB failed", log.Fields{"Error": err, "key": key})
231 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530232 if err := igp.WriteToDb(cntx, igc.Mvlan, igc.GroupAddr, igc.Device); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530233 logger.Errorw(ctx, "Igmp group port Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
234 }
235
236 logger.Infow(ctx, "Group Port Migrated", log.Fields{"IGD": igp})
237 } else {
238 logger.Warnw(ctx, "Unable to decode port from database", log.Fields{"str": string(b)})
239 }
240 }
241}