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