2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 type testingControl struct {
38 syncChan chan struct{}
41 func (tc *testingControl) ReadyCB(data interface{}) {
42 xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc)
43 tc.syncChan <- struct{}{}
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 type testingRmrControl struct {
52 rmrClientTest *xapp.RMRClient
53 rmrConChan chan *xapp.RMRParams
56 func (tc *testingRmrControl) Consume(msg *xapp.RMRParams) (err error) {
57 xapp.Logger.Info("testingRmrControl(%s) Consume", tc.desc)
62 func (tc *testingRmrControl) RmrSend(params *xapp.RMRParams) (err error) {
64 //NOTE: Do this way until xapp-frame sending is improved
68 for ; i <= 10 && status == false; i++ {
69 status = tc.rmrClientTest.SendMsg(params)
71 xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s", i, params.Mtype, params.SubId, params.Xid)
72 time.Sleep(500 * time.Millisecond)
76 err = errors.New("rmr.Send() failed")
77 tc.rmrClientTest.Free(params.Mbuf)
82 func createNewRmrControl(desc string, rtfile string, port string, stat string) *testingRmrControl {
83 os.Setenv("RMR_SEED_RT", rtfile)
84 os.Setenv("RMR_SRC_ID", "localhost:"+port)
85 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
86 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
87 newConn := &testingRmrControl{}
89 newConn.syncChan = make(chan struct{})
90 newConn.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
91 newConn.rmrConChan = make(chan *xapp.RMRParams)
92 newConn.rmrClientTest.SetReadyCB(newConn.ReadyCB, nil)
93 go newConn.rmrClientTest.Start(newConn)
98 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
102 func testError(t *testing.T, pattern string, args ...interface{}) {
103 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
104 t.Errorf(fmt.Sprintf(pattern, args...))
107 func testCreateTmpFile(str string) (string, error) {
108 file, err := ioutil.TempFile("/tmp", "*.rt")
112 _, err = file.WriteString(str)
117 return file.Name(), nil
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 var xappConn *testingRmrControl
125 var e2termConn *testingRmrControl
127 func TestMain(m *testing.M) {
128 xapp.Logger.Info("TestMain start")
131 //Cfg creation won't work like this as xapp-frame reads it during init.
142 "protPort": "tcp:14560",
145 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
146 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
151 "namespaces": ["sdl", "rnib"]
154 "HostAddr" : "localhost",
160 cfgfilename,_ := testCreateTmpFile(cfgstr)
161 defer os.Remove(cfgfilename)
162 os.Setenv("CFG_FILE", cfgfilename)
164 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
166 //---------------------------------
168 //---------------------------------
169 xapp.Logger.Info("### submgr main run ###")
171 subsrt := `newrt|start
172 mse|12010|-1|localhost:14560
173 mse|12010,localhost:14560|-1|localhost:15560
174 mse|12011,localhost:15560|-1|localhost:14560
175 mse|12011|-1|localhost:13560
176 mse|12012,localhost:15560|-1|localhost:14560
177 mse|12012|-1|localhost:13560
178 mse|12020|-1|localhost:14560
179 mse|12020,localhost:14560|-1|localhost:15560
180 mse|12021,localhost:15560|-1|localhost:14560
181 mse|12021|-1|localhost:13560
182 mse|12022,localhost:15560|-1|localhost:14560
183 mse|12022|-1|localhost:13560
187 subrtfilename, _ := testCreateTmpFile(subsrt)
188 defer os.Remove(subrtfilename)
189 os.Setenv("RMR_SEED_RT", subrtfilename)
190 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
192 mainCtrl := &testingControl{}
193 mainCtrl.desc = "main"
194 mainCtrl.syncChan = make(chan struct{})
196 os.Setenv("RMR_SRC_ID", "localhost:14560")
198 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
199 go xapp.RunWithParams(c, false)
202 //---------------------------------
204 //---------------------------------
205 xapp.Logger.Info("### xapp rmr run ###")
207 xapprt := `newrt|start
208 mse|12010|-1|localhost:14560
209 mse|12011|-1|localhost:13560
210 mse|12012|-1|localhost:13560
211 mse|12020|-1|localhost:14560
212 mse|12021|-1|localhost:13560
213 mse|12022|-1|localhost:13560
217 xapprtfilename, _ := testCreateTmpFile(xapprt)
218 defer os.Remove(xapprtfilename)
219 xappConn = createNewRmrControl("xappConn", xapprtfilename, "13560", "RMRXAPPSTUB")
221 //---------------------------------
223 //---------------------------------
224 xapp.Logger.Info("### e2term rmr run ###")
226 e2termrt := `newrt|start
227 mse|12010|-1|localhost:15560
228 mse|12011|-1|localhost:14560
229 mse|12012|-1|localhost:14560
230 mse|12020|-1|localhost:15560
231 mse|12021|-1|localhost:14560
232 mse|12022|-1|localhost:14560
236 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
237 defer os.Remove(e2termrtfilename)
238 e2termConn = createNewRmrControl("e2termConn", e2termrtfilename, "15560", "RMRE2TERMSTUB")
240 //---------------------------------
242 //---------------------------------
243 http_handler := func(w http.ResponseWriter, r *http.Request) {
244 xapp.Logger.Info("(http handler) handling")
249 http.HandleFunc("/", http_handler)
250 http.ListenAndServe("localhost:8989", nil)
253 //---------------------------------
255 //---------------------------------