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