blob: c146f23f703e9810bd86a0300bdb6e0ef762936c [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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package controller
17
18import (
19 "context"
20 "errors"
21 "time"
22
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053024
Naveen Sampath04696f72022-06-13 15:19:14 +053025 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
26)
27
28// ChangeEventTask structure
29type ChangeEventTask struct {
Naveen Sampath04696f72022-06-13 15:19:14 +053030 ctx context.Context
31 event *ofp.ChangeEvent
32 device *Device
33 timestamp string
vinokuma926cb3e2023-03-29 11:41:06 +053034 taskID uint8
Naveen Sampath04696f72022-06-13 15:19:14 +053035}
36
37// NewChangeEventTask is constructor for ChangeEventTask
38func 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
49func (cet *ChangeEventTask) Name() string {
50 return "Change Event Task"
51}
52
53// TaskID to return task id of the task
54func (cet *ChangeEventTask) TaskID() uint8 {
55 return cet.taskID
56}
57
58// Timestamp to return timestamp for the task
59func (cet *ChangeEventTask) Timestamp() string {
60 return cet.timestamp
61}
62
63// Stop to stop the task
64func (cet *ChangeEventTask) Stop() {
65}
66
67// Start to start the Change event task
68func (cet *ChangeEventTask) Start(ctx context.Context, taskID uint8) error {
69 cet.taskID = taskID
70 cet.ctx = ctx
Naveen Sampath04696f72022-06-13 15:19:14 +053071 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 Joseph429b9d92022-11-16 18:51:05 +053077 _ = cet.device.AddPort(ctx, status.PortStatus.Desc)
Naveen Sampath04696f72022-06-13 15:19:14 +053078 if state == uint32(ofp.OfpPortState_OFPPS_LIVE) {
Akash Sonief452f12024-12-12 18:20:28 +053079 cet.device.ProcessPortState(ctx, portNo, state, portName)
Naveen Sampath04696f72022-06-13 15:19:14 +053080 }
81 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_DELETE {
Akash Sonief452f12024-12-12 18:20:28 +053082 cet.device.CheckAndDeletePort(ctx, portNo, portName)
Naveen Sampath04696f72022-06-13 15:19:14 +053083 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_MODIFY {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053084 cet.device.ProcessPortUpdate(ctx, portName, portNo, state)
Naveen Sampath04696f72022-06-13 15:19:14 +053085 }
Akash Soni6168f312023-05-18 20:57:33 +053086 logger.Debugw(ctx, "Processed Port Change Event", log.Fields{"Port No": portNo, "Port Name": portName, "State": state, "Reason": status.PortStatus.Reason})
Naveen Sampath04696f72022-06-13 15:19:14 +053087 return nil
88 }
Akash Sonid36d23b2023-08-18 12:51:40 +053089 return errors.New("invalid message received")
Naveen Sampath04696f72022-06-13 15:19:14 +053090}