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