5dd6dc35664c30c650850f7671e2bf059206c6e1
[ealt-edge.git] / mep / mepagent / pkg / service / register.go
1 /*
2  *  Copyright 2020 Huawei Technologies Co., Ltd.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package service
18
19 import (
20         "encoding/json"
21         "log"
22         "os"
23         "strconv"
24         "time"
25 )
26
27 func SvcReg(confPath string) (string, error) {
28     var urlProto string
29         var gwPORT string
30
31         conf, err := GetConf(confPath)
32         if err != nil {
33                 log.Println(err.Error())
34                 return "", err
35         }
36
37         appInstanceId := conf.AppInstanceId
38         serviceInfos := conf.ServiceInfoPosts
39         gwRoutes := conf.MepGWROUTES
40         gwIP := conf.MepGWIP
41
42         sslMode := os.Getenv("APP_SSL_MODE")
43         //if ssl mode is enabled, then config tls
44         if sslMode == "0" {
45                 gwPORT = conf.HttpGWPORT
46                 urlProto = "http://"
47         } else {
48                 gwPORT = conf.HttpsGWPORT
49                 urlProto = "https://"
50         }
51
52         url := urlProto + gwIP + ":" + gwPORT + gwRoutes + "/mep/mec_service_mgmt/v1/applications/" + appInstanceId + "/services"
53         log.Println("Register url is" + url)
54
55     for _, serviceInfo := range serviceInfos {
56                 data, e := json.Marshal(serviceInfo)
57                 if e != nil {
58                         log.Println("Failed to marshal service info to object")
59                         continue
60                 }
61
62                 for i := 1; i <= 5; i++ { // if register failed, then retry five times
63                         _, err := RegisterToMep(string(data), url)
64                         if err != nil {
65                                 log.Println("Failed to register to mep, appInstanceId is" + appInstanceId + ", serviceName is " + serviceInfo.SerName)
66                         } else {
67                                 log.Println("Register mep main to mep success, appInstanceId is" + appInstanceId + ", serviceName is " + serviceInfo.SerName)
68                                 break
69                         }
70                         log.Println("Failed to register mep main to mep, will retry 5 times, retry interval is 30 s, already retry " + strconv.Itoa(i) + " time")
71                         time.Sleep(30 * time.Second) // register failed , 30 seconds after try again
72                 }
73         }
74
75         return "", nil
76 }