blob: a0bba15b9f09e5706314a3027a489d49293f8e55 [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 "errors"
Tinoj Joseph07cc5372022-07-18 22:53:51 +053020 "context"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 "net"
22 "voltha-go-controller/internal/pkg/types"
23
24 "strings"
25
26 "github.com/google/gopacket/layers"
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
Tinoj Joseph07cc5372022-07-18 22:53:51 +053031type paramsUpdationFunc func(cntx context.Context, hash string, value interface{}) error
Naveen Sampath04696f72022-06-13 15:19:14 +053032
33//map to store conversion functions
34var updationMap = map[string]paramsUpdationFunc{
35 database.VnetPath: updateVnets,
36 database.VpvPath: updateVpvs,
37 database.ServicePath: updateServices,
38 database.MvlanPath: updateMvlans,
39 database.IgmpGroupPath: updateIgmpGroups,
40 database.IgmpDevicePath: updateIgmpDevices,
41 database.IgmpProfPath: updateIgmpProfiles,
42}
43
44// UpdateDbData to update database data
Tinoj Joseph07cc5372022-07-18 22:53:51 +053045func UpdateDbData(cntx context.Context, dbPath, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053046 if migrationFunc, ok := updationMap[dbPath]; ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053047 err := migrationFunc(cntx, hash, value)
Naveen Sampath04696f72022-06-13 15:19:14 +053048 if err != nil {
49 logger.Error(ctx, "Error in migrating data\n")
50 return errors.New("Error-in-migration")
51 }
52 }
53 return nil
54}
55
56//This function modifyies the old data as per current version requirement and also
57//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053058func updateServices(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053059 param := value.(*VoltService)
60 param.VnetID = VnetKey(param.SVlan, param.CVlan, param.UniVlan)
61 return nil
62}
63
64//This function modifyies the old data as per current version requirement and also
65//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053066func updateVnets(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053067 param := value.(*VoltVnet)
68 newKey := VnetKey(param.SVlan, param.CVlan, param.UniVlan)
69 if newKey != hash {
70 //Delete the older key
Tinoj Joseph07cc5372022-07-18 22:53:51 +053071 _ = db.DelVnet(cntx, hash)
Naveen Sampath04696f72022-06-13 15:19:14 +053072 } else {
73 //Update SVlan Tag Protocol id param with default valud if not present
74 if param.SVlanTpid == 0 {
75 param.SVlanTpid = layers.EthernetTypeDot1Q
76 }
77 }
78 param.Name = newKey
79 if param.DevicesList == nil || len(param.DevicesList) == 0 {
80 param.DevicesList = append(param.DevicesList, "") //Empty OLT serial number as of now since submgr won't have proper serial num
81 }
82 return nil
83}
84
85//This function modifyies the old data as per current version requirement and also
86//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +053087func updateVpvs(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053088
89 //var param VoltPortVnet
90 param := value.(*VoltPortVnet)
91
92 //Update SVlan Tag Protocol id param with default valud if not present
93 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
102 //Add the vpv under new path
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530103 param.WriteToDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530104 //delete the older path
105 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 {
115 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 }
119
120 }
121 if _, ok := param.Groups[common.StaticGroup]; ok {
122 param.Groups[common.StaticGroup].IsStatic = true
123 }
124 return nil
125}
126
127//This function modifyies the old Igmp Group data as per current version requirement and also
128//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530129func updateIgmpGroups(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530130
131 ig := value.(*IgmpGroup)
132 logger.Infow(ctx, "Group Data Migration", log.Fields{"ig": ig, "GroupAddr": ig.GroupAddr, "hash": hash})
133 if ig.GroupAddr == nil {
134 ig.GroupAddr = net.ParseIP("0.0.0.0")
135 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530136 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530137 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
138 }
139
140 return nil
141}
142
143//This function modifyies the old Igmp Device data as per current version requirement and also
144//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530145func updateIgmpDevices(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530146 igd := value.(*IgmpGroupDevice)
147 logger.Infow(ctx, "Group Device Migration", log.Fields{"igd": igd, "GroupAddr": igd.GroupAddr, "hash": hash})
148 if igd.GroupAddr == nil {
149 igd.GroupAddr = net.ParseIP("0.0.0.0")
150 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530151 if err := igd.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530152 logger.Errorw(ctx, "Igmp group device Write to DB failed", log.Fields{"Device": igd.Device,
153 "GroupName": igd.GroupName, "GroupAddr": igd.GroupAddr.String()})
154 }
155
156 return nil
157}
158
159//This function modifyies the old Igmp Profile data as per current version requirement and also
160//returns the new path on which the modified data has to be written
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530161func updateIgmpProfiles(cntx context.Context, hash string, value interface{}) error {
Naveen Sampath04696f72022-06-13 15:19:14 +0530162 igmpProfile := value.(*IgmpProfile)
163 logger.Infow(ctx, "IGMP Profile Migration", log.Fields{"igmpProfile": igmpProfile, "hash": hash})
164 return nil
165}
166
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530167func (ig *IgmpGroup) migrateIgmpDevices(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530168
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530169 devices, _ := db.GetPrevIgmpDevices(cntx, ig.Mvlan, ig.GroupName)
Naveen Sampath04696f72022-06-13 15:19:14 +0530170 logger.Infow(ctx, "Migratable Devices", log.Fields{"Devices": devices})
171 for _, device := range devices {
172 b, ok := device.Value.([]byte)
173 if !ok {
174 logger.Warn(ctx, "The value type is not []byte")
175 continue
176 }
177 if igd, err := NewIgmpGroupDeviceFromBytes(b); err == nil {
178 key := database.BasePath + database.IgmpDevicePath + igd.Mvlan.String() + "/" + igd.GroupName + "/" + igd.Device
179 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igd": igd})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530180 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
182 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530183 if err := UpdateDbData(cntx, database.IgmpDevicePath, key, igd); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530184 logger.Warnw(ctx, "Group Device Migration failed", log.Fields{"IGD": igd, "Error": err})
185 } else {
186 logger.Infow(ctx, "Group Device Migrated", log.Fields{"IGD": igd})
187 }
188 } else {
189 logger.Warnw(ctx, "Unable to decode device from database", log.Fields{"str": string(b)})
190 }
191 }
192}
193
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530194func (igd *IgmpGroupDevice) migrateIgmpChannels(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530195
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 {
199
200 b, ok := channel.Value.([]byte)
201 if !ok {
202 logger.Warn(ctx, "The value type is not []byte")
203 continue
204 }
205 if igc, err := NewIgmpGroupChannelFromBytes(b); err == nil {
206 key := database.BasePath + database.IgmpChannelPath + igc.GroupName + "/" + igc.Device + "/" + igc.GroupAddr.String()
207 logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igc": igc})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530208 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530209 logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key})
210 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530211 if err := igc.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530212 logger.Errorw(ctx, "Igmp group channel Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
213 }
214
215 logger.Infow(ctx, "Group Channel Migrated", log.Fields{"IGD": igc})
216 } else {
217 logger.Warnw(ctx, "Unable to decode channel from database", log.Fields{"str": string(b)})
218 }
219 }
220}
221
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530222func (igc *IgmpGroupChannel) migrateIgmpPorts(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530223
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530224 ports, _ := db.GetPrevIgmpRcvrs(cntx, igc.GroupAddr, igc.Device)
Naveen Sampath04696f72022-06-13 15:19:14 +0530225 logger.Infow(ctx, "Migratable Ports", log.Fields{"Ports": ports})
226 for _, port := range ports {
227
228 b, ok := port.Value.([]byte)
229 if !ok {
230 logger.Warn(ctx, "The value type is not []byte")
231 continue
232 }
233 if igp, err := NewIgmpGroupPortFromBytes(b); err == nil {
234 key := database.BasePath + database.IgmpPortPath + igc.GroupAddr.String() + "/" + igc.Device + "/" + igp.Port
235 logger.Infow(ctx, "Deleting old entry", log.Fields{"Key": key, "Igp": igp})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530236 if err := db.Del(cntx, key); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530237 logger.Errorw(ctx, "Igmp Group port Delete from DB failed", log.Fields{"Error": err, "key": key})
238 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530239 if err := igp.WriteToDb(cntx, igc.Mvlan, igc.GroupAddr, igc.Device); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530240 logger.Errorw(ctx, "Igmp group port Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr})
241 }
242
243 logger.Infow(ctx, "Group Port Migrated", log.Fields{"IGD": igp})
244 } else {
245 logger.Warnw(ctx, "Unable to decode port from database", log.Fields{"str": string(b)})
246 }
247 }
248}