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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package controller |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "time" |
| 22 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 23 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 24 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 26 | ) |
| 27 | |
| 28 | // ChangeEventTask structure |
| 29 | type ChangeEventTask struct { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 30 | ctx context.Context |
| 31 | event *ofp.ChangeEvent |
| 32 | device *Device |
| 33 | timestamp string |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 34 | taskID uint8 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // NewChangeEventTask is constructor for ChangeEventTask |
| 38 | func NewChangeEventTask(ctx context.Context, event *ofp.ChangeEvent, device *Device) *ChangeEventTask { |
| 39 | var cet ChangeEventTask |
| 40 | cet.device = device |
| 41 | cet.event = event |
| 42 | cet.ctx = ctx |
| 43 | tstamp := (time.Now()).Format(time.RFC3339Nano) |
| 44 | cet.timestamp = tstamp |
| 45 | return &cet |
| 46 | } |
| 47 | |
| 48 | // Name returns the name of the task |
| 49 | func (cet *ChangeEventTask) Name() string { |
| 50 | return "Change Event Task" |
| 51 | } |
| 52 | |
| 53 | // TaskID to return task id of the task |
| 54 | func (cet *ChangeEventTask) TaskID() uint8 { |
| 55 | return cet.taskID |
| 56 | } |
| 57 | |
| 58 | // Timestamp to return timestamp for the task |
| 59 | func (cet *ChangeEventTask) Timestamp() string { |
| 60 | return cet.timestamp |
| 61 | } |
| 62 | |
| 63 | // Stop to stop the task |
| 64 | func (cet *ChangeEventTask) Stop() { |
| 65 | } |
| 66 | |
| 67 | // Start to start the Change event task |
| 68 | func (cet *ChangeEventTask) Start(ctx context.Context, taskID uint8) error { |
| 69 | cet.taskID = taskID |
| 70 | cet.ctx = ctx |
| 71 | |
| 72 | if status, ok := cet.event.Event.(*ofp.ChangeEvent_PortStatus); ok { |
| 73 | portNo := status.PortStatus.Desc.PortNo |
| 74 | portName := status.PortStatus.Desc.Name |
| 75 | state := status.PortStatus.Desc.State |
| 76 | logger.Infow(ctx, "Process Port Change Event", log.Fields{"Port No": portNo, "Port Name": portName, "State": state, "Reason": status.PortStatus.Reason}) |
| 77 | if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_ADD { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 78 | _ = cet.device.AddPort(ctx, status.PortStatus.Desc) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 79 | if state == uint32(ofp.OfpPortState_OFPPS_LIVE) { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 80 | cet.device.ProcessPortState(ctx, portNo, state) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 81 | } |
| 82 | } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_DELETE { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 83 | if err := cet.device.DelPort(ctx, portNo); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 84 | logger.Warnw(ctx, "DelPort Failed", log.Fields{"Port No": portNo, "Error": err}) |
| 85 | } |
| 86 | } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_MODIFY { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 87 | cet.device.ProcessPortUpdate(ctx, portName, portNo, state) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 88 | } |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 89 | logger.Debugw(ctx, "Processed Port Change Event", log.Fields{"Port No": portNo, "Port Name": portName, "State": state, "Reason": status.PortStatus.Reason}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 90 | return nil |
| 91 | } |
| 92 | return errors.New("Invalid message received") |
| 93 | } |