blob: 4bfafa58436b4b3697ecd5333f36cede447ca287 [file] [log] [blame]
khenaidooabad44c2018-08-03 16:58:35 -04001package core
2
3import (
4 "github.com/opencord/voltha-go/common/log"
5 ca "github.com/opencord/voltha-go/protos/core_adapter"
6 "github.com/opencord/voltha-go/protos/voltha"
7 "github.com/golang/protobuf/ptypes"
8 "errors"
9)
10
11type RequestHandlerProxy struct {
12 TestMode bool
13}
14
15func (rhp *RequestHandlerProxy) GetDevice(args []*ca.Argument)(error, *voltha.Device) {
16 if len(args) != 1 {
17 log.Warn("invalid-number-of-args", log.Fields{"args": args})
18 err := errors.New("invalid-number-of-args")
19 return err, nil
20 }
21 pID := &ca.StrType{}
22 if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
23 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
24 return err, nil
25 }
26 log.Debugw("GetDevice", log.Fields{"deviceId": pID.Val})
27 // TODO process the request
28
29 if rhp.TestMode { // Execute only for test cases
30 return nil, &voltha.Device{Id: pID.Val}
31 }
32 return nil, nil
33}
34
35
36func (rhp *RequestHandlerProxy) GetChildDevice(args []*ca.Argument)(error, *voltha.Device) {
37 if len(args) < 1 {
38 log.Warn("invalid-number-of-args", log.Fields{"args": args})
39 err := errors.New("invalid-number-of-args")
40 return err, nil
41 }
42 pID := &ca.StrType{}
43 if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
44 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
45 return err, nil
46 }
47 // TODO decompose the other parameteres for matching criteria and process
48 log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Val})
49
50 if rhp.TestMode { // Execute only for test cases
51 return nil, &voltha.Device{Id: pID.Val}
52 }
53 return nil, nil
54}
55
56func (rhp *RequestHandlerProxy) GetPorts(args []*ca.Argument)(error, *voltha.Ports) {
57 if len(args) != 2 {
58 log.Warn("invalid-number-of-args", log.Fields{"args": args})
59 err := errors.New("invalid-number-of-args")
60 return err, nil
61 }
62 pID := &ca.StrType{}
63 if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
64 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
65 return err, nil
66 }
67 // Porttype is an enum sent as an integer proto
68 pt := &ca.IntType{}
69 if err := ptypes.UnmarshalAny(args[1].Value, pt); err != nil {
70 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
71 return err, nil
72 }
73
74 // TODO decompose the other parameteres for matching criteria
75 log.Debugw("GetPorts", log.Fields{"deviceID": pID.Val, "portype": pt.Val})
76
77 if rhp.TestMode { // Execute only for test cases
78 aPort := &voltha.Port{Label:"test_port"}
79 allPorts := &voltha.Ports{}
80 allPorts.Items = append(allPorts.Items, aPort)
81 return nil, allPorts
82 }
83 return nil, nil
84
85}
86
87func (rhp *RequestHandlerProxy) GetChildDevices(args []*ca.Argument)(error, *voltha.Device) {
88 if len(args) != 1 {
89 log.Warn("invalid-number-of-args", log.Fields{"args": args})
90 err := errors.New("invalid-number-of-args")
91 return err, nil
92 }
93 pID := &ca.StrType{}
94 if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
95 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
96 return err, nil
97 }
98 // TODO decompose the other parameteres for matching criteria and process
99 log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Val})
100
101 if rhp.TestMode { // Execute only for test cases
102 return nil, &voltha.Device{Id: pID.Val}
103 }
104 return nil, nil
105}
106
107// ChildDeviceDetected is invoked when a child device is detected. The following
108// parameters are expected:
109// {parent_device_id, parent_port_no, child_device_type, proxy_address, admin_state, **kw)
110func (rhp *RequestHandlerProxy) ChildDeviceDetected(args []*ca.Argument) (error) {
111 if len(args) < 5 {
112 log.Warn("invalid-number-of-args", log.Fields{"args": args})
113 err := errors.New("invalid-number-of-args")
114 return err
115 }
116
117 pID := &ca.StrType{}
118 if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
119 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
120 return err
121 }
122 portNo := &ca.IntType{}
123 if err := ptypes.UnmarshalAny(args[1].Value, portNo); err != nil {
124 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
125 return err
126 }
127 dt := &ca.StrType{}
128 if err := ptypes.UnmarshalAny(args[2].Value, dt); err != nil {
129 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
130 return err
131 }
132 pAddr := &voltha.Device_ProxyAddress{}
133 if err := ptypes.UnmarshalAny(args[3].Value, pAddr); err != nil {
134 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
135 return err
136 }
137 adminState := &ca.IntType{}
138 if err := ptypes.UnmarshalAny(args[4].Value, adminState); err != nil {
139 log.Warnw("cannot-unmarshal-argument", log.Fields{"error": err})
140 return err
141 }
142
143 // Need to decode the other params - in this case the key will represent the proto type
144 // TODO decompose the other parameteres for matching criteria and process
145 log.Debugw("ChildDeviceDetected", log.Fields{"deviceId": pID.Val, "portNo":portNo.Val,
146 "deviceType": dt.Val, "proxyAddress": pAddr, "adminState": adminState})
147
148 if rhp.TestMode { // Execute only for test cases
149 return nil
150 }
151 return nil
152}
153
154