summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BaseController.java
blob: 8b590caa484799b894aa3b6f985d3a71a6954a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.onap.sdc.dcae.composition.controller;

import javax.servlet.http.HttpServletRequest;

import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.composition.impl.BaseBusinessLogic;
import org.onap.sdc.dcae.composition.restmodels.sdc.Asset;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.composition.util.SystemProperties;
import org.onap.sdc.dcae.enums.AssetType;
import org.onap.sdc.dcae.enums.LifecycleOperationType;
import org.onap.sdc.dcae.errormng.ActionStatus;
import org.onap.sdc.dcae.errormng.DcaeException;
import org.onap.sdc.dcae.errormng.ErrConfMgr;
import org.onap.sdc.dcae.errormng.ErrConfMgr.ApiType;
import org.onap.sdc.dcae.errormng.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.google.gson.Gson;

public abstract class BaseController {
	
	protected Gson gson = new Gson();

	@Autowired
	protected SystemProperties systemProperties;

	@Autowired
	protected BaseBusinessLogic baseBusinessLogic;

	protected OnapLoggerError errLogger = OnapLoggerError.getInstance();
	protected OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

	@ModelAttribute("requestId")
	public String getRequestId(HttpServletRequest request) {
		return request.getAttribute("requestId").toString();
	}

	Asset checkout(String userId, String uuid, AssetType assetType, String requestId) throws Exception {
		return baseBusinessLogic.getSdcRestClient().changeAssetLifecycleState(userId, uuid, LifecycleOperationType.CHECKOUT.name(), null, assetType, requestId);
	}

	Asset checkin(String userId, String uuid, AssetType assetType, String requestId) throws Exception {
		return baseBusinessLogic.getSdcRestClient().changeAssetLifecycleState(userId, uuid, LifecycleOperationType.CHECKIN.name(), "checking in " + assetType.name() + uuid, assetType, requestId);
	}


	boolean isNeedToCheckOut(String lifecycleState) {
		return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT != DcaeBeConstants.LifecycleStateEnum.findState(lifecycleState);
	}

	void checkUserIfResourceCheckedOut(String userId, Asset asset) throws DcaeException {
		if (DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == DcaeBeConstants.LifecycleStateEnum.findState(asset.getLifecycleState())) {
			String lastUpdaterUserId = asset.getLastUpdaterUserId();
			if (lastUpdaterUserId != null && !lastUpdaterUserId.equals(userId)) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "User conflicts. Operation not allowed for user {} on resource checked out by {}", userId, lastUpdaterUserId);
				ResponseFormat responseFormat = ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.USER_CONFLICT, null, userId, asset.getName(), lastUpdaterUserId);
				throw new DcaeException(HttpStatus.FORBIDDEN, responseFormat.getRequestError());
			}
		}
	}

	void checkVfcmtType(ResourceDetailed vfcmt) {
		if (!"VFCMT".equals(vfcmt.getResourceType()) || !"Template".equals(vfcmt.getCategory())) {
			ResponseFormat responseFormat = ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.RESOURCE_NOT_VFCMT_ERROR, null, vfcmt.getUuid());
			throw new DcaeException(HttpStatus.BAD_REQUEST, responseFormat.getRequestError());
		}
	}

	ResponseEntity handleException(Exception e, ApiType apiType, String... variables){
		errLogger.log(LogLevel.ERROR, this.getClass().getName(), e.getMessage());
		return ErrConfMgr.INSTANCE.handleException(e, apiType, variables);
	}
}