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