Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -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 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltha-go/common/log" |
| 20 | "github.com/opencord/voltha-go/protos/common" |
| 21 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 22 | "github.com/opencord/voltha-go/protos/voltha" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 23 | "sync" |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type ModelTestConfig struct { |
| 27 | Root *root |
| 28 | Backend *Backend |
| 29 | RootProxy *Proxy |
| 30 | DbPrefix string |
| 31 | DbType string |
| 32 | DbHost string |
| 33 | DbPort int |
| 34 | DbTimeout int |
| 35 | } |
| 36 | |
| 37 | var ( |
| 38 | modelTestConfig = &ModelTestConfig{ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | DbPrefix: "service/voltha/data/core/0001", |
| 40 | DbType: "etcd", |
| 41 | DbHost: "localhost", |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 42 | //DbHost: "10.106.153.44", |
| 43 | DbPort: 2379, |
| 44 | DbTimeout: 5, |
| 45 | } |
| 46 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 47 | logports = []*voltha.LogicalPort{ |
| 48 | { |
| 49 | Id: "123", |
| 50 | DeviceId: "logicalport-0-device-id", |
| 51 | DevicePortNo: 123, |
| 52 | RootPort: false, |
| 53 | }, |
| 54 | } |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 55 | ports = []*voltha.Port{ |
| 56 | { |
| 57 | PortNo: 123, |
| 58 | Label: "test-port-0", |
| 59 | Type: voltha.Port_PON_OLT, |
| 60 | AdminState: common.AdminState_ENABLED, |
| 61 | OperStatus: common.OperStatus_ACTIVE, |
| 62 | DeviceId: "etcd_port-0-device-id", |
| 63 | Peers: []*voltha.Port_PeerPort{}, |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | stats = &openflow_13.OfpFlowStats{ |
| 68 | Id: 1111, |
| 69 | } |
| 70 | flows = &openflow_13.Flows{ |
| 71 | Items: []*openflow_13.OfpFlowStats{stats}, |
| 72 | } |
| 73 | device = &voltha.Device{ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 74 | Id: devID, |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 75 | Type: "simulated_olt", |
| 76 | Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"}, |
| 77 | AdminState: voltha.AdminState_PREPROVISIONED, |
| 78 | Flows: flows, |
| 79 | Ports: ports, |
| 80 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | |
| 82 | logicalDevice = &voltha.LogicalDevice{ |
| 83 | Id: devID, |
| 84 | DatapathId: 0, |
| 85 | Ports: logports, |
| 86 | Flows: flows, |
| 87 | } |
| 88 | |
| 89 | devID string |
| 90 | ldevID string |
| 91 | targetDevID string |
| 92 | targetLogDevID string |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 93 | ) |
| 94 | |
| 95 | func init() { |
| 96 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 97 | log.UpdateAllLoggers(log.Fields{"instanceId": "MODEL_TEST"}) |
| 98 | |
| 99 | defer log.CleanUp() |
| 100 | |
| 101 | modelTestConfig.Backend = NewBackend( |
| 102 | modelTestConfig.DbType, |
| 103 | modelTestConfig.DbHost, |
| 104 | modelTestConfig.DbPort, |
| 105 | modelTestConfig.DbTimeout, |
| 106 | modelTestConfig.DbPrefix, |
| 107 | ) |
| 108 | |
| 109 | msgClass := &voltha.Voltha{} |
| 110 | root := NewRoot(msgClass, modelTestConfig.Backend) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | //root := NewRoot(msgClass, nil) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 112 | |
| 113 | if modelTestConfig.Backend != nil { |
| 114 | modelTestConfig.Root = root.Load(msgClass) |
| 115 | } else { |
| 116 | modelTestConfig.Root = root |
| 117 | } |
| 118 | |
| 119 | GetProfiling().Report() |
| 120 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 121 | modelTestConfig.RootProxy = modelTestConfig.Root.node.CreateProxy("/", false) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func commonCallback(args ...interface{}) interface{} { |
| 125 | log.Infof("Running common callback - arg count: %s", len(args)) |
| 126 | |
| 127 | for i := 0; i < len(args); i++ { |
| 128 | log.Infof("ARG %d : %+v", i, args[i]) |
| 129 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 130 | |
| 131 | mutex := sync.Mutex{} |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 132 | execStatus := args[1].(*bool) |
| 133 | |
| 134 | // Inform the caller that the callback was executed |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 135 | mutex.Lock() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 136 | *execStatus = true |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 137 | mutex.Unlock() |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | func commonCallback2(args ...interface{}) interface{} { |
| 143 | log.Infof("Running common callback - arg count: %s", len(args)) |
| 144 | |
| 145 | return nil |
| 146 | } |
| 147 | |
| 148 | func commonCallbackFunc(args ...interface{}) interface{} { |
| 149 | log.Infof("Running common callback - arg count: %d", len(args)) |
| 150 | |
| 151 | for i := 0; i < len(args); i++ { |
| 152 | log.Infof("ARG %d : %+v", i, args[i]) |
| 153 | } |
| 154 | execStatusFunc := args[1].(func(bool)) |
| 155 | |
| 156 | // Inform the caller that the callback was executed |
| 157 | execStatusFunc(true) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 158 | |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func firstCallback(args ...interface{}) interface{} { |
| 163 | name := args[0] |
| 164 | id := args[1] |
| 165 | log.Infof("Running first callback - name: %s, id: %s\n", name, id) |
| 166 | return nil |
| 167 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 168 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 169 | func secondCallback(args ...interface{}) interface{} { |
| 170 | name := args[0].(map[string]string) |
| 171 | id := args[1] |
| 172 | log.Infof("Running second callback - name: %s, id: %f\n", name["name"], id) |
| 173 | // FIXME: the panic call seem to interfere with the logging mechanism |
| 174 | //panic("Generating a panic in second callback") |
| 175 | return nil |
| 176 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 177 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 178 | func thirdCallback(args ...interface{}) interface{} { |
| 179 | name := args[0] |
| 180 | id := args[1].(*voltha.Device) |
| 181 | log.Infof("Running third callback - name: %+v, id: %s\n", name, id.Id) |
| 182 | return nil |
| 183 | } |