Modified to support Certificate and KeyPath
[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         "strconv"
23         "time"
24 )
25
26 func SvcReg(confPath string) (string, error) {
27         conf, err := GetConf(confPath)
28         if err != nil {
29                 log.Println(err.Error())
30                 return "", err
31         }
32
33         appInstanceId := conf.AppInstanceId
34         serviceInfos := conf.ServiceInfoPosts
35         mepServerIP := conf.MepServerIP
36         mepServerPORT := conf.MepServerPORT
37         url := "http://" + mepServerIP + ":" + mepServerPORT + "/mep/mec_service_mgmt/v1/applications/" + appInstanceId + "/services"
38
39     for _, serviceInfo := range serviceInfos {
40                 data, e := json.Marshal(serviceInfo)
41                 if e != nil {
42                         log.Println("Failed to marshal service info to object")
43                         continue
44                 }
45
46                 for i := 1; i <= 5; i++ { // if register failed, then retry five times
47                         _, err := RegisterToMep(string(data), url)
48                         if err != nil {
49                                 log.Println("Failed to register to mep, appInstanceId is" + appInstanceId + ", serviceName is " + serviceInfo.SerName)
50                         } else {
51                                 log.Println("Register mep main to mep success, appInstanceId is" + appInstanceId + ", serviceName is " + serviceInfo.SerName)
52                                 break
53                         }
54                         log.Println("Failed to register mep main to mep, will retry 5 times, retry interval is 30 s, already retry " + strconv.Itoa(i) + " time")
55                         time.Sleep(30 * time.Second) // register failed , 30 seconds after try again
56                 }
57         }
58
59         return "", nil
60 }