0bf05bca3ce1ebebca8a2d1046a3c2c48f5d7093
[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         "net/http"
36         "routing-manager/pkg/rpe"
37         "routing-manager/pkg/rtmgr"
38         "routing-manager/pkg/sdl"
39         "time"
40 )
41
42 type HttpGetter struct {
43         Engine
44         FetchAllXApps FetchAllXAppsHandler
45 }
46
47 func NewHttpGetter() *HttpGetter {
48         instance := new(HttpGetter)
49         instance.FetchAllXApps = fetchAllXApps
50         return instance
51 }
52
53 var myClient = &http.Client{Timeout: 5 * time.Second}
54
55 func fetchAllXApps(xmurl string) (*[]rtmgr.XApp, error) {
56         rtmgr.Logger.Info("Invoked httpGetter.fetchXappList: " + xmurl)
57         r, err := myClient.Get(xmurl)
58         if err != nil {
59                 return nil, err
60         }
61         defer r.Body.Close()
62
63         if r.StatusCode == 200 {
64                 rtmgr.Logger.Debug("http client raw response: %v", r)
65                 var xApps []rtmgr.XApp
66                 err = json.NewDecoder(r.Body).Decode(&xApps)
67                 if err != nil {
68                         rtmgr.Logger.Warn("Json decode failed: " + err.Error())
69                 }
70                 rtmgr.Logger.Info("HTTP GET: OK")
71                 rtmgr.Logger.Debug("httpGetter.fetchXappList returns: %v", xApps)
72                 return &xApps, err
73         }
74         rtmgr.Logger.Warn("httpGetter got an unexpected http status code: %v", r.StatusCode)
75         return nil, nil
76 }
77
78 func (g *HttpGetter) Initialize(xmurl string, nbiif string, fileName string, configfile string,
79         sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool) error {
80         return nil
81 }
82
83 func (g *HttpGetter) Terminate() error {
84         return nil
85 }