blob: ae744d613202c5bae99b2e8c1457c7e71ef9a38b [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
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 Joseph429b9d92022-11-16 18:51:05 +053078 _ = cet.device.AddPort(ctx, status.PortStatus.Desc)
Naveen Sampath04696f72022-06-13 15:19:14 +053079 if state == uint32(ofp.OfpPortState_OFPPS_LIVE) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053080 cet.device.ProcessPortState(ctx, portNo, state)
Naveen Sampath04696f72022-06-13 15:19:14 +053081 }
82 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_DELETE {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053083 if err := cet.device.DelPort(ctx, portNo); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053084 logger.Warnw(ctx, "DelPort Failed", log.Fields{"Port No": portNo, "Error": err})
85 }
86 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_MODIFY {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053087 cet.device.ProcessPortUpdate(ctx, portName, portNo, state)
Naveen Sampath04696f72022-06-13 15:19:14 +053088 }
Akash Soni6168f312023-05-18 20:57:33 +053089 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 +053090 return nil
91 }
92 return errors.New("Invalid message received")
93}