Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "errors" |
| 20 | "net" |
| 21 | "voltha-go-controller/internal/pkg/types" |
| 22 | |
| 23 | "strings" |
| 24 | |
| 25 | "github.com/google/gopacket/layers" |
| 26 | "voltha-go-controller/database" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 27 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type paramsUpdationFunc func(hash string, value interface{}) error |
| 31 | |
| 32 | //map to store conversion functions |
| 33 | var updationMap = map[string]paramsUpdationFunc{ |
| 34 | database.VnetPath: updateVnets, |
| 35 | database.VpvPath: updateVpvs, |
| 36 | database.ServicePath: updateServices, |
| 37 | database.MvlanPath: updateMvlans, |
| 38 | database.IgmpGroupPath: updateIgmpGroups, |
| 39 | database.IgmpDevicePath: updateIgmpDevices, |
| 40 | database.IgmpProfPath: updateIgmpProfiles, |
| 41 | } |
| 42 | |
| 43 | // UpdateDbData to update database data |
| 44 | func UpdateDbData(dbPath, hash string, value interface{}) error { |
| 45 | if migrationFunc, ok := updationMap[dbPath]; ok { |
| 46 | err := migrationFunc(hash, value) |
| 47 | if err != nil { |
| 48 | logger.Error(ctx, "Error in migrating data\n") |
| 49 | return errors.New("Error-in-migration") |
| 50 | } |
| 51 | } |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | //This function modifyies the old data as per current version requirement and also |
| 56 | //returns the new path on which the modified data has to be written |
| 57 | func updateServices(hash string, value interface{}) error { |
| 58 | param := value.(*VoltService) |
| 59 | param.VnetID = VnetKey(param.SVlan, param.CVlan, param.UniVlan) |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | //This function modifyies the old data as per current version requirement and also |
| 64 | //returns the new path on which the modified data has to be written |
| 65 | func updateVnets(hash string, value interface{}) error { |
| 66 | param := value.(*VoltVnet) |
| 67 | newKey := VnetKey(param.SVlan, param.CVlan, param.UniVlan) |
| 68 | if newKey != hash { |
| 69 | //Delete the older key |
| 70 | _ = db.DelVnet(hash) |
| 71 | } else { |
| 72 | //Update SVlan Tag Protocol id param with default valud if not present |
| 73 | if param.SVlanTpid == 0 { |
| 74 | param.SVlanTpid = layers.EthernetTypeDot1Q |
| 75 | } |
| 76 | } |
| 77 | param.Name = newKey |
| 78 | if param.DevicesList == nil || len(param.DevicesList) == 0 { |
| 79 | param.DevicesList = append(param.DevicesList, "") //Empty OLT serial number as of now since submgr won't have proper serial num |
| 80 | } |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | //This function modifyies the old data as per current version requirement and also |
| 85 | //returns the new path on which the modified data has to be written |
| 86 | func updateVpvs(hash string, value interface{}) error { |
| 87 | |
| 88 | //var param VoltPortVnet |
| 89 | param := value.(*VoltPortVnet) |
| 90 | |
| 91 | //Update SVlan Tag Protocol id param with default valud if not present |
| 92 | if param.SVlanTpid == 0 { |
| 93 | param.SVlanTpid = layers.EthernetTypeDot1Q |
| 94 | } |
| 95 | |
| 96 | if strings.Count(hash, "-") > 1 { |
| 97 | logger.Info(ctx, "Already upgraded") |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | //Add the vpv under new path |
| 102 | param.WriteToDb() |
| 103 | //delete the older path |
| 104 | fullPath := database.BasePath + database.VpvPath + hash |
| 105 | if err := db.Del(fullPath); err != nil { |
| 106 | logger.Errorw(ctx, "Vpv Delete from DB failed", log.Fields{"Error": err, "key": fullPath}) |
| 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func updateMvlans(hash string, value interface{}) error { |
| 112 | param := value.(*MvlanProfile) |
| 113 | if param.DevicesList == nil || len(param.DevicesList) == 0 { |
| 114 | param.DevicesList = make(map[string]OperInProgress) //Empty OLT serial number as of now since submgr won't have proper serial num |
| 115 | if err := param.WriteToDb(); err != nil { |
| 116 | logger.Errorw(ctx, "Mvlan profile write to DB failed", log.Fields{"ProfileName": param.Name}) |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | if _, ok := param.Groups[common.StaticGroup]; ok { |
| 121 | param.Groups[common.StaticGroup].IsStatic = true |
| 122 | } |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | //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 |
| 128 | func updateIgmpGroups(hash string, value interface{}) error { |
| 129 | |
| 130 | ig := value.(*IgmpGroup) |
| 131 | logger.Infow(ctx, "Group Data Migration", log.Fields{"ig": ig, "GroupAddr": ig.GroupAddr, "hash": hash}) |
| 132 | if ig.GroupAddr == nil { |
| 133 | ig.GroupAddr = net.ParseIP("0.0.0.0") |
| 134 | } |
| 135 | if err := ig.WriteToDb(); err != nil { |
| 136 | logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName}) |
| 137 | } |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | //This function modifyies the old Igmp Device data as per current version requirement and also |
| 143 | //returns the new path on which the modified data has to be written |
| 144 | func updateIgmpDevices(hash string, value interface{}) error { |
| 145 | igd := value.(*IgmpGroupDevice) |
| 146 | logger.Infow(ctx, "Group Device Migration", log.Fields{"igd": igd, "GroupAddr": igd.GroupAddr, "hash": hash}) |
| 147 | if igd.GroupAddr == nil { |
| 148 | igd.GroupAddr = net.ParseIP("0.0.0.0") |
| 149 | } |
| 150 | if err := igd.WriteToDb(); err != nil { |
| 151 | logger.Errorw(ctx, "Igmp group device Write to DB failed", log.Fields{"Device": igd.Device, |
| 152 | "GroupName": igd.GroupName, "GroupAddr": igd.GroupAddr.String()}) |
| 153 | } |
| 154 | |
| 155 | return nil |
| 156 | } |
| 157 | |
| 158 | //This function modifyies the old Igmp Profile data as per current version requirement and also |
| 159 | //returns the new path on which the modified data has to be written |
| 160 | func updateIgmpProfiles(hash string, value interface{}) error { |
| 161 | igmpProfile := value.(*IgmpProfile) |
| 162 | logger.Infow(ctx, "IGMP Profile Migration", log.Fields{"igmpProfile": igmpProfile, "hash": hash}) |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func (ig *IgmpGroup) migrateIgmpDevices() { |
| 167 | |
| 168 | devices, _ := db.GetPrevIgmpDevices(ig.Mvlan, ig.GroupName) |
| 169 | logger.Infow(ctx, "Migratable Devices", log.Fields{"Devices": devices}) |
| 170 | for _, device := range devices { |
| 171 | b, ok := device.Value.([]byte) |
| 172 | if !ok { |
| 173 | logger.Warn(ctx, "The value type is not []byte") |
| 174 | continue |
| 175 | } |
| 176 | if igd, err := NewIgmpGroupDeviceFromBytes(b); err == nil { |
| 177 | key := database.BasePath + database.IgmpDevicePath + igd.Mvlan.String() + "/" + igd.GroupName + "/" + igd.Device |
| 178 | logger.Infow(ctx, "Deleting old entry", log.Fields{"Path": key, "igd": igd}) |
| 179 | if err := db.Del(key); err != nil { |
| 180 | logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 181 | } |
| 182 | if err := UpdateDbData(database.IgmpDevicePath, key, igd); err != nil { |
| 183 | logger.Warnw(ctx, "Group Device Migration failed", log.Fields{"IGD": igd, "Error": err}) |
| 184 | } else { |
| 185 | logger.Infow(ctx, "Group Device Migrated", log.Fields{"IGD": igd}) |
| 186 | } |
| 187 | } else { |
| 188 | logger.Warnw(ctx, "Unable to decode device from database", log.Fields{"str": string(b)}) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func (igd *IgmpGroupDevice) migrateIgmpChannels() { |
| 194 | |
| 195 | channels, _ := db.GetPrevIgmpChannels(igd.GroupName, igd.Device) |
| 196 | logger.Infow(ctx, "Migratable Channels", log.Fields{"Channels": channels}) |
| 197 | for _, channel := range channels { |
| 198 | |
| 199 | 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}) |
| 207 | if err := db.Del(key); err != nil { |
| 208 | logger.Errorw(ctx, "Igmp Group Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 209 | } |
| 210 | if err := igc.WriteToDb(); err != nil { |
| 211 | 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 | |
| 221 | func (igc *IgmpGroupChannel) migrateIgmpPorts() { |
| 222 | |
| 223 | ports, _ := db.GetPrevIgmpRcvrs(igc.GroupAddr, igc.Device) |
| 224 | logger.Infow(ctx, "Migratable Ports", log.Fields{"Ports": ports}) |
| 225 | for _, port := range ports { |
| 226 | |
| 227 | b, ok := port.Value.([]byte) |
| 228 | if !ok { |
| 229 | logger.Warn(ctx, "The value type is not []byte") |
| 230 | continue |
| 231 | } |
| 232 | if igp, err := NewIgmpGroupPortFromBytes(b); err == nil { |
| 233 | key := database.BasePath + database.IgmpPortPath + igc.GroupAddr.String() + "/" + igc.Device + "/" + igp.Port |
| 234 | logger.Infow(ctx, "Deleting old entry", log.Fields{"Key": key, "Igp": igp}) |
| 235 | if err := db.Del(key); err != nil { |
| 236 | logger.Errorw(ctx, "Igmp Group port Delete from DB failed", log.Fields{"Error": err, "key": key}) |
| 237 | } |
| 238 | if err := igp.WriteToDb(igc.Mvlan, igc.GroupAddr, igc.Device); err != nil { |
| 239 | logger.Errorw(ctx, "Igmp group port Write to DB failed", log.Fields{"mvlan": igc.Mvlan, "GroupAddr": igc.GroupAddr}) |
| 240 | } |
| 241 | |
| 242 | logger.Infow(ctx, "Group Port Migrated", log.Fields{"IGD": igp}) |
| 243 | } else { |
| 244 | logger.Warnw(ctx, "Unable to decode port from database", log.Fields{"str": string(b)}) |
| 245 | } |
| 246 | } |
| 247 | } |