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