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 controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | "voltha-go-controller/internal/pkg/tasks" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 23 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v5/go/common" |
| 25 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 26 | ) |
| 27 | |
| 28 | // AuditEventType type |
| 29 | type AuditEventType uint8 |
| 30 | |
| 31 | const ( |
| 32 | // AuditEventDeviceDisc constant |
| 33 | AuditEventDeviceDisc AuditEventType = 0 |
| 34 | // AuditEventDeviceStateChange constant |
| 35 | AuditEventDeviceStateChange AuditEventType = 1 |
| 36 | ) |
| 37 | |
| 38 | const ( |
| 39 | // NNIPortID NNI port id |
| 40 | NNIPortID uint32 = 0x1000000 |
| 41 | ) |
| 42 | |
| 43 | // AuditDevice structure |
| 44 | type AuditDevice struct { |
| 45 | taskID uint8 |
| 46 | ctx context.Context |
| 47 | device *Device |
| 48 | stop bool |
| 49 | timestamp string |
| 50 | event AuditEventType |
| 51 | } |
| 52 | |
| 53 | // NewAuditDevice is constructor for AuditDevice |
| 54 | func NewAuditDevice(device *Device, event AuditEventType) *AuditDevice { |
| 55 | var ad AuditDevice |
| 56 | ad.device = device |
| 57 | ad.stop = false |
| 58 | tstamp := (time.Now()).Format(time.RFC3339Nano) |
| 59 | ad.timestamp = tstamp |
| 60 | ad.event = event |
| 61 | return &ad |
| 62 | } |
| 63 | |
| 64 | // Name returns the task name |
| 65 | func (ad *AuditDevice) Name() string { |
| 66 | return "Device Audit Task" |
| 67 | } |
| 68 | |
| 69 | // TaskID returns the task id |
| 70 | func (ad *AuditDevice) TaskID() uint8 { |
| 71 | return ad.taskID |
| 72 | } |
| 73 | |
| 74 | // Timestamp returns the timestamp for the task |
| 75 | func (ad *AuditDevice) Timestamp() string { |
| 76 | return ad.timestamp |
| 77 | } |
| 78 | |
| 79 | // Stop to stop the task |
| 80 | func (ad *AuditDevice) Stop() { |
| 81 | ad.stop = true |
| 82 | } |
| 83 | |
| 84 | // Start to start the task |
| 85 | func (ad *AuditDevice) Start(ctx context.Context, taskID uint8) error { |
| 86 | logger.Warnw(ctx, "Audit Device Task Triggered", log.Fields{"Context": ctx, "taskId": taskID, "Device": ad.device.ID}) |
| 87 | ad.taskID = taskID |
| 88 | ad.ctx = ctx |
| 89 | |
| 90 | if ad.stop { |
| 91 | logger.Errorw(ctx, "Audit Device Task Cancelled", log.Fields{"Context": ad.ctx, "Task": ad.taskID}) |
| 92 | return tasks.ErrTaskCancelError |
| 93 | } |
| 94 | |
| 95 | ofpps, err := ad.device.VolthaClient().ListLogicalDevicePorts(ad.ctx, &common.ID{Id: ad.device.ID}) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | // Compute the difference between the ports received and ports at VGC |
| 101 | // First build a map of all the received ports under missing ports. We |
| 102 | // will eliminate the ports that are in the device from the missing ports |
| 103 | // so that the elements remaining are missing ports. The ones that are |
| 104 | // not in missing ports are added to excess ports which should be deleted |
| 105 | // from the VGC. |
| 106 | missingPorts := make(map[uint32]*ofp.OfpPort) |
| 107 | for _, ofpp := range ofpps.Items { |
| 108 | missingPorts[ofpp.OfpPort.PortNo] = ofpp.OfpPort |
| 109 | } |
| 110 | |
| 111 | var excessPorts []uint32 |
| 112 | GetController().SetAuditFlags(ad.device) |
| 113 | |
| 114 | processPortState := func(id uint32, vgcPort *DevicePort) { |
| 115 | logger.Debugw(ctx, "Process Port State Ind", log.Fields{"Port No": vgcPort.ID, "Port Name": vgcPort.Name}) |
| 116 | |
| 117 | if ofpPort, ok := missingPorts[id]; ok { |
| 118 | if ((vgcPort.State == PortStateDown) && (ofpPort.State == uint32(ofp.OfpPortState_OFPPS_LIVE))) || ((vgcPort.State == PortStateUp) && (ofpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE))) { |
| 119 | // This port exists in the received list and the map at |
| 120 | // VGC. This is common so delete it |
| 121 | logger.Infow(ctx, "Port State Mismatch", log.Fields{"Port": vgcPort.ID, "OfpPort": ofpPort.PortNo, "ReceivedState": ofpPort.State, "CurrentState": vgcPort.State}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 122 | ad.device.ProcessPortState(ctx, ofpPort.PortNo, ofpPort.State) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 123 | } else { |
| 124 | //To ensure the flows are in sync with port status and no mismatch due to reboot, |
| 125 | // repush/delete flows based on current port status |
| 126 | logger.Infow(ctx, "Port State Processing", log.Fields{"Port": vgcPort.ID, "OfpPort": ofpPort.PortNo, "ReceivedState": ofpPort.State, "CurrentState": vgcPort.State}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 127 | ad.device.ProcessPortStateAfterReboot(ctx, ofpPort.PortNo, ofpPort.State) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 128 | } |
| 129 | delete(missingPorts, id) |
| 130 | } else { |
| 131 | // This port is missing from the received list. This is an |
| 132 | // excess port at VGC. This must be added to excess ports |
| 133 | excessPorts = append(excessPorts, id) |
| 134 | } |
| 135 | logger.Debugw(ctx, "Processed Port State Ind", log.Fields{"Port No": vgcPort.ID, "Port Name": vgcPort.Name}) |
| 136 | |
| 137 | } |
| 138 | |
| 139 | // 1st process the NNI port before all other ports so that the device state can be updated. |
| 140 | if vgcPort, ok := ad.device.PortsByID[NNIPortID]; ok { |
| 141 | logger.Info(ctx, "Processing NNI port state") |
| 142 | processPortState(NNIPortID, vgcPort) |
| 143 | } |
| 144 | |
| 145 | for id, vgcPort := range ad.device.PortsByID { |
| 146 | if id == NNIPortID { |
| 147 | //NNI port already processed |
| 148 | continue |
| 149 | } |
| 150 | if ad.stop { |
| 151 | break |
| 152 | } |
| 153 | processPortState(id, vgcPort) |
| 154 | } |
| 155 | GetController().ResetAuditFlags(ad.device) |
| 156 | |
| 157 | if ad.stop { |
| 158 | logger.Errorw(ctx, "Audit Device Task Cancelled", log.Fields{"Context": ad.ctx, "Task": ad.taskID}) |
| 159 | return tasks.ErrTaskCancelError |
| 160 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 161 | ad.AddMissingPorts(ctx, missingPorts) |
| 162 | ad.DelExcessPorts(ctx, excessPorts) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 163 | ad.device.deviceAuditInProgress = false |
| 164 | logger.Warnw(ctx, "Audit Device Task Completed", log.Fields{"Context": ctx, "taskId": taskID, "Device": ad.device.ID}) |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | // AddMissingPorts to add the missing ports |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 169 | func (ad *AuditDevice) AddMissingPorts(cntx context.Context, mps map[uint32]*ofp.OfpPort) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 170 | logger.Debugw(ctx, "Device Audit - Add Missing Ports", log.Fields{"NumPorts": len(mps)}) |
| 171 | |
| 172 | addMissingPort := func(mp *ofp.OfpPort) { |
| 173 | logger.Debugw(ctx, "Process Port Add Ind", log.Fields{"Port No": mp.PortNo, "Port Name": mp.Name}) |
| 174 | |
| 175 | // Error is ignored as it only drops duplicate ports |
| 176 | logger.Infow(ctx, "Calling AddPort", log.Fields{"No": mp.PortNo, "Name": mp.Name}) |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 177 | if err := ad.device.AddPort(cntx, mp); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 178 | logger.Warnw(ctx, "AddPort Failed", log.Fields{"No": mp.PortNo, "Name": mp.Name, "Reason": err}) |
| 179 | } |
| 180 | if mp.State == uint32(ofp.OfpPortState_OFPPS_LIVE) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 181 | ad.device.ProcessPortState(cntx, mp.PortNo, mp.State) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 182 | } |
| 183 | logger.Debugw(ctx, "Processed Port Add Ind", log.Fields{"Port No": mp.PortNo, "Port Name": mp.Name}) |
| 184 | |
| 185 | } |
| 186 | |
| 187 | // 1st process the NNI port before all other ports so that the flow provisioning for UNIs can be enabled |
| 188 | if mp, ok := mps[NNIPortID]; ok { |
| 189 | logger.Info(ctx, "Adding Missing NNI port") |
| 190 | addMissingPort(mp) |
| 191 | } |
| 192 | |
| 193 | for portNo, mp := range mps { |
| 194 | if portNo != NNIPortID { |
| 195 | addMissingPort(mp) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // DelExcessPorts to delete the excess ports |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 201 | func (ad *AuditDevice) DelExcessPorts(cntx context.Context, eps []uint32) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 202 | logger.Debugw(ctx, "Device Audit - Delete Excess Ports", log.Fields{"NumPorts": len(eps)}) |
| 203 | for _, id := range eps { |
| 204 | // Now delete the port from the device @ VGC |
| 205 | logger.Infow(ctx, "Device Audit - Deleting Port", log.Fields{"PortId": id}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 206 | if err := ad.device.DelPort(cntx, id); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 207 | logger.Warnw(ctx, "DelPort Failed", log.Fields{"PortId": id, "Reason": err}) |
| 208 | } |
| 209 | } |
| 210 | } |