blob: 9a14d1ab33db91b98cb71fdfd02a8e1e993a0849 [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.
14*/
15
16package controller
17
18import (
19 "context"
20 "errors"
21 "time"
22
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053024 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
25)
26
27// ChangeEventTask structure
28type 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
37func 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
48func (cet *ChangeEventTask) Name() string {
49 return "Change Event Task"
50}
51
52// TaskID to return task id of the task
53func (cet *ChangeEventTask) TaskID() uint8 {
54 return cet.taskID
55}
56
57// Timestamp to return timestamp for the task
58func (cet *ChangeEventTask) Timestamp() string {
59 return cet.timestamp
60}
61
62// Stop to stop the task
63func (cet *ChangeEventTask) Stop() {
64}
65
66// Start to start the Change event task
67func (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 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) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053079 cet.device.ProcessPortState(ctx, portNo, state)
Naveen Sampath04696f72022-06-13 15:19:14 +053080 }
81 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_DELETE {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053082 if err := cet.device.DelPort(ctx, portNo); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053083 logger.Warnw(ctx, "DelPort Failed", log.Fields{"Port No": portNo, "Error": err})
84 }
85 } else if status.PortStatus.Reason == ofp.OfpPortReason_OFPPR_MODIFY {
Tinoj Joseph07cc5372022-07-18 22:53:51 +053086 cet.device.ProcessPortUpdate(ctx, portName, portNo, state)
Naveen Sampath04696f72022-06-13 15:19:14 +053087 }
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}