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 //application subscription
102 func doAppSubscribe(w http.ResponseWriter, r *http.Request) {
104 workPlan := NewWorkSpace(w, r)
106 (&DecodeRestReq{}).WithBody(&models.SerAvailabilityNotificationSubscription{}),
108 workPlan.Finally(&SendHttpRspCreated{})
110 workspace.WkRun(workPlan)
113 func getAppSubscribes(w http.ResponseWriter, r *http.Request) {
115 workPlan := NewWorkSpace(w, r)
119 workPlan.Finally(&SendHttpRsp{})
121 workspace.WkRun(workPlan)
124 func getOneAppSubscribe(w http.ResponseWriter, r *http.Request) {
126 workPlan := NewWorkSpace(w, r)
130 workPlan.Finally(&SendHttpRsp{})
132 workspace.WkRun(workPlan)
135 func delOneAppSubscribe(w http.ResponseWriter, r *http.Request) {
137 workPlan := NewWorkSpace(w, r)
141 workPlan.Finally(&SendHttpRsp{})
143 workspace.WkRun(workPlan)
146 //service registery request
147 func serviceRegister(w http.ResponseWriter, r *http.Request) {
148 log.Info("Register service start...")
150 workPlan := NewWorkSpace(w, r)
152 (&DecodeRestReq{}).WithBody(&models.ServiceInfo{}),
153 &RegisterServiceId{},
154 &RegisterServiceInst{})
155 workPlan.Finally(&SendHttpRspCreated{})
157 workspace.WkRun(workPlan)
160 func serviceDiscover(w http.ResponseWriter, r *http.Request) {
161 log.Info("Discover service service start...")
163 workPlan := NewWorkSpace(w, r)
169 workPlan.Finally(&SendHttpRsp{})
171 workspace.WkRun(workPlan)
174 func serviceUpdate(w http.ResponseWriter, r *http.Request) {
175 log.Info("Update a service start...")
177 workPlan := NewWorkSpace(w, r)
179 (&DecodeRestReq{}).WithBody(&models.ServiceInfo{}),
181 workPlan.Finally(&SendHttpRsp{})
183 workspace.WkRun(workPlan)
186 func getOneService(w http.ResponseWriter, r *http.Request) {
187 log.Info("Register service start...")
189 workPlan := NewWorkSpace(w, r)
193 workPlan.Finally(&SendHttpRsp{})
195 workspace.WkRun(workPlan)
199 func serviceDelete(w http.ResponseWriter, r *http.Request) {
200 log.Info("Delete a service start...")
202 workPlan := NewWorkSpace(w, r)
206 workPlan.Finally(&SendHttpRsp{})
208 workspace.WkRun(workPlan)
211 func WebsocketListAndWatch(ctx context.Context, req *proto.WatchInstanceRequest, consumerSvcId string) {
212 if req == nil || len(req.SelfServiceId) == 0 {
213 log.Warn("request fomat invalid!")
216 domainProject := util.ParseDomainProject(ctx)
217 if !svcutil.ServiceExist(ctx, domainProject, req.SelfServiceId) {
218 log.Warn("service does not exist!")
221 DoWebsocketListAndWatch(ctx, req.SelfServiceId, consumerSvcId, func() ([]*proto.WatchInstanceResponse, int64) {
222 return svcutil.QueryAllProvidersInstances(ctx, req.SelfServiceId)
226 func DoWebsocketListAndWatch(ctx context.Context, serviceId string, consumerSvcId string, f func() ([]*proto.WatchInstanceResponse, int64)) {
227 domainProject := util.ParseDomainProject(ctx)
228 socket := &Websocket{
230 watcher: notify.NewInstanceEventListWatcher(serviceId, domainProject, f),
231 serviceID: consumerSvcId,
233 ProcessSocket(socket)
236 func ProcessSocket(socket *Websocket) {
237 if err := socket.Init(); err != nil {
240 socket.HandleWatchWebSocketControlMessage()