From 1a0189f890e4ff65ee245be9b7ef67788ac53a61 Mon Sep 17 00:00:00 2001 From: "Stone, Avi (as206k)" Date: Thu, 24 May 2018 16:12:32 +0300 Subject: Update code Update to latest version after improving testing and sonar coverage Change-Id: I527f4bc38d3b4e1005d38e3a2584fa730857425d Issue-ID: SDC-1313 Signed-off-by: Stone, Avi (as206k) --- .../composition/controller/BaseController.java | 60 +- .../controller/BlueprintController.java | 68 +- .../controller/CompositionController.java | 212 +----- .../controller/ConfigurationController.java | 2 +- .../controller/LifecycleController.java | 11 +- .../controller/RuleEditorController.java | 9 +- .../composition/controller/VfcmtController.java | 3 - .../dcae/composition/impl/BaseBusinessLogic.java | 34 +- .../composition/impl/BlueprintBusinessLogic.java | 12 +- .../composition/impl/CompositionBusinessLogic.java | 9 +- .../impl/CompositionCatalogBusinessLogic.java | 205 ++++++ .../composition/impl/ReferenceBusinessLogic.java | 2 +- .../composition/impl/RuleEditorBusinessLogic.java | 1 - .../composition/impl/ServiceBusinessLogic.java | 48 +- .../dcae/rule/editor/validators/RuleValidator.java | 4 +- .../impl/CompositionCatalogBusinessLogicTest.java | 74 ++ .../composition/impl/ServiceBusinessLogicTest.java | 406 +++++++---- .../java/org/onap/sdc/dcae/catalog/asdc/ASDC.java | 802 +-------------------- .../onap/sdc/dcae/catalog/asdc/ASDCController.java | 0 .../org/onap/sdc/dcae/catalog/asdc/ASDCUtils.java | 276 +------ .../org/onap/sdc/dcae/errormng/RequestError.java | 18 +- .../org/onap/sdc/dcae/catalog/commons/Action.java | 2 - .../org/onap/sdc/dcae/catalog/commons/Future.java | 3 - .../sdc/dcae/catalog/commons/FutureHandler.java | 2 - .../org/onap/sdc/dcae/catalog/commons/Futures.java | 8 +- .../sdc/dcae/catalog/engine/CatalogController.java | 9 +- .../onap/sdc/dcae/catalog/engine/CatalogError.java | 3 - .../sdc/dcae/catalog/engine/CatalogRequest.java | 2 - 28 files changed, 717 insertions(+), 1568 deletions(-) create mode 100644 dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/impl/CompositionCatalogBusinessLogic.java create mode 100644 dcaedt_be/src/test/java/org/onap/sdc/dcae/composition/impl/CompositionCatalogBusinessLogicTest.java delete mode 100644 dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/catalog/asdc/ASDCController.java diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BaseController.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BaseController.java index 818a30d..131efad 100644 --- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BaseController.java +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BaseController.java @@ -1,36 +1,22 @@ package org.onap.sdc.dcae.composition.controller; -import javax.servlet.http.HttpServletRequest; - +import com.google.gson.Gson; +import org.onap.sdc.common.onaplog.Enums.LogLevel; 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; +import javax.servlet.http.HttpServletRequest; public abstract class BaseController { protected Gson gson = new Gson(); - @Autowired - protected SystemProperties systemProperties; - @Autowired protected BaseBusinessLogic baseBusinessLogic; @@ -42,48 +28,8 @@ public abstract class BaseController { return request.getAttribute("requestId").toString(); } - ResourceDetailed checkoutVfcmt(String userId, String uuid, String requestId) { - return baseBusinessLogic.getSdcRestClient().changeResourceLifecycleState(userId, uuid, LifecycleOperationType.CHECKOUT.name(), null, requestId); - } - - ResourceDetailed checkinVfcmt(String userId, String uuid, String requestId) { - return baseBusinessLogic.getSdcRestClient().changeResourceLifecycleState(userId, uuid, LifecycleOperationType.CHECKIN.name(), "checking in vfcmt" + uuid, requestId); - } - - - boolean isNeedToCheckOut(String lifecycleState) { - return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT != DcaeBeConstants.LifecycleStateEnum.findState(lifecycleState); - } - - void checkUserIfResourceCheckedOut(String userId, Asset asset) { - 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 (AssetType.VFCMT != getValidAssetTypeOrNull(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); } - - AssetType getValidAssetTypeOrNull(String type) { - try { - return AssetType.getAssetTypeByName(type); - } catch (IllegalArgumentException e) { - debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "invalid asset type: {}. Error: {}", type, e); - return null; - } - } } diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BlueprintController.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BlueprintController.java index 5f4cee7..0d638d9 100644 --- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BlueprintController.java +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/BlueprintController.java @@ -7,48 +7,44 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.annotation.PostConstruct; - @RestController @EnableAutoConfiguration @CrossOrigin public class BlueprintController extends BaseController { - @Autowired - private BlueprintBusinessLogic blueprintBusinessLogic; + @Autowired + private BlueprintBusinessLogic blueprintBusinessLogic; - @PostConstruct - public void init(){ + /*** + * VFCMT - Resource, blueprint - as an artifact as an service. + * @param context + * @param userId + * @param vfcmtUuid + * @param serviceUuid + * @param instanceName + * @param requestId + * @return ResponseEntity + */ + @RequestMapping(value = "{context}/createBluePrint/{VFCMTUuid}/{serviceUuid}/{instanceName}", method = RequestMethod.POST) + public ResponseEntity createBlueprint( + @RequestHeader("USER_ID") String userId, + @PathVariable String context, + @PathVariable("VFCMTUuid") String vfcmtUuid, + @PathVariable("serviceUuid") String serviceUuid, + @PathVariable("instanceName") String instanceName, + @ModelAttribute("requestId") String requestId) { + return blueprintBusinessLogic.generateAndSaveBlueprint(userId, context, vfcmtUuid, serviceUuid, instanceName, "", requestId); } - /*** - * VFCMT - Resource, blueprint - as an artifact as an service. - * @param context - * @param userId - * @param vfcmtUuid - * @param serviceUuid - * @param instanceName - * @param requestId - * @return ResponseEntity - */ - @RequestMapping(value = "{context}/createBluePrint/{VFCMTUuid}/{serviceUuid}/{instanceName}", method = RequestMethod.POST) - public ResponseEntity createBlueprint(@RequestHeader("USER_ID") String userId, - @PathVariable String context, - @PathVariable("VFCMTUuid") String vfcmtUuid, - @PathVariable("serviceUuid") String serviceUuid, - @PathVariable("instanceName") String instanceName, - @ModelAttribute("requestId") String requestId) { - return blueprintBusinessLogic.generateAndSaveBlueprint(userId, context, vfcmtUuid, serviceUuid, instanceName, "", requestId); - } - - @Deprecated - @RequestMapping(value = "/createBluePrint/{VFCMTUuid}/{serviceUuid}/{instanceName}/{monitoringFlowType}", method = RequestMethod.POST) - public ResponseEntity createBluePrintWithFlowType(@RequestHeader("USER_ID") String userId, - @PathVariable("VFCMTUuid") String vfcmtUuid, - @PathVariable("serviceUuid") String serviceUuid, - @PathVariable("instanceName") String serviceInstanceName, - @PathVariable("monitoringFlowType") String monitoringFlowType, - @ModelAttribute("requestId") String requestId) { - return blueprintBusinessLogic.generateAndSaveBlueprint(userId, AssetType.SERVICE.name(), vfcmtUuid, serviceUuid, serviceInstanceName, monitoringFlowType, requestId); - } + @Deprecated + @RequestMapping(value = "/createBluePrint/{VFCMTUuid}/{serviceUuid}/{instanceName}/{monitoringFlowType}", method = RequestMethod.POST) + public ResponseEntity createBluePrintWithFlowType( + @RequestHeader("USER_ID") String userId, + @PathVariable("VFCMTUuid") String vfcmtUuid, + @PathVariable("serviceUuid") String serviceUuid, + @PathVariable("instanceName") String instanceName, + @PathVariable("monitoringFlowType") String monitoringFlowType, + @ModelAttribute("requestId") String requestId) { + return blueprintBusinessLogic.generateAndSaveBlueprint(userId, AssetType.SERVICE.name(), vfcmtUuid, serviceUuid, instanceName, monitoringFlowType, requestId); + } } diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/CompositionController.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/CompositionController.java index 3261ddc..dbcbcc9 100644 --- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/CompositionController.java +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/controller/CompositionController.java @@ -1,17 +1,15 @@ package org.onap.sdc.dcae.composition.controller; -import org.json.JSONArray; -import org.json.JSONException; import org.onap.sdc.common.onaplog.Enums.LogLevel; +import org.onap.sdc.dcae.catalog.engine.CatalogResponse; +import org.onap.sdc.dcae.catalog.engine.ElementRequest; +import org.onap.sdc.dcae.catalog.engine.ItemsRequest; import org.onap.sdc.dcae.composition.impl.CompositionBusinessLogic; +import org.onap.sdc.dcae.composition.impl.CompositionCatalogBusinessLogic; import org.onap.sdc.dcae.composition.restmodels.MessageResponse; import org.onap.sdc.dcae.composition.restmodels.ReferenceUUID; import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact; -import org.onap.sdc.dcae.composition.restmodels.sdc.Asset; import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; -import org.onap.sdc.dcae.catalog.Catalog; -import org.onap.sdc.dcae.catalog.Catalog.*; -import org.onap.sdc.dcae.catalog.engine.*; import org.onap.sdc.dcae.composition.util.DcaeBeConstants; import org.onap.sdc.dcae.enums.LifecycleOperationType; import org.onap.sdc.dcae.errormng.ActionStatus; @@ -25,8 +23,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.async.DeferredResult; -import javax.annotation.PostConstruct; -import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,22 +34,22 @@ import java.util.stream.Collectors; @CrossOrigin public class CompositionController extends BaseController { - @Autowired private CatalogController catalogController; + @Autowired + private CompositionCatalogBusinessLogic compositionCatalogBusinessLogic; - @Autowired private CompositionBusinessLogic compositionBusinessLogic; + @Autowired + private CompositionBusinessLogic compositionBusinessLogic; - @PostConstruct public void init() { - catalogController.setDefaultCatalog(URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.ASDC_CATALOG_URL))); - } - - @RequestMapping(value = { "/utils/clone/{assetType}/{sourceId}/{targetId}" }, method = { RequestMethod.GET }, produces = { "application/json" }) public ResponseEntity clone(@RequestHeader("USER_ID") String userId, + @Deprecated + @RequestMapping(value = { "/utils/clone/{assetType}/{sourceId}/{targetId}" }, method = { RequestMethod.GET }, produces = { "application/json" }) + public ResponseEntity clone(@RequestHeader("USER_ID") String userId, @PathVariable("assetType") String theAssetType, @PathVariable("sourceId") String theSourceId, @PathVariable("targetId") String theTargetId, @ModelAttribute("requestId") String requestId) { MessageResponse response = new MessageResponse(); try { // fetch the source and assert it is a vfcmt containing clone worthy artifacts (composition + rules) ResourceDetailed sourceVfcmt = baseBusinessLogic.getSdcRestClient().getResource(theSourceId, requestId); - checkVfcmtType(sourceVfcmt); + baseBusinessLogic.checkVfcmtType(sourceVfcmt); List artifactsToClone = CollectionUtils.isEmpty(sourceVfcmt.getArtifacts()) ? null : sourceVfcmt.getArtifacts().stream().filter(p -> DcaeBeConstants.Composition.fileNames.COMPOSITION_YML.equals(p.getArtifactName()) || p.getArtifactName().endsWith(DcaeBeConstants.Composition.fileNames.MAPPING_RULE_POSTFIX)) @@ -66,9 +62,9 @@ public class CompositionController extends BaseController { // fetch the target ResourceDetailed vfcmt = baseBusinessLogic.getSdcRestClient().getResource(theTargetId, requestId); debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), vfcmt.toString()); - checkVfcmtType(vfcmt); - checkUserIfResourceCheckedOut(userId, vfcmt); - boolean isTargetNeed2Checkout = isNeedToCheckOut(vfcmt.getLifecycleState()); + baseBusinessLogic.checkVfcmtType(vfcmt); + baseBusinessLogic.checkUserIfResourceCheckedOut(userId, vfcmt); + boolean isTargetNeed2Checkout = baseBusinessLogic.isNeedToCheckOut(vfcmt.getLifecycleState()); if (isTargetNeed2Checkout) { ResourceDetailed targetVfcmt = baseBusinessLogic.getSdcRestClient().changeResourceLifecycleState(userId, theTargetId, LifecycleOperationType.CHECKOUT.name(), "checking out VFCMT before clone", requestId); if (null == targetVfcmt) { @@ -95,183 +91,39 @@ public class CompositionController extends BaseController { } } - @RequestMapping(value = "/elements", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public DeferredResult items(@RequestBody(required = false) ItemsRequest theRequest) { - - final ItemsRequest request = (theRequest == null) ? ItemsRequest.EMPTY_REQUEST : theRequest; - - Catalog catalog = catalogController.getCatalog(request.getCatalog()); - DeferredResult result = new DeferredResult(request.getTimeout()); - - catalog.rootsByLabel(request.getStartingLabel()).setHandler(catalogController.new CatalogHandler(request, result) { - public CatalogResponse handleData(Folders theFolders) { - JSONArray ja = new JSONArray(); - if (theFolders != null) { - for (Folder folder : theFolders) { - ja.put(catalogController.patchData(catalog, folder.data())); - } - } - CatalogResponse response = new CatalogResponse(this.request); - try { - response.data().put("elements", ja); - } catch (JSONException e) { - errLogger.log(LogLevel.ERROR, this.getClass().getName(), "JSONException putting json elements to response {}", e); - } - return response; - } - }); - return result; + @RequestMapping(value = "/elements", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") + public DeferredResult items(@RequestBody(required = false) ItemsRequest theRequest) { + return compositionCatalogBusinessLogic.getItems(theRequest); } - @RequestMapping(value = "/{theItemId}/elements", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public DeferredResult items(@RequestBody(required = false) ItemsRequest theRequest, - @PathVariable String theItemId) { - - final ItemsRequest request = (theRequest == null) ? ItemsRequest.EMPTY_REQUEST : theRequest; - - Catalog catalog = catalogController.getCatalog(request.getCatalog()); - DeferredResult result = new DeferredResult(request.getTimeout()); - - catalog - // .fetchFolderByItemId(theItemId) - .folder(theItemId).withParts().withPartAnnotations().withItems().withItemAnnotations().withItemModels().execute().setHandler(catalogController.new CatalogHandler(request, result) { - public CatalogResponse handleData(Folder theFolder) { - CatalogResponse response = new CatalogResponse(this.request); - if (theFolder == null) { - return response; - } - - try { - Elements folders = theFolder.elements("parts", Folders.class); - if (folders != null) { - for (Object folder : folders) { - catalogController.patchData(catalog, ((Element) folder).data()); - // lots of ephemere proxies created here .. - Elements annotations = ((Element) folder).elements("annotations", Annotations.class); - if (annotations != null) { - for (Object a : annotations) { - catalogController.patchData(catalog, ((Annotation) a).data()); - } - } - } - } - Elements items = theFolder.elements("items", Items.class); - if (items != null) { - for (Object i : items) { - catalogController.patchData(catalog, ((Element) i).data()); - // lots of ephemere proxies created here .. - Elements annotations = ((Element) i).elements("annotations", Annotations.class); - if (annotations != null) { - for (Object a : annotations) { - catalogController.patchData(catalog, ((Annotation) a).data()); - } - } - } - } - } catch (Exception x) { - errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Exception processing catalog {}", x); - return new CatalogError(this.request, "", x); - } - - try { - response.data().put("element", theFolder.data()); - } catch (JSONException e) { - errLogger.log(LogLevel.ERROR, this.getClass().getName(), "JSONException putting element to response {}", e); - } - return response; - } - }); - - return result; + @RequestMapping(value = "/{theItemId}/elements", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") + public DeferredResult items(@RequestBody(required = false) ItemsRequest theRequest, @PathVariable String theItemId) { + return compositionCatalogBusinessLogic.getItemById(theRequest, theItemId); } - @RequestMapping(value = "/{theItemId}/model", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public DeferredResult model(@RequestBody(required = false) ElementRequest theRequest, - @PathVariable String theItemId) { - final ElementRequest request = (theRequest == null) ? ElementRequest.EMPTY_REQUEST : theRequest; - - Catalog catalog = catalogController.getCatalog(request.getCatalog()); - DeferredResult result = new DeferredResult<>(request.getTimeout()); - - catalog.item(theItemId).withModels().execute().setHandler(catalogController.new CatalogHandler(request, result) { - public CatalogResponse handleData(Item theItem) { - if (theItem == null) { - return new CatalogError(this.request, "No such item"); - } - Templates models = null; - try { - models = (Templates) theItem.elements("models", Templates.class); - if (models == null || models.isEmpty()) { - return new CatalogError(this.request, "Item has no models"); - } - if (models.size() > 1) { - return new CatalogError(this.request, "Item has more than one model !?"); - } - catalog.template(models.get(0).id()).withInputs().withOutputs().withNodes().withNodeProperties().withNodePropertiesAssignments().withNodeRequirements().withNodeCapabilities().withNodeCapabilityProperties() - .withNodeCapabilityPropertyAssignments().withPolicies().withPolicyProperties().withPolicyPropertiesAssignments().execute().setHandler(catalogController.new CatalogHandler