Merge "mecm-mepm uninstall playbook added"
[ealt-edge.git] / mep / mepserver / mp1 / uuid / mp1context.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 uuid
18
19 import (
20         "crypto/sha256"
21         "fmt"
22
23         "github.com/apache/servicecomb-service-center/pkg/util"
24         mgr "github.com/apache/servicecomb-service-center/server/plugin"
25         "github.com/apache/servicecomb-service-center/server/plugin/pkg/uuid"
26         "github.com/apache/servicecomb-service-center/server/plugin/pkg/uuid/buildin"
27         "golang.org/x/net/context"
28 )
29
30 func init() {
31         mgr.RegisterPlugin(mgr.Plugin{PName: mgr.UUID, Name: "mp1context", New: New})
32
33 }
34
35 func New() mgr.PluginInstance {
36         return &Mp1ContextUUID{}
37 }
38
39 type Mp1ContextUUID struct {
40         buildin.BuildinUUID
41 }
42
43 func (cu *Mp1ContextUUID) fromContext(ctx context.Context) string {
44         key, ok := ctx.Value(uuid.ContextKey).(string)
45         if !ok {
46                 return ""
47         }
48         return key
49 }
50
51 func (cu *Mp1ContextUUID) GetServiceId(ctx context.Context) string {
52         content := cu.fromContext(ctx)
53         if len(content) == 0 {
54                 return cu.BuildinUUID.GetServiceId(ctx)
55         }
56
57         shaSum := sha256.Sum256(util.StringToBytesWithNoCopy(content))
58         shaHalf := shaSum[0:8]
59         return fmt.Sprintf("%x", shaHalf)
60 }
61
62 func (cu *Mp1ContextUUID) GetInstanceId(_ context.Context) string {
63         shaSum := sha256.Sum256(util.StringToBytesWithNoCopy(util.GenerateUuid()))
64         shaHalf := shaSum[0:8]
65         return fmt.Sprintf("%x", shaHalf)
66 }