blob: 5d7b81884a5e7cc18acdb42a36a3e988beef63b4 [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
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.
wahidw761934a2019-11-27 06:07:26 +000017
18
19 This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 platform project (RICP).
21
Balint Uveges871fa392019-04-02 20:31:11 +000022==================================================================================
23*/
24/*
25 Mnemonic: httpgetter.go
26 Abstract: HTTPgetter NBI implementation
zkoczkaeb2ff0d2019-09-26 16:59:54 +020027 Simple HTTP getter solution.
Balint Uveges871fa392019-04-02 20:31:11 +000028 Date: 15 March 2019
29*/
30
31package nbi
32
33import (
34 "encoding/json"
wahidwa8596ec2019-12-05 06:30:42 +000035 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Balint Uveges871fa392019-04-02 20:31:11 +000036 "net/http"
kalnagy92162652019-07-02 15:15:49 +020037 "routing-manager/pkg/rpe"
zkoczkaeb2ff0d2019-09-26 16:59:54 +020038 "routing-manager/pkg/rtmgr"
kalnagy92162652019-07-02 15:15:49 +020039 "routing-manager/pkg/sdl"
Balint Uveges871fa392019-04-02 20:31:11 +000040 "time"
prabhukaliswamye110ee02019-12-23 09:51:01 +000041 "sync"
Balint Uveges871fa392019-04-02 20:31:11 +000042)
43
kalnagy92162652019-07-02 15:15:49 +020044type HttpGetter struct {
zkoczkaaaf8d392019-10-02 17:16:06 +020045 Engine
46 FetchAllXApps FetchAllXAppsHandler
kalnagy92162652019-07-02 15:15:49 +020047}
Balint Uveges871fa392019-04-02 20:31:11 +000048
kalnagy92162652019-07-02 15:15:49 +020049func NewHttpGetter() *HttpGetter {
50 instance := new(HttpGetter)
zkoczkaaaf8d392019-10-02 17:16:06 +020051 instance.FetchAllXApps = fetchAllXApps
kalnagy92162652019-07-02 15:15:49 +020052 return instance
53}
54
55var myClient = &http.Client{Timeout: 5 * time.Second}
56
zkoczkaaaf8d392019-10-02 17:16:06 +020057func fetchAllXApps(xmurl string) (*[]rtmgr.XApp, error) {
wahidwa8596ec2019-12-05 06:30:42 +000058 xapp.Logger.Info("Invoked httpGetter.fetchXappList: " + xmurl)
kalnagy92162652019-07-02 15:15:49 +020059 r, err := myClient.Get(xmurl)
Balint Uveges871fa392019-04-02 20:31:11 +000060 if err != nil {
61 return nil, err
62 }
63 defer r.Body.Close()
kalnagy92162652019-07-02 15:15:49 +020064
65 if r.StatusCode == 200 {
wahidwa8596ec2019-12-05 06:30:42 +000066 xapp.Logger.Debug("http client raw response: %v", r)
zkoczkaaaf8d392019-10-02 17:16:06 +020067 var xApps []rtmgr.XApp
68 err = json.NewDecoder(r.Body).Decode(&xApps)
kalnagy92162652019-07-02 15:15:49 +020069 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +000070 xapp.Logger.Warn("Json decode failed: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020071 }
wahidwa8596ec2019-12-05 06:30:42 +000072 xapp.Logger.Info("HTTP GET: OK")
73 xapp.Logger.Debug("httpGetter.fetchXappList returns: %v", xApps)
zkoczkaaaf8d392019-10-02 17:16:06 +020074 return &xApps, err
kalnagy92162652019-07-02 15:15:49 +020075 }
wahidwa8596ec2019-12-05 06:30:42 +000076 xapp.Logger.Warn("httpGetter got an unexpected http status code: %v", r.StatusCode)
kalnagy92162652019-07-02 15:15:49 +020077 return nil, nil
78}
79
80func (g *HttpGetter) Initialize(xmurl string, nbiif string, fileName string, configfile string,
prabhukaliswamye110ee02019-12-23 09:51:01 +000081 sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool, m *sync.Mutex) error {
kalnagy92162652019-07-02 15:15:49 +020082 return nil
83}
84
85func (g *HttpGetter) Terminate() error {
86 return nil
Balint Uveges871fa392019-04-02 20:31:11 +000087}