2 * Copyright 2020 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 "github.com/apache/servicecomb-service-center/pkg/log"
24 "github.com/apache/servicecomb-service-center/pkg/rest"
25 "github.com/apache/servicecomb-service-center/pkg/util"
26 "github.com/apache/servicecomb-service-center/server/core/proto"
27 "github.com/apache/servicecomb-service-center/server/notify"
28 v4 "github.com/apache/servicecomb-service-center/server/rest/controller/v4"
29 svcutil "github.com/apache/servicecomb-service-center/server/service/util"
31 "mepserver/mp1/arch/workspace"
32 "mepserver/mp1/models"
36 basePath = "/mep/mec_service_mgmt/v1"
37 servicesPath = basePath + "/services"
38 appServicesPath = basePath + "/applications/:appInstanceId" + "/services"
39 appSubscribePath = basePath + "/applications/:appInstanceId/subscriptions"
43 SerErrFailBase workspace.ErrCode = workspace.TaskFail
44 SerErrServiceNotFound = 2
45 SerInstanceNotFound = 3
47 SubscriptionNotFound = 5
48 OperateDataWithEtcdErr = 6
49 SerErrServiceDelFailed = 7
50 SerErrServiceUpdFailed = 8
53 type APIHookFunc func() models.EndPointInfo
55 type APIGwHook struct {
61 func SetAPIHook(hook APIGwHook) {
65 func GetApiHook() APIGwHook {
75 RegisterServant(&Mp1Service{})
78 type Mp1Service struct {
79 v4.MicroServiceService
82 func (m *Mp1Service) URLPatterns() []rest.Route {
85 {Method: rest.HTTP_METHOD_POST, Path: appSubscribePath, Func: doAppSubscribe},
86 {Method: rest.HTTP_METHOD_GET, Path: appSubscribePath, Func: getAppSubscribes},
87 {Method: rest.HTTP_METHOD_GET, Path: appSubscribePath + "/:subscriptionId", Func: getOneAppSubscribe},
88 {Method: rest.HTTP_METHOD_DELETE, Path: appSubscribePath + "/:subscriptionId", Func: delOneAppSubscribe},
90 {Method: rest.HTTP_METHOD_POST, Path: appServicesPath, Func: serviceRegister},
91 {Method: rest.HTTP_METHOD_GET, Path: appServicesPath, Func: serviceDiscover},
92 {Method: rest.HTTP_METHOD_PUT, Path: appServicesPath + "/:serviceId", Func: serviceUpdate},
93 {Method: rest.HTTP_METHOD_GET, Path: appServicesPath + "/:serviceId", Func: getOneService},
94 {Method: rest.HTTP_METHOD_DELETE, Path: appServicesPath + "/:serviceId", Func: serviceDelete},
96 {Method: rest.HTTP_METHOD_GET, Path: servicesPath, Func: serviceDiscover},
97 {Method: rest.HTTP_METHOD_GET, Path: servicesPath + "/:serviceId", Func: getOneService},
101 func doAppSubscribe(w http.ResponseWriter, r *http.Request) {
103 workPlan := NewWorkSpace(w, r)
105 (&DecodeRestReq{}).WithBody(&models.SerAvailabilityNotificationSubscription{}),
107 workPlan.Finally(&SendHttpRspCreated{})
109 workspace.WkRun(workPlan)
112 func getAppSubscribes(w http.ResponseWriter, r *http.Request) {
114 workPlan := NewWorkSpace(w, r)
118 workPlan.Finally(&SendHttpRsp{})
120 workspace.WkRun(workPlan)
123 func getOneAppSubscribe(w http.ResponseWriter, r *http.Request) {
125 workPlan := NewWorkSpace(w, r)
129 workPlan.Finally(&SendHttpRsp{})
131 workspace.WkRun(workPlan)
134 func delOneAppSubscribe(w http.ResponseWriter, r *http.Request) {
136 workPlan := NewWorkSpace(w, r)
140 workPlan.Finally(&SendHttpRsp{})
142 workspace.WkRun(workPlan)
145 func serviceRegister(w http.ResponseWriter, r *http.Request) {
146 log.Info("Register service start...")
148 workPlan := NewWorkSpace(w, r)
150 (&DecodeRestReq{}).WithBody(&models.ServiceInfo{}),
151 &RegisterServiceId{},
152 &RegisterServiceInst{})
153 workPlan.Finally(&SendHttpRspCreated{})
155 workspace.WkRun(workPlan)
158 func serviceDiscover(w http.ResponseWriter, r *http.Request) {
159 log.Info("Discover service service start...")
161 workPlan := NewWorkSpace(w, r)
167 workPlan.Finally(&SendHttpRsp{})
169 workspace.WkRun(workPlan)
172 func serviceUpdate(w http.ResponseWriter, r *http.Request) {
173 log.Info("Update a service start...")
175 workPlan := NewWorkSpace(w, r)
177 (&DecodeRestReq{}).WithBody(&models.ServiceInfo{}),
179 workPlan.Finally(&SendHttpRsp{})
181 workspace.WkRun(workPlan)
184 func getOneService(w http.ResponseWriter, r *http.Request) {
185 log.Info("Register service start...")
187 workPlan := NewWorkSpace(w, r)
191 workPlan.Finally(&SendHttpRsp{})
193 workspace.WkRun(workPlan)
197 func serviceDelete(w http.ResponseWriter, r *http.Request) {
198 log.Info("Delete a service start...")
200 workPlan := NewWorkSpace(w, r)
204 workPlan.Finally(&SendHttpRsp{})
206 workspace.WkRun(workPlan)
209 func WebsocketListAndWatch(ctx context.Context, req *proto.WatchInstanceRequest, consumerSvcId string) {
210 if req == nil || len(req.SelfServiceId) == 0 {
211 log.Warn("request fomat invalid!")
214 domainProject := util.ParseDomainProject(ctx)
215 if !svcutil.ServiceExist(ctx, domainProject, req.SelfServiceId) {
216 log.Warn("service does not exist!")
219 DoWebsocketListAndWatch(ctx, req.SelfServiceId, consumerSvcId, func() ([]*proto.WatchInstanceResponse, int64) {
220 return svcutil.QueryAllProvidersInstances(ctx, req.SelfServiceId)
224 func DoWebsocketListAndWatch(ctx context.Context, serviceId string, consumerSvcId string, f func() ([]*proto.WatchInstanceResponse, int64)) {
225 domainProject := util.ParseDomainProject(ctx)
226 socket := &Websocket{
228 watcher: notify.NewInstanceEventListWatcher(serviceId, domainProject, f),
229 serviceID: consumerSvcId,
231 ProcessSocket(socket)
234 func ProcessSocket(socket *Websocket) {
235 if err := socket.Init(); err != nil {
238 socket.HandleWatchWebSocketControlMessage()