class AAICreateTasks {

    public void createOwningEntity(BuildingBlockExecution execution) {
        try {
            ServiceInstance serviceInstance =
                    extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
            OwningEntity owningEntity = serviceInstance.getOwningEntity();
            if (Strings.isNullOrEmpty(owningEntity.getOwningEntityId())
                    && Strings.isNullOrEmpty(owningEntity.getOwningEntityName())) {
                execution.setVariable("ErrorCreateOEAAI", EXCEPTION_NAME_AND_ID_ARE_NULL);
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "EXCEPTION_NAME_AND_ID_ARE_NULL");
            } else if (Strings.isNullOrEmpty(owningEntity.getOwningEntityId())
                    && !Strings.isNullOrEmpty(owningEntity.getOwningEntityName())) {
                if (aaiSIResources.existsOwningEntityName(owningEntity.getOwningEntityName())) {
                    org.onap.aai.domain.yang.OwningEntity aaiEntity =
                            aaiSIResources.getOwningEntityByName(owningEntity.getOwningEntityName());
                    owningEntity.setOwningEntityId(aaiEntity.getOwningEntityId());
                    owningEntity.setOwningEntityName(owningEntity.getOwningEntityName());
                    aaiSIResources.connectOwningEntityandServiceInstance(owningEntity, serviceInstance);
                } else {
                    owningEntity.setOwningEntityId(UUID.randomUUID().toString());
                    aaiSIResources.createOwningEntityandConnectServiceInstance(owningEntity, serviceInstance);
                }
            } else {
                if (aaiSIResources.existsOwningEntity(owningEntity)) {
                    aaiSIResources.connectOwningEntityandServiceInstance(owningEntity, serviceInstance);
                } else {
                    if (Strings.isNullOrEmpty(owningEntity.getOwningEntityName())) {
                        String msg =
                                "Exception in AAICreateOwningEntity. Can't create an owningEntity with no owningEntityName.";
                        logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                                ErrorCode.UnknownError.getValue(), msg);
                        exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
                    } else {
                        if (aaiSIResources.existsOwningEntityName(owningEntity.getOwningEntityName())) {
                            logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
                                    EXCEPTION_NAME_EXISTS_WITH_DIFFERENT_ID, "BPMN", ErrorCode.UnknownError.getValue(),
                                    EXCEPTION_NAME_EXISTS_WITH_DIFFERENT_ID);
                            exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
                                    EXCEPTION_NAME_EXISTS_WITH_DIFFERENT_ID);
                        } else {
                            aaiSIResources.createOwningEntityandConnectServiceInstance(owningEntity, serviceInstance);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
        }
    }

}