DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / controller / BaseController.java
1 package org.onap.sdc.dcae.composition.controller;
2
3 import javax.servlet.http.HttpServletRequest;
4
5 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
6 import org.onap.sdc.common.onaplog.OnapLoggerError;
7 import org.onap.sdc.common.onaplog.Enums.LogLevel;
8 import org.onap.sdc.dcae.composition.impl.BaseBusinessLogic;
9 import org.onap.sdc.dcae.composition.restmodels.sdc.Asset;
10 import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
11 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
12 import org.onap.sdc.dcae.composition.util.SystemProperties;
13 import org.onap.sdc.dcae.enums.AssetType;
14 import org.onap.sdc.dcae.enums.LifecycleOperationType;
15 import org.onap.sdc.dcae.errormng.ActionStatus;
16 import org.onap.sdc.dcae.errormng.DcaeException;
17 import org.onap.sdc.dcae.errormng.ErrConfMgr;
18 import org.onap.sdc.dcae.errormng.ErrConfMgr.ApiType;
19 import org.onap.sdc.dcae.errormng.ResponseFormat;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.http.HttpStatus;
22 import org.springframework.http.ResponseEntity;
23 import org.springframework.web.bind.annotation.ModelAttribute;
24
25 import com.google.gson.Gson;
26
27 public abstract class BaseController {
28         
29         protected Gson gson = new Gson();
30
31         @Autowired
32         protected SystemProperties systemProperties;
33
34         @Autowired
35         protected BaseBusinessLogic baseBusinessLogic;
36
37         protected OnapLoggerError errLogger = OnapLoggerError.getInstance();
38         protected OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
39
40         @ModelAttribute("requestId")
41         public String getRequestId(HttpServletRequest request) {
42                 return request.getAttribute("requestId").toString();
43         }
44
45         Asset checkout(String userId, String uuid, AssetType assetType, String requestId) throws Exception {
46                 return baseBusinessLogic.getSdcRestClient().changeAssetLifecycleState(userId, uuid, LifecycleOperationType.CHECKOUT.name(), null, assetType, requestId);
47         }
48
49         Asset checkin(String userId, String uuid, AssetType assetType, String requestId) throws Exception {
50                 return baseBusinessLogic.getSdcRestClient().changeAssetLifecycleState(userId, uuid, LifecycleOperationType.CHECKIN.name(), "checking in " + assetType.name() + uuid, assetType, requestId);
51         }
52
53
54         boolean isNeedToCheckOut(String lifecycleState) {
55                 return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT != DcaeBeConstants.LifecycleStateEnum.findState(lifecycleState);
56         }
57
58         void checkUserIfResourceCheckedOut(String userId, Asset asset) throws DcaeException {
59                 if (DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == DcaeBeConstants.LifecycleStateEnum.findState(asset.getLifecycleState())) {
60                         String lastUpdaterUserId = asset.getLastUpdaterUserId();
61                         if (lastUpdaterUserId != null && !lastUpdaterUserId.equals(userId)) {
62                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "User conflicts. Operation not allowed for user {} on resource checked out by {}", userId, lastUpdaterUserId);
63                                 ResponseFormat responseFormat = ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.USER_CONFLICT, null, userId, asset.getName(), lastUpdaterUserId);
64                                 throw new DcaeException(HttpStatus.FORBIDDEN, responseFormat.getRequestError());
65                         }
66                 }
67         }
68
69         void checkVfcmtType(ResourceDetailed vfcmt) {
70                 if (!"VFCMT".equals(vfcmt.getResourceType()) || !"Template".equals(vfcmt.getCategory())) {
71                         ResponseFormat responseFormat = ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.RESOURCE_NOT_VFCMT_ERROR, null, vfcmt.getUuid());
72                         throw new DcaeException(HttpStatus.BAD_REQUEST, responseFormat.getRequestError());
73                 }
74         }
75
76         ResponseEntity handleException(Exception e, ApiType apiType, String... variables){
77                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), e.getMessage());
78                 return ErrConfMgr.INSTANCE.handleException(e, apiType, variables);
79         }
80 }