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