routing manager version 0.3.2
[ric-plt/rtmgr.git] / pkg / nbi / nbi.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:     nbi.go
21   Abstract:     Contains NBI (NorthBound Interface) module definitions and generic NBI components
22   Date:         12 March 2019
23 */
24
25 package nbi
26
27 import (
28         "errors"
29         "routing-manager/pkg/rtmgr"
30         "net/url"
31         apiclient "routing-manager/pkg/appmgr_client"
32         "routing-manager/pkg/appmgr_client/operations"
33         "routing-manager/pkg/appmgr_model"
34         httptransport "github.com/go-openapi/runtime/client"
35         "github.com/go-openapi/strfmt"
36         "github.com/go-openapi/swag"
37         "time"
38
39 )
40
41 var (
42         SupportedNbis = []*NbiEngineConfig{
43                 &NbiEngineConfig{
44                         Name:     "httpGetter",
45                         Version:  "v1",
46                         Protocol: "http",
47                         Instance: NewHttpGetter(),
48                         IsAvailable: true,
49                 },
50                 &NbiEngineConfig{
51                         Name:     "httpRESTful",
52                         Version:  "v1",
53                         Protocol: "http",
54                         Instance: NewHttpRestful(),
55                         IsAvailable: true,
56                 },
57         }
58 )
59
60 type Nbi struct {
61
62 }
63
64 func GetNbi(nbiName string) (NbiEngine, error) {
65         for _, nbi := range SupportedNbis {
66                 if nbi.Name == nbiName && nbi.IsAvailable {
67                         return nbi.Instance, nil
68                 }
69         }
70         return nil, errors.New("NBI:" + nbiName + " is not supported or still not a available")
71 }
72
73 func CreateSubReq(restUrl string, restPort string) *appmgr_model.SubscriptionRequest {
74         // TODO: parametize function
75         subReq := appmgr_model.SubscriptionRequest{
76                 TargetURL:  swag.String(restUrl + ":" + restPort + "/ric/v1/handles/xapp-handle/"),
77                 EventType:  swag.String("all"),
78                 MaxRetries: swag.Int64(5),
79                 RetryTimer: swag.Int64(10),
80         }
81
82         return &subReq
83 }
84
85 func PostSubReq(xmUrl string, nbiif string) error {
86         // setting up POST request to Xapp Manager
87         appmgrUrl, err := url.Parse(xmUrl)
88         if err != nil {
89                 rtmgr.Logger.Error("Invalid XApp manager url/hostname: " + err.Error())
90                 return err
91         }
92         nbiifUrl, err := url.Parse(nbiif)
93         if err != nil {
94                 rtmgr.Logger.Error("Invalid NBI address/port: " + err.Error())
95                 return err
96         }
97         transport := httptransport.New(appmgrUrl.Hostname()+":"+appmgrUrl.Port(), "/ric/v1", []string{"http"})
98         client := apiclient.New(transport, strfmt.Default)
99         addSubParams := operations.NewAddSubscriptionParamsWithTimeout(10 * time.Second)
100         // create sub req with rest url and port
101         subReq := CreateSubReq(string(nbiifUrl.Scheme+"://"+nbiifUrl.Hostname()), nbiifUrl.Port())
102         resp, postErr := client.Operations.AddSubscription(addSubParams.WithSubscriptionRequest(subReq))
103         if postErr != nil {
104                 rtmgr.Logger.Error("POST unsuccessful:"+postErr.Error())
105                 return postErr
106         } else {
107                 // TODO: use the received ID
108                 rtmgr.Logger.Info("POST received: "+string(resp.Payload.ID))
109                 return nil
110         }
111 }
112