Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 1 | /* |
| 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. |
wahidw | 761934a | 2019-11-27 06:07:26 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 20 | platform project (RICP). |
| 21 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 22 | ================================================================================== |
| 23 | */ |
| 24 | /* |
| 25 | Mnemonic: httpgetter.go |
| 26 | Abstract: HTTPgetter NBI implementation |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 27 | Simple HTTP getter solution. |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 28 | Date: 15 March 2019 |
| 29 | */ |
| 30 | |
| 31 | package nbi |
| 32 | |
| 33 | import ( |
| 34 | "encoding/json" |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 35 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 36 | "net/http" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 37 | "routing-manager/pkg/rpe" |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 38 | "routing-manager/pkg/rtmgr" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 39 | "routing-manager/pkg/sdl" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 40 | "time" |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame^] | 41 | "sync" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 42 | ) |
| 43 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 44 | type HttpGetter struct { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 45 | Engine |
| 46 | FetchAllXApps FetchAllXAppsHandler |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 47 | } |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 48 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 49 | func NewHttpGetter() *HttpGetter { |
| 50 | instance := new(HttpGetter) |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 51 | instance.FetchAllXApps = fetchAllXApps |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 52 | return instance |
| 53 | } |
| 54 | |
| 55 | var myClient = &http.Client{Timeout: 5 * time.Second} |
| 56 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 57 | func fetchAllXApps(xmurl string) (*[]rtmgr.XApp, error) { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 58 | xapp.Logger.Info("Invoked httpGetter.fetchXappList: " + xmurl) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 59 | r, err := myClient.Get(xmurl) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | defer r.Body.Close() |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 64 | |
| 65 | if r.StatusCode == 200 { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 66 | xapp.Logger.Debug("http client raw response: %v", r) |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 67 | var xApps []rtmgr.XApp |
| 68 | err = json.NewDecoder(r.Body).Decode(&xApps) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 69 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 70 | xapp.Logger.Warn("Json decode failed: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 71 | } |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 72 | xapp.Logger.Info("HTTP GET: OK") |
| 73 | xapp.Logger.Debug("httpGetter.fetchXappList returns: %v", xApps) |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 74 | return &xApps, err |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 75 | } |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 76 | xapp.Logger.Warn("httpGetter got an unexpected http status code: %v", r.StatusCode) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 77 | return nil, nil |
| 78 | } |
| 79 | |
| 80 | func (g *HttpGetter) Initialize(xmurl string, nbiif string, fileName string, configfile string, |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame^] | 81 | sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool, m *sync.Mutex) error { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (g *HttpGetter) Terminate() error { |
| 86 | return nil |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 87 | } |