data convertor of consul and msb 41/58541/1
authorLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 02:39:43 +0000 (10:39 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 02:39:43 +0000 (10:39 +0800)
Issue-ID: MSB-250

Change-Id: I8c0446af0e1954b68d5af1331bd25426ec4e6ea1
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/models/conversion.go [new file with mode: 0644]

diff --git a/msb2pilot/src/msb2pilot/models/conversion.go b/msb2pilot/src/msb2pilot/models/conversion.go
new file mode 100644 (file)
index 0000000..144d05b
--- /dev/null
@@ -0,0 +1,117 @@
+/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and the Apache License 2.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ *     ZTE - initial Project
+ */
+package models
+
+import (
+       "encoding/json"
+       "msb2pilot/log"
+       "strings"
+
+       "github.com/hashicorp/consul/api"
+)
+
+func ConvertService(endpoints []*api.CatalogService) *MsbService {
+       if len(endpoints) == 0 {
+               return nil
+       }
+
+       endpoint := endpoints[0]
+       service := &MsbService{
+               ServiceName:  endpoint.ServiceName,
+               ModifyIndex:  endpoint.ModifyIndex,
+               ConsulLabels: &ConsulLabels{},
+       }
+
+       convertMsbLabels(service.ConsulLabels, endpoint.ServiceTags)
+       return service
+}
+
+func convertBaseInfo(baseString string) (baseInfo *BaseInfo, err error) {
+       baseInfo = new(BaseInfo)
+       err = json.Unmarshal([]byte(baseString), baseInfo)
+
+       return
+}
+
+func convertNameSpace(ns string) (nameSpace *NameSpace, err error) {
+       nameSpace = new(NameSpace)
+       err = json.Unmarshal([]byte(ns), nameSpace)
+       return
+}
+
+func LoadMsbServiceFromMap(services map[string][]string) []*MsbService {
+       result := make([]*MsbService, 0, len(services))
+
+       for k, v := range services {
+               service := &MsbService{
+                       ServiceName:  k,
+                       ConsulLabels: new(ConsulLabels),
+               }
+               convertMsbLabels(service.ConsulLabels, v)
+               result = append(result, service)
+       }
+       return result
+}
+
+func ConsulService2MsbService(consulService *api.CatalogService) *MsbService {
+       msbService := &MsbService{
+               ServiceName:    consulService.ServiceName,
+               ServiceAddress: consulService.ServiceAddress,
+               ServicePort:    consulService.ServicePort,
+               ConsulLabels:   new(ConsulLabels),
+       }
+
+       convertMsbLabels(msbService.ConsulLabels, consulService.ServiceTags)
+
+       return msbService
+}
+
+func convertMsbLabel(label, labelstr string) interface{} {
+       var result interface{}
+       var err error
+
+       labelPrefix := "\"" + label + "\":"
+
+       if strings.HasPrefix(labelstr, labelPrefix) {
+               kvp := strings.Split(labelstr, labelPrefix)
+               value := kvp[1]
+
+               switch label {
+               case "base":
+                       result, err = convertBaseInfo(value)
+               case "ns":
+                       result, err = convertNameSpace(value)
+               }
+
+               if err != nil {
+                       log.Log.Error("parse msb label error", err)
+                       return nil
+               }
+
+               return result
+       }
+
+       return nil
+}
+func convertMsbLabels(consulLabels *ConsulLabels, labels []string) {
+       for _, label := range labels {
+               baseInfo := convertMsbLabel("base", label)
+               if baseInfo != nil {
+                       consulLabels.BaseInfo = baseInfo.(*BaseInfo)
+               }
+
+               ns := convertMsbLabel("ns", label)
+               if ns != nil {
+                       consulLabels.NameSpace = ns.(*NameSpace)
+               }
+       }
+}