blob: bb6f58a314e505fb65271329c6ed98bc3ff39a13 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
2 * Copyright 2019-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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidooab1f7bd2019-11-14 14:00:27 -050017package mocks
18
19import (
20 "context"
21 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "strings"
23
khenaidooab1f7bd2019-11-14 14:00:27 -050024 "github.com/gogo/protobuf/proto"
25 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
26 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
27 "github.com/opencord/voltha-lib-go/v2/pkg/log"
28 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
29 of "github.com/opencord/voltha-protos/v2/go/openflow_13"
30 "github.com/opencord/voltha-protos/v2/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
32
npujar1d86a522019-11-14 17:11:16 +053033// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050034type ONUAdapter struct {
35 coreProxy adapterif.CoreProxy
36 Adapter
37}
38
npujar1d86a522019-11-14 17:11:16 +053039// NewONUAdapter creates ONU adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050040func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
41 a := &ONUAdapter{}
42 a.coreProxy = cp
43 return a
44}
45
npujar1d86a522019-11-14 17:11:16 +053046// Adopt_device creates new handler for added device
47func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050048 go func() {
49 d := proto.Clone(device).(*voltha.Device)
50 d.Root = false
51 d.Vendor = "onu_adapter_mock"
52 d.Model = "go-mock"
53 d.SerialNumber = com.GetRandomSerialNumber()
54 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
55 onuA.storeDevice(d)
56 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
57 log.Fatalf("deviceUpdate-failed-%s", res)
58 }
59 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
60 d.OperStatus = voltha.OperStatus_DISCOVERED
61
62 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
63 log.Fatalf("device-state-update-failed-%s", err)
64 }
65
66 uniPortNo := uint32(2)
67 if device.ProxyAddress != nil {
68 if device.ProxyAddress.ChannelId != 0 {
69 uniPortNo = device.ProxyAddress.ChannelId
70 }
71 }
72
73 uniPort := &voltha.Port{
74 PortNo: uniPortNo,
75 Label: fmt.Sprintf("uni-%d", uniPortNo),
76 Type: voltha.Port_ETHERNET_UNI,
77 OperStatus: voltha.OperStatus_ACTIVE,
78 }
79 var err error
80 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
81 log.Fatalf("PortCreated-failed-%s", err)
82 }
83
84 ponPortNo := uint32(1)
85 if device.ParentPortNo != 0 {
86 ponPortNo = device.ParentPortNo
87 }
88
89 ponPort := &voltha.Port{
90 PortNo: ponPortNo,
91 Label: fmt.Sprintf("pon-%d", ponPortNo),
92 Type: voltha.Port_PON_ONU,
93 OperStatus: voltha.OperStatus_ACTIVE,
94 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
95 PortNo: uniPortNo}}, // Peer port is UNI port
96 }
npujar1d86a522019-11-14 17:11:16 +053097 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -050098 log.Fatalf("PortCreated-failed-%s", err)
99 }
100
101 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
102 d.OperStatus = voltha.OperStatus_ACTIVE
103
104 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
105 log.Fatalf("device-state-update-failed-%s", err)
106 }
107 //Get the latest device data from the Core
108 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
109 log.Fatalf("getting-device-failed-%s", err)
110 }
111
112 if err = onuA.updateDevice(d); err != nil {
113 log.Fatalf("saving-device-failed-%s", err)
114 }
115 }()
116 return nil
117}
118
npujar1d86a522019-11-14 17:11:16 +0530119// Get_ofp_port_info returns ofp device info
120func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 if d := onuA.getDevice(device.Id); d == nil {
122 log.Fatalf("device-not-found-%s", device.Id)
123 }
124 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
125 return &ic.PortCapability{
126 Port: &voltha.LogicalPort{
127 OfpPort: &of.OfpPort{
128 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
129 Config: 0,
130 State: uint32(of.OfpPortState_OFPPS_LIVE),
131 Curr: capability,
132 Advertised: capability,
133 Peer: capability,
134 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
135 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
136 },
137 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530138 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500139 },
140 }, nil
141}
142
npujar1d86a522019-11-14 17:11:16 +0530143// Disable_device disables device
144func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 go func() {
146 if d := onuA.getDevice(device.Id); d == nil {
147 log.Fatalf("device-not-found-%s", device.Id)
148 }
149 cloned := proto.Clone(device).(*voltha.Device)
150 // Update the all ports state on that device to disable
151 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
152 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
153 }
154 //Update the device state
155 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
156 cloned.OperStatus = voltha.OperStatus_UNKNOWN
157
158 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
159 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
160 }
161 if err := onuA.updateDevice(cloned); err != nil {
162 log.Fatalf("saving-device-failed-%s", err)
163 }
164 }()
165 return nil
166}
167
npujar1d86a522019-11-14 17:11:16 +0530168// Reenable_device reenables device
169func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500170 go func() {
171 if d := onuA.getDevice(device.Id); d == nil {
172 log.Fatalf("device-not-found-%s", device.Id)
173 }
174
175 cloned := proto.Clone(device).(*voltha.Device)
176 // Update the all ports state on that device to enable
177 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
178 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
179 }
180
181 //Update the device state
182 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
183 cloned.OperStatus = voltha.OperStatus_ACTIVE
184
185 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
186 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
187 }
188 if err := onuA.updateDevice(cloned); err != nil {
189 log.Fatalf("saving-device-failed-%s", err)
190 }
191 }()
192 return nil
193}