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