Mahir Gunyel | b24b9d0 | 2021-05-13 08:58:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package events |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "strconv" |
| 21 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v5/go/common" |
| 23 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Mahir Gunyel | b24b9d0 | 2021-05-13 08:58:16 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type ContextType string |
| 27 | |
| 28 | const ( |
| 29 | // ContextAdminState is for the admin state of the Device in the context of the event |
| 30 | ContextAdminState ContextType = "admin-state" |
| 31 | // ContextConnectState is for the connect state of the Device in the context of the event |
| 32 | ContextConnectState ContextType = "connect-state" |
| 33 | // ContextOperState is for the operational state of the Device in the context of the event |
| 34 | ContextOperState ContextType = "oper-state" |
| 35 | // ContextPrevdminState is for the previous admin state of the Device in the context of the event |
| 36 | ContextPrevAdminState ContextType = "prev-admin-state" |
| 37 | // ContextPrevConnectState is for the previous connect state of the Device in the context of the event |
| 38 | ContextPrevConnectState ContextType = "prev-connect-state" |
| 39 | // ContextPrevOperState is for the previous operational state of the Device in the context of the event |
| 40 | ContextPrevOperState ContextType = "prev-oper-state" |
| 41 | // ContextDeviceID is for the previous operational state of the Device in the context of the event |
| 42 | ContextDeviceID ContextType = "id" |
| 43 | // ContextParentID is for the parent id in the context of the event |
| 44 | ContextParentID ContextType = "parent-id" |
| 45 | // ContextSerialNumber is for the serial number of the Device in the context of the event |
| 46 | ContextSerialNumber ContextType = "serial-number" |
| 47 | // ContextIsRoot is for the root flag of Device in the context of the event |
| 48 | ContextIsRoot ContextType = "is-root" |
| 49 | // ContextParentPort is for the parent interface id of child in the context of the event |
| 50 | ContextParentPort ContextType = "parent-port" |
Abhilash Laxmeshwar | c7bfea0 | 2021-11-24 18:50:33 +0530 | [diff] [blame] | 51 | //ContextPonID is the PON ID the Child device is connected to |
| 52 | ContextPonID ContextType = "pon-id" |
| 53 | //ContextOnuID is the Onu ID of the child device |
| 54 | ContextOnuID ContextType = "onu-id" |
| 55 | //ContextParentSerialNum is the serila number of the parent device |
| 56 | ContextParentSerialNum ContextType = "olt-serial-number" |
Mahir Gunyel | b24b9d0 | 2021-05-13 08:58:16 -0700 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | type EventName string |
| 60 | |
| 61 | const ( |
| 62 | DeviceStateChangeEvent EventName = "DEVICE_STATE_CHANGE" |
Abhilash Laxmeshwar | c7bfea0 | 2021-11-24 18:50:33 +0530 | [diff] [blame] | 63 | OltDeviceStateDeleted EventName = "OLT_DELETED_RAISE_EVENT" |
| 64 | OnuDeviceStateDeleted EventName = "ONU_DELETED_RAISE_EVENT" |
Mahir Gunyel | b24b9d0 | 2021-05-13 08:58:16 -0700 | [diff] [blame] | 65 | ) |
| 66 | |
| 67 | type EventAction string |
| 68 | |
| 69 | const ( |
| 70 | Raise EventAction = "RAISE_EVENT" |
| 71 | Clear EventAction = "CLEAR_EVENT" |
| 72 | ) |
| 73 | |
| 74 | //CreateDeviceStateChangeEvent forms and returns a new DeviceStateChange Event |
| 75 | func CreateDeviceStateChangeEvent(serialNumber string, deviceID string, parentID string, |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 76 | prevOperStatus common.OperStatus_Types, prevConnStatus common.ConnectStatus_Types, prevAdminStatus common.AdminState_Types, |
| 77 | operStatus common.OperStatus_Types, connStatus common.ConnectStatus_Types, adminStatus common.AdminState_Types, |
Mahir Gunyel | b24b9d0 | 2021-05-13 08:58:16 -0700 | [diff] [blame] | 78 | parentPort uint32, isRoot bool) *voltha.DeviceEvent { |
| 79 | |
| 80 | context := make(map[string]string) |
| 81 | /* Populating event context */ |
| 82 | context[string(ContextSerialNumber)] = serialNumber |
| 83 | context[string(ContextDeviceID)] = deviceID |
| 84 | context[string(ContextParentID)] = parentID |
| 85 | context[string(ContextPrevOperState)] = prevOperStatus.String() |
| 86 | context[string(ContextPrevConnectState)] = prevConnStatus.String() |
| 87 | context[string(ContextPrevAdminState)] = prevAdminStatus.String() |
| 88 | context[string(ContextOperState)] = operStatus.String() |
| 89 | context[string(ContextConnectState)] = connStatus.String() |
| 90 | context[string(ContextAdminState)] = adminStatus.String() |
| 91 | context[string(ContextIsRoot)] = strconv.FormatBool(isRoot) |
| 92 | context[string(ContextParentPort)] = strconv.FormatUint(uint64(parentPort), 10) |
| 93 | |
| 94 | return &voltha.DeviceEvent{ |
| 95 | Context: context, |
| 96 | ResourceId: deviceID, |
| 97 | DeviceEventName: fmt.Sprintf("%s_%s", string(DeviceStateChangeEvent), string(Raise)), |
| 98 | } |
| 99 | } |
Abhilash Laxmeshwar | c7bfea0 | 2021-11-24 18:50:33 +0530 | [diff] [blame] | 100 | |
| 101 | //CreateDeviceDeletedEvent forms and returns a new DeviceState Event |
| 102 | func CreateDeviceDeletedEvent(serialNumber string, deviceID string, parentID string, |
| 103 | onuId uint32, parentPonPort uint32, isRoot bool) *voltha.DeviceEvent { |
| 104 | |
| 105 | context := make(map[string]string) |
| 106 | eventName := string(OltDeviceStateDeleted) |
| 107 | /* Populating event context */ |
| 108 | context[string(ContextSerialNumber)] = serialNumber |
| 109 | context[string(ContextDeviceID)] = deviceID |
| 110 | if !isRoot { |
| 111 | context[string(ContextPonID)] = strconv.FormatUint(uint64(parentPonPort), 10) |
| 112 | context[string(ContextOnuID)] = strconv.FormatUint(uint64(onuId), 10) |
| 113 | context[string(ContextParentID)] = parentID |
| 114 | eventName = string(OnuDeviceStateDeleted) |
| 115 | |
| 116 | } |
| 117 | |
| 118 | return &voltha.DeviceEvent{ |
| 119 | Context: context, |
| 120 | ResourceId: deviceID, |
| 121 | DeviceEventName: eventName, |
| 122 | } |
| 123 | } |