blob: 634272cc4d17443963ab6def9e95010234905a74 [file] [log] [blame]
Elia Battistone1cecb22022-03-21 10:05:25 +01001/*
2* Copyright 2022-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
17package core
18
19import (
20 "fmt"
21
Elia Battiston589addb2022-04-04 16:40:01 +020022 "github.com/opencord/voltha-protos/v5/go/common"
Elia Battistone1cecb22022-03-21 10:05:25 +010023 "github.com/opencord/voltha-protos/v5/go/voltha"
24)
25
26const (
27 DeviceAggregationModel = "bbf-device-aggregation"
28 DevicesPath = "/" + DeviceAggregationModel + ":devices"
Elia Battiston589addb2022-04-04 16:40:01 +020029 HardwarePath = "data/ietf-hardware:hardware"
30
31 //Device types
32 DeviceTypeOlt = "bbf-device-types:olt"
33 DeviceTypeOnu = "bbf-device-types:onu"
34
35 //Admin states
36 ietfAdminStateUnknown = "unknown"
37 ietfAdminStateLocked = "locked"
38 ietfAdminStateUnlocked = "unlocked"
39
40 //Oper states
41 ietfOperStateUnknown = "unknown"
42 ietfOperStateDisabled = "disabled"
43 ietfOperStateEnabled = "enabled"
44 ietfOperStateTesting = "testing"
Elia Battistone1cecb22022-03-21 10:05:25 +010045)
46
47type YangItem struct {
48 Path string
49 Value string
50}
51
52//getDevicePath returns the yang path to the root of the device with a specific ID
53func getDevicePath(id string) string {
54 return fmt.Sprintf("%s/device[name='%s']", DevicesPath, id)
55}
56
Elia Battiston589addb2022-04-04 16:40:01 +020057//getDevicePath returns the yang path to the root of the device's hardware module in its data mountpoint
58func getDeviceHardwarePath(id string) string {
59 return fmt.Sprintf("%s/device[name='%s']/%s/component[name='%s']", DevicesPath, id, HardwarePath, id)
60}
61
62//ietfHardwareAdminState returns the string that represents the ietf-hardware admin state
63//enum value corresponding to the one of VOLTHA
64func ietfHardwareAdminState(volthaAdminState voltha.AdminState_Types) string {
65 //TODO: verify this mapping is correct
66 switch volthaAdminState {
67 case common.AdminState_UNKNOWN:
68 return ietfAdminStateUnknown
69 case common.AdminState_PREPROVISIONED:
70 case common.AdminState_DOWNLOADING_IMAGE:
71 case common.AdminState_ENABLED:
72 return ietfAdminStateUnlocked
73 case common.AdminState_DISABLED:
74 return ietfAdminStateLocked
75 }
76
77 //TODO: does something map to "shutting-down" ?
78
79 return ietfAdminStateUnknown
80}
81
82//ietfHardwareOperState returns the string that represents the ietf-hardware oper state
83//enum value corresponding to the one of VOLTHA
84func ietfHardwareOperState(volthaOperState voltha.OperStatus_Types) string {
85 //TODO: verify this mapping is correct
86 switch volthaOperState {
87 case common.OperStatus_UNKNOWN:
88 return ietfOperStateUnknown
89 case common.OperStatus_TESTING:
90 return ietfOperStateTesting
91 case common.OperStatus_ACTIVE:
92 return ietfOperStateEnabled
93 case common.OperStatus_DISCOVERED:
94 case common.OperStatus_ACTIVATING:
95 case common.OperStatus_FAILED:
96 case common.OperStatus_RECONCILING:
97 case common.OperStatus_RECONCILING_FAILED:
98 return ietfOperStateDisabled
99 }
100
101 return ietfOperStateUnknown
102}
103
Elia Battistone1cecb22022-03-21 10:05:25 +0100104//translateDevice returns a slice of yang items that represent a voltha device
105func translateDevice(device voltha.Device) []YangItem {
106 devicePath := getDevicePath(device.Id)
Elia Battiston589addb2022-04-04 16:40:01 +0200107 hardwarePath := getDeviceHardwarePath(device.Id)
Elia Battistone1cecb22022-03-21 10:05:25 +0100108
Elia Battiston589addb2022-04-04 16:40:01 +0200109 result := []YangItem{}
Elia Battistone1cecb22022-03-21 10:05:25 +0100110
Elia Battiston589addb2022-04-04 16:40:01 +0200111 //Device type
Elia Battistone1cecb22022-03-21 10:05:25 +0100112 if device.Root {
Elia Battiston589addb2022-04-04 16:40:01 +0200113 result = append(result, YangItem{
114 Path: fmt.Sprintf("%s/type", devicePath),
115 Value: DeviceTypeOlt,
116 })
Elia Battistone1cecb22022-03-21 10:05:25 +0100117 } else {
Elia Battiston589addb2022-04-04 16:40:01 +0200118 result = append(result, YangItem{
119 Path: fmt.Sprintf("%s/type", devicePath),
120 Value: DeviceTypeOnu,
121 })
Elia Battistone1cecb22022-03-21 10:05:25 +0100122 }
123
Elia Battiston589addb2022-04-04 16:40:01 +0200124 //Vendor name
125 result = append(result, YangItem{
126 Path: fmt.Sprintf("%s/mfg-name", hardwarePath),
127 Value: device.Vendor,
128 })
129
130 //Model
131 result = append(result, YangItem{
132 Path: fmt.Sprintf("%s/model-name", hardwarePath),
133 Value: device.Model,
134 })
135
136 //Hardware version
137 result = append(result, YangItem{
138 Path: fmt.Sprintf("%s/hardware-rev", hardwarePath),
139 Value: device.HardwareVersion,
140 })
141
142 //Firmware version
143 result = append(result, YangItem{
144 Path: fmt.Sprintf("%s/firmware-rev", hardwarePath),
145 Value: device.FirmwareVersion,
146 })
147
148 //Serial number
149 result = append(result, YangItem{
150 Path: fmt.Sprintf("%s/serial-num", hardwarePath),
151 Value: device.SerialNumber,
152 })
153
154 //Administrative state
155 //Translates VOLTHA admin state enum to ietf-hardware enum
156 result = append(result, YangItem{
157 Path: fmt.Sprintf("%s/state/admin-state", hardwarePath),
158 Value: ietfHardwareAdminState(device.AdminState),
159 })
160
161 //Operative state
162 result = append(result, YangItem{
163 Path: fmt.Sprintf("%s/state/oper-state", hardwarePath),
164 Value: ietfHardwareOperState(device.OperStatus),
165 })
166
167 return result
Elia Battistone1cecb22022-03-21 10:05:25 +0100168}
169
170//translateDevices returns a slice of yang items that represent a list of voltha devices
171func translateDevices(devices voltha.Devices) []YangItem {
172 result := []YangItem{}
173
174 for _, device := range devices.Items {
175 result = append(result, translateDevice(*device)...)
176 }
177
178 return result
179}