blob: 096d7b558f803a97fde9ba3eeb016c24a6cfc897 [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 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053047 logger.Debugw(ctx, "Update Db Data", log.Fields{"DbPath": dbPath, "Hash": hash})
Naveen Sampath04696f72022-06-13 15:19:14 +053048 if migrationFunc, ok := updationMap[dbPath]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053049 err := migrationFunc(cntx, hash, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053050 if err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053051 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 {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053060 logger.Debugw(ctx, "Update Services", log.Fields{"Hash": hash})
Naveen Sampath04696f72022-06-13 15:19:14 +053061 param := value.(*VoltService)
62 param.VnetID = VnetKey(param.SVlan, param.CVlan, param.UniVlan)
63 return nil
64}
65
vinokuma926cb3e2023-03-29 11:41:06 +053066// This function modifyies the old data as per current version requirement and also
67// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053068func updateVnets(cntx context.Context, hash string, value interface{}) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053069 logger.Debugw(ctx, "Update Vnets", log.Fields{"Hash": hash})
Naveen Sampath04696f72022-06-13 15:19:14 +053070 param := value.(*VoltVnet)
71 newKey := VnetKey(param.SVlan, param.CVlan, param.UniVlan)
72 if newKey != hash {
vinokuma926cb3e2023-03-29 11:41:06 +053073 // Delete the older key
Tinoj Joseph07cc5372022-07-18 22:53:51 +053074 _ = db.DelVnet(cntx, hash)
Naveen Sampath04696f72022-06-13 15:19:14 +053075 } else {
vinokuma926cb3e2023-03-29 11:41:06 +053076 // Update SVlan Tag Protocol id param with default valud if not present
Naveen Sampath04696f72022-06-13 15:19:14 +053077 if param.SVlanTpid == 0 {
78 param.SVlanTpid = layers.EthernetTypeDot1Q
79 }
80 }
81 param.Name = newKey
82 if param.DevicesList == nil || len(param.DevicesList) == 0 {
vinokuma926cb3e2023-03-29 11:41:06 +053083 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 +053084 }
85 return nil
86}
87
vinokuma926cb3e2023-03-29 11:41:06 +053088// This function modifyies the old data as per current version requirement and also
89// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053090func updateVpvs(cntx context.Context, hash string, value interface{}) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053091 logger.Debugw(ctx, "Update Vpvs", log.Fields{"Hash": hash})
Naveen Sampath04696f72022-06-13 15:19:14 +053092 //var param VoltPortVnet
93 param := value.(*VoltPortVnet)
94
vinokuma926cb3e2023-03-29 11:41:06 +053095 // Update SVlan Tag Protocol id param with default valud if not present
Naveen Sampath04696f72022-06-13 15:19:14 +053096 if param.SVlanTpid == 0 {
97 param.SVlanTpid = layers.EthernetTypeDot1Q
98 }
99
100 if strings.Count(hash, "-") > 1 {
101 logger.Info(ctx, "Already upgraded")
102 return nil
103 }
104
vinokuma926cb3e2023-03-29 11:41:06 +0530105 // Add the vpv under new path
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530106 param.WriteToDb(cntx)
vinokuma926cb3e2023-03-29 11:41:06 +0530107 // delete the older path
Naveen Sampath04696f72022-06-13 15:19:14 +0530108 fullPath := database.BasePath + database.VpvPath + hash
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530109 if err := db.Del(cntx, fullPath); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530110 logger.Errorw(ctx, "Vpv Delete from DB failed", log.Fields{"Error": err, "key": fullPath})
111 }
112 return nil
113}
114
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530115func updateMvlans(cntx context.Context, hash string, value interface{}) error {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530116 logger.Debugw(ctx, "Update Mvlans", log.Fields{"Hash": hash})
Naveen Sampath04696f72022-06-13 15:19:14 +0530117 param := value.(*MvlanProfile)
118 if param.DevicesList == nil || len(param.DevicesList) == 0 {
vinokuma926cb3e2023-03-29 11:41:06 +0530119 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 +0530120 if err := param.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530121 logger.Errorw(ctx, "Mvlan profile write to DB failed", log.Fields{"ProfileName": param.Name})
122 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530123 }
124 if _, ok := param.Groups[common.StaticGroup]; ok {
125 param.Groups[common.StaticGroup].IsStatic = true
126 }
127 return nil
128}
129
vinokuma926cb3e2023-03-29 11:41:06 +0530130// This function modifyies the old Igmp Group data as per current version requirement and also
131// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530132func updateIgmpGroups(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530133 ig := value.(*IgmpGroup)
134 logger.Infow(ctx, "Group Data Migration", log.Fields{"ig": ig, "GroupAddr": ig.GroupAddr, "hash": hash})
135 if ig.GroupAddr == nil {
136 ig.GroupAddr = net.ParseIP("0.0.0.0")
137 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530138 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530139 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
140 }
141
142 return nil
143}
144
vinokuma926cb3e2023-03-29 11:41:06 +0530145// This function modifyies the old Igmp Device data as per current version requirement and also
146// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530147func updateIgmpDevices(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530148 igd := value.(*IgmpGroupDevice)
149 logger.Infow(ctx, "Group Device Migration", log.Fields{"igd": igd, "GroupAddr": igd.GroupAddr, "hash": hash})
150 if igd.GroupAddr == nil {
151 igd.GroupAddr = net.ParseIP("0.0.0.0")
152 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530153 if err := igd.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530154 logger.Errorw(ctx, "Igmp group device Write to DB failed", log.Fields{"Device": igd.Device,
vinokuma926cb3e2023-03-29 11:41:06 +0530155 "GroupName": igd.GroupName, "GroupAddr": igd.GroupAddr.String()})
Naveen Sampath04696f72022-06-13 15:19:14 +0530156 }
157
158 return nil
159}
160
vinokuma926cb3e2023-03-29 11:41:06 +0530161// This function modifyies the old Igmp Profile data as per current version requirement and also
162// returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530163func updateIgmpProfiles(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530164 igmpProfile := value.(*IgmpProfile)
165 logger.Infow(ctx, "IGMP Profile Migration", log.Fields{"igmpProfile": igmpProfile, "hash": hash})
166 return nil
167}
168
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530169func (ig *IgmpGroup) migrateIgmpDevices(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530170 devices, _ := db.GetPrevIgmpDevices(cntx, ig.Mvlan, ig.GroupName)
Naveen Sampath04696f72022-06-13 15:19:14 +0530171 logger.Infow(ctx, "Migratable Devices", log.Fields{"Devices": devices})
172 for _, device := range devices {
173 b, ok := device.Value.([]byte)
174 if !ok {
175 logger.Warn(ctx, "The value type is not []byte")
176 continue
177 }
178 if igd, err := NewIgmpGroupDeviceFromBytes(b); err == nil {
179 key := database.BasePath + database.IgmpDevicePath + igd.Mvlan.String() + "/" + igd.GroupName + "/" + igd.Device
180 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igd": igd})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530181 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530182 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
183 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530184 if err := UpdateDbData(cntx, database.IgmpDevicePath, key, igd); err != nil {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +0530185 logger.Errorw(ctx, "Group Device Migration failed", log.Fields{"IGD": igd, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530186 } else {
187 logger.Infow(ctx, "Group Device Migrated", log.Fields{"IGD": igd})
188 }
189 } else {
190 logger.Warnw(ctx, "Unable to decode device from database", log.Fields{"str": string(b)})
191 }
192 }
193}
194
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530195func (igd *IgmpGroupDevice) migrateIgmpChannels(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530196 channels, _ := db.GetPrevIgmpChannels(cntx, igd.GroupName, igd.Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530197 logger.Infow(ctx, "Migratable Channels", log.Fields{"Channels": channels})
198 for _, channel := range channels {
Naveen Sampath04696f72022-06-13 15:19:14 +0530199 b, ok := channel.Value.([]byte)
200 if !ok {
201 logger.Warn(ctx, "The value type is not []byte")
202 continue
203 }
204 if igc, err := NewIgmpGroupChannelFromBytes(b); err == nil {
205 key := database.BasePath + database.IgmpChannelPath + igc.GroupName + "/" + igc.Device + "/" + igc.GroupAddr.String()
206 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igc": igc})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530207 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530208 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
209 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530210 if err := igc.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530211 logger.Errorw(ctx, "Igmp group channel Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
212 }
213
214 logger.Infow(ctx, "Group Channel Migrated", log.Fields{"IGD": igc})
215 } else {
216 logger.Warnw(ctx, "Unable to decode channel from database", log.Fields{"str": string(b)})
217 }
218 }
219}
220
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530221func (igc *IgmpGroupChannel) migrateIgmpPorts(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530222 ports, _ := db.GetPrevIgmpRcvrs(cntx, igc.GroupAddr, igc.Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530223 logger.Infow(ctx, "Migratable Ports", log.Fields{"Ports": ports})
224 for _, port := range ports {
Naveen Sampath04696f72022-06-13 15:19:14 +0530225 b, ok := port.Value.([]byte)
226 if !ok {
227 logger.Warn(ctx, "The value type is not []byte")
228 continue
229 }
230 if igp, err := NewIgmpGroupPortFromBytes(b); err == nil {
231 key := database.BasePath + database.IgmpPortPath + igc.GroupAddr.String() + "/" + igc.Device + "/" + igp.Port
232 logger.Infow(ctx, "Deleting old entry", log.Fields{"Key": key, "Igp": igp})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530233 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530234 logger.Errorw(ctx, "Igmp Group port Delete from DB failed", log.Fields{"Error": err, "key": key})
235 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530236 if err := igp.WriteToDb(cntx, igc.Mvlan, igc.GroupAddr, igc.Device); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530237 logger.Errorw(ctx, "Igmp group port Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
238 }
239
240 logger.Infow(ctx, "Group Port Migrated", log.Fields{"IGD": igp})
241 } else {
242 logger.Warnw(ctx, "Unable to decode port from database", log.Fields{"str": string(b)})
243 }
244 }
245}