Merge "mecm-mepm uninstall playbook added"
[ealt-edge.git] / mep / mepserver / mp1 / plan_subscribe_app.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 mp1
18
19 import (
20         "context"
21         "encoding/json"
22         "fmt"
23         "net/http"
24
25         "github.com/apache/servicecomb-service-center/pkg/log"
26         "github.com/apache/servicecomb-service-center/server/core/backend"
27         "github.com/apache/servicecomb-service-center/server/core/proto"
28         "github.com/apache/servicecomb-service-center/server/plugin/pkg/registry"
29         "github.com/satori/go.uuid"
30
31         "mepserver/mp1/arch/workspace"
32         "mepserver/mp1/models"
33 )
34
35 type SubscribeIst struct {
36         workspace.TaskBase
37         R             *http.Request       `json:"r,in"`
38         HttpErrInf    *proto.Response     `json:"httpErrInf,out"`
39         Ctx           context.Context     `json:"ctx,in"`
40         W             http.ResponseWriter `json:"w,in"`
41         RestBody      interface{}         `json:"restBody,in"`
42         AppInstanceId string              `json:"appInstanceId,in"`
43         SubscribeId   string              `json:"subscribeId,in"`
44         HttpRsp       interface{}         `json:"httpRsp,out"`
45 }
46
47 //service subscription request
48 func (t *SubscribeIst) OnRequest(data string) workspace.TaskCode {
49
50         mp1SubscribeInfo, ok := t.RestBody.(*models.SerAvailabilityNotificationSubscription)
51         if !ok {
52                 t.SetFirstErrorCode(SerErrFailBase, "restBody failed")
53                 return workspace.TaskFinish
54         }
55
56         appInstanceId := t.AppInstanceId
57         subscribeId := uuid.NewV4().String()
58         t.SubscribeId = subscribeId
59         subscribeJSON, err := json.Marshal(mp1SubscribeInfo)
60         if err != nil {
61                 log.Errorf(err, "can not Marshal subscribe info")
62                 t.SetFirstErrorCode(ParseInfoErr, "can not marshal subscribe info")
63                 return workspace.TaskFinish
64         }
65         opts := []registry.PluginOp{
66                 registry.OpPut(registry.WithStrKey("/cse-sr/etsi/subscribe/"+appInstanceId+"/"+subscribeId),
67                                    registry.WithValue(subscribeJSON)),
68         }
69         _, resultErr := backend.Registry().TxnWithCmp(context.Background(), opts, nil, nil)
70         if resultErr != nil {
71                 log.Errorf(err, "subscription to etcd failed!")
72                 t.SetFirstErrorCode(OperateDataWithEtcdErr, "put subscription to etcd failed")
73                 return workspace.TaskFinish
74         }
75
76         req := &proto.WatchInstanceRequest{SelfServiceId: appInstanceId[:len(appInstanceId)/2]}
77         t.R.Method = "WATCHLIST"
78         WebsocketListAndWatch(t.Ctx, req, appInstanceId)
79         t.buildResponse(mp1SubscribeInfo)
80
81         return workspace.TaskFinish
82 }
83
84 func (t *SubscribeIst) buildResponse(sub *models.SerAvailabilityNotificationSubscription) {
85         appInstanceID := t.AppInstanceId
86         subscribeID := t.SubscribeId
87
88         t.HttpRsp = sub
89         location := fmt.Sprintf("/mec_service_mgmt/v1/applications/%s/subscriptions/%s/", appInstanceID, subscribeID)
90         t.W.Header().Set("Location", location)
91 }