/
util.go
executable file
·106 lines (97 loc) · 2.54 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package util
import (
"encoding/json"
"fmt"
"crypto/tls"
"github.com/wsilvad/nwdaf/model"
"github.com/free5gc/openapi/models"
"golang.org/x/net/http2"
"net/http"
"net"
"os"
)
func GetConfiguration()model.Config{
file, fail := os.Open("config/config.json")
if fail != nil {
panic(fail.Error())
}
defer file.Close()
decoder := json.NewDecoder(file)
configuration := model.Config{}
err := decoder.Decode(&configuration)
if err != nil {
panic(err.Error())
}
return configuration
}
func SearchNFServiceUri(nfProfile models.NfProfile, serviceName models.ServiceName,
nfServiceStatus models.NfServiceStatus) (nfUri string, endpoint string, apiVersion string) {
if nfProfile.NfServices != nil {
for _, service := range *nfProfile.NfServices {
if service.ServiceName == serviceName && service.NfServiceStatus == nfServiceStatus {
if nfProfile.Fqdn != "" {
nfUri = nfProfile.Fqdn
} else if service.Fqdn != "" {
nfUri = service.Fqdn
} else if service.ApiPrefix != "" {
nfUri = service.ApiPrefix
} else if service.IpEndPoints != nil {
point := (*service.IpEndPoints)[0]
if point.Ipv4Address != "" {
nfUri = getSbiUri(service.Scheme, point.Ipv4Address, point.Port)
} else if len(nfProfile.Ipv4Addresses) != 0 {
nfUri = getSbiUri(service.Scheme, nfProfile.Ipv4Addresses[0], point.Port)
}
}
}
if nfUri != "" {
endpoint = string(service.ServiceName)
apiVersion = getApiVersion(service.Versions, 1)
break
}
}
}
return
}
func getApiVersion(versions *[]models.NfServiceVersion, position int) (version string) {
for _, v := range *versions {
version = v.ApiVersionInUri
break
}
return
}
func getSbiUri(scheme models.UriScheme, ipv4Address string, port int32) (uri string) {
if port != 0 {
uri = fmt.Sprintf("%s://%s:%d", scheme, ipv4Address, port)
} else {
switch scheme {
case models.UriScheme_HTTP:
uri = fmt.Sprintf("%s://%s:80", scheme, ipv4Address)
case models.UriScheme_HTTPS:
uri = fmt.Sprintf("%s://%s:443", scheme, ipv4Address)
}
}
return
}
func GetHttpConnection()(http.Client){
client := http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
},
}
return client
}
func GetServerPort()string{
ConfigPort := GetConfiguration().Port
return fmt.Sprintf("%s%d", ":", ConfigPort)
}
func GetMongoDBUri()string{
return GetConfiguration().MongoURI
}
func GetDBName()string{
dbName := GetConfiguration().DBName
return dbName
}