diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java | 220 |
1 files changed, 81 insertions, 139 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java index 81c41bc152..d293e9b615 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/DistributionBusinessLogic.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,12 +17,18 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.distribution; +import static org.apache.commons.lang.BooleanUtils.isTrue; +import static org.openecomp.sdc.be.components.distribution.engine.DistributionEngineInitTask.buildTopicName; +import static org.openecomp.sdc.be.config.ConfigurationManager.getConfigurationManager; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import fj.data.Either; +import java.util.List; +import javax.annotation.Resource; +import javax.ws.rs.core.Response; import org.apache.commons.collections.CollectionUtils; import org.apache.http.HttpStatus; import org.openecomp.sdc.be.components.distribution.engine.CambriaErrorResponse; @@ -47,29 +53,31 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Component; -import javax.annotation.Resource; -import javax.ws.rs.core.Response; -import java.util.List; - -import static org.apache.commons.lang.BooleanUtils.isTrue; -import static org.openecomp.sdc.be.components.distribution.engine.DistributionEngineInitTask.buildTopicName; -import static org.openecomp.sdc.be.config.ConfigurationManager.getConfigurationManager; - @Component("distributionBusinessLogic") public class DistributionBusinessLogic { + public static final String REGISTER_IN_DISTRIBUTION_ENGINE = "registerInDistributionEngine"; public static final String UN_REGISTER_IN_DISTRIBUTION_ENGINE = "unregisterInDistributionEngine"; - private Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static final Logger log = Logger.getLogger(DistributionBusinessLogic.class); + private Gson gson = new GsonBuilder().setPrettyPrinting().create(); @Resource private IDistributionEngine distributionEngine; - private ResponseFormatManager responseFormatManager = ResponseFormatManager.getInstance(); @Resource private ICambriaHandler cambriaHandler; + public static String getNotificationTopicName(String envName) { + DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration(); + return DistributionEngineInitTask.buildTopicName(config.getDistributionNotifTopicName(), envName); + } + + public static String getStatusTopicName(String envName) { + DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration(); + return DistributionEngineInitTask.buildTopicName(config.getDistributionStatusTopicName(), envName); + } + private void initRequestEnvEndPointsAndKeys(RegistrationRequest registrationRequest, DistributionEngineConfiguration config) { - if(CollectionUtils.isEmpty(registrationRequest.getDistEnvEndPoints())){ + if (CollectionUtils.isEmpty(registrationRequest.getDistEnvEndPoints())) { registrationRequest.setDistEnvEndPoints(config.getUebServers()); registrationRequest.setManagerApiPublicKey(config.getUebPublicKey()); registrationRequest.setManagerApiSecretKey(config.getUebSecretKey()); @@ -79,210 +87,148 @@ public class DistributionBusinessLogic { registrationRequest.setManagerApiSecretKey(environment.getUebSecretKey()); } } - public Either<ServerListResponse, ResponseFormat> getUebServerList() { + public Either<ServerListResponse, ResponseFormat> getUebServerList() { DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager() - .getDistributionEngineConfiguration(); - + .getDistributionEngineConfiguration(); List<String> serverList = distributionEngineConfiguration.getUebServers(); - if (serverList != null && !serverList.isEmpty()) { - ServerListResponse serverListResponse = new ServerListResponse(); - serverListResponse.setUebServerList(serverList); - return Either.left(serverListResponse); } else { - ResponseFormat errorResponseWrapper = getResponseFormatManager() - .getResponseFormat(ActionStatus.GENERAL_ERROR); + ResponseFormat errorResponseWrapper = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR); return Either.right(errorResponseWrapper); } - } - public void handleRegistration(Wrapper<Response> responseWrapper, RegistrationRequest registrationRequest, - AuditHandler auditHandler) { + public void handleRegistration(Wrapper<Response> responseWrapper, RegistrationRequest registrationRequest, AuditHandler auditHandler) { CambriaErrorResponse registerResponse = null; try { DistributionEngineConfiguration config = getConfigurationManager().getDistributionEngineConfiguration(); - String statusTopicName = buildTopicName(config.getDistributionStatusTopicName(), - registrationRequest.getDistrEnvName()); + String statusTopicName = buildTopicName(config.getDistributionStatusTopicName(), registrationRequest.getDistrEnvName()); initRequestEnvEndPointsAndKeys(registrationRequest, config); - registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, - SubscriberTypeEnum.PRODUCER, statusTopicName); - + registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, SubscriberTypeEnum.PRODUCER, statusTopicName); auditHandler.auditRegisterACL(registerResponse, SubscriberTypeEnum.PRODUCER, - DistributionTopicData.newBuilder() - .statusTopic(statusTopicName) - .build()); + DistributionTopicData.newBuilder().statusTopic(statusTopicName).build()); boolean isRegisteredAsProducerOnStatusSuccess = responseWrapper.isEmpty(); - // Story [347698] Distribution Client Get Indication from + // component whether to register as consumer and producer on + // status topic boolean registeredAsConsumerOnStatus = false; if (isRegisteredAsProducerOnStatusSuccess && isTrue(registrationRequest.getIsConsumerToSdcDistrStatusTopic())) { - registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, - SubscriberTypeEnum.CONSUMER, statusTopicName); + registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, SubscriberTypeEnum.CONSUMER, + statusTopicName); auditHandler.auditRegisterACL(registerResponse, SubscriberTypeEnum.CONSUMER, - DistributionTopicData.newBuilder() - .statusTopic(statusTopicName) - .build()); + DistributionTopicData.newBuilder().statusTopic(statusTopicName).build()); registeredAsConsumerOnStatus = responseWrapper.isEmpty(); - } - if (responseWrapper.isEmpty()) { - String notificationTopicName = buildTopicName(config.getDistributionNotifTopicName(), - registrationRequest.getDistrEnvName()); - registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, - SubscriberTypeEnum.CONSUMER, notificationTopicName); + String notificationTopicName = buildTopicName(config.getDistributionNotifTopicName(), registrationRequest.getDistrEnvName()); + registerResponse = registerDistributionClientToTopic(responseWrapper, registrationRequest, SubscriberTypeEnum.CONSUMER, + notificationTopicName); auditHandler.auditRegisterACL(registerResponse, SubscriberTypeEnum.CONSUMER, - DistributionTopicData.newBuilder() - .notificationTopic(notificationTopicName) - .build()); + DistributionTopicData.newBuilder().notificationTopic(notificationTopicName).build()); } // Unregister Rollback if (!responseWrapper.isEmpty()) { if (isRegisteredAsProducerOnStatusSuccess) { - CambriaErrorResponse unRegisterResponse = unRegisterDistributionClientFromTopic(registrationRequest, - SubscriberTypeEnum.PRODUCER, statusTopicName); + CambriaErrorResponse unRegisterResponse = unRegisterDistributionClientFromTopic(registrationRequest, SubscriberTypeEnum.PRODUCER, + statusTopicName); auditHandler.auditUnRegisterACL(unRegisterResponse, SubscriberTypeEnum.PRODUCER, - DistributionTopicData.newBuilder() - .statusTopic(statusTopicName) - .build()); + DistributionTopicData.newBuilder().statusTopic(statusTopicName).build()); } if (registeredAsConsumerOnStatus) { - CambriaErrorResponse unRegisterResponse = unRegisterDistributionClientFromTopic(registrationRequest, - SubscriberTypeEnum.CONSUMER, statusTopicName); + CambriaErrorResponse unRegisterResponse = unRegisterDistributionClientFromTopic(registrationRequest, SubscriberTypeEnum.CONSUMER, + statusTopicName); auditHandler.auditUnRegisterACL(unRegisterResponse, SubscriberTypeEnum.CONSUMER, - DistributionTopicData.newBuilder() - .statusTopic(statusTopicName) - .build()); + DistributionTopicData.newBuilder().statusTopic(statusTopicName).build()); } } - if (responseWrapper.isEmpty()) { TopicRegistrationResponse okTopicResponse = buildTopicResponse(registrationRequest); responseWrapper.setInnerElement(Response.status(HttpStatus.SC_OK).entity(okTopicResponse).build()); } - } catch (Exception e) { log.error("registration to topic failed", e); - BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(REGISTER_IN_DISTRIBUTION_ENGINE, - "registration of subscriber to topic"); - Response errorResponse = buildErrorResponse( - getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); + BeEcompErrorManager.getInstance() + .logBeDistributionEngineSystemError(REGISTER_IN_DISTRIBUTION_ENGINE, "registration of subscriber to topic"); + Response errorResponse = buildErrorResponse(getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); responseWrapper.setInnerElement(errorResponse); } finally { auditHandler.auditRegisterRequest(registerResponse); } } - public void handleUnRegistration(Wrapper<Response> responseWrapper, RegistrationRequest unRegistrationRequest, - AuditHandler auditHandler) { + public void handleUnRegistration(Wrapper<Response> responseWrapper, RegistrationRequest unRegistrationRequest, AuditHandler auditHandler) { Wrapper<CambriaErrorResponse> cambriaResponseWrapper = new Wrapper<>(); try { String statusTopicName = getStatusTopicName(unRegistrationRequest.getDistrEnvName()); - DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager() - .getDistributionEngineConfiguration(); + DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration(); initRequestEnvEndPointsAndKeys(unRegistrationRequest, config); - CambriaErrorResponse unregisterClientProducerTopicResponse = unRegisterDistributionClientFromTopic( - unRegistrationRequest, SubscriberTypeEnum.PRODUCER, statusTopicName); + CambriaErrorResponse unregisterClientProducerTopicResponse = unRegisterDistributionClientFromTopic(unRegistrationRequest, + SubscriberTypeEnum.PRODUCER, statusTopicName); auditHandler.auditUnRegisterACL(unregisterClientProducerTopicResponse, SubscriberTypeEnum.PRODUCER, - DistributionTopicData.newBuilder() - .statusTopic(statusTopicName) - .build()); + DistributionTopicData.newBuilder().statusTopic(statusTopicName).build()); updateResponseWrapper(cambriaResponseWrapper, unregisterClientProducerTopicResponse); - String notificationTopicName = getNotificationTopicName(unRegistrationRequest.getDistrEnvName()); - CambriaErrorResponse unregisterClientConsumerTopicResponse = unRegisterDistributionClientFromTopic( - unRegistrationRequest, SubscriberTypeEnum.CONSUMER, notificationTopicName); + CambriaErrorResponse unregisterClientConsumerTopicResponse = unRegisterDistributionClientFromTopic(unRegistrationRequest, + SubscriberTypeEnum.CONSUMER, notificationTopicName); auditHandler.auditUnRegisterACL(unregisterClientConsumerTopicResponse, SubscriberTypeEnum.CONSUMER, - DistributionTopicData.newBuilder() - .notificationTopic(notificationTopicName) - .build()); + DistributionTopicData.newBuilder().notificationTopic(notificationTopicName).build()); updateResponseWrapper(cambriaResponseWrapper, unregisterClientConsumerTopicResponse); - // Success unregister both topics TopicUnregistrationResponse unregisterResponse = new TopicUnregistrationResponse( - getNotificationTopicName(unRegistrationRequest.getDistrEnvName()), - getStatusTopicName(unRegistrationRequest.getDistrEnvName()), - unregisterClientConsumerTopicResponse.getOperationStatus(), - unregisterClientProducerTopicResponse.getOperationStatus()); - + getNotificationTopicName(unRegistrationRequest.getDistrEnvName()), getStatusTopicName(unRegistrationRequest.getDistrEnvName()), + unregisterClientConsumerTopicResponse.getOperationStatus(), unregisterClientProducerTopicResponse.getOperationStatus()); if (cambriaResponseWrapper.getInnerElement().getOperationStatus() == CambriaOperationStatus.OK) { responseWrapper.setInnerElement(Response.status(HttpStatus.SC_OK).entity(unregisterResponse).build()); } else { - BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(UN_REGISTER_IN_DISTRIBUTION_ENGINE, - "unregistration failed"); - responseWrapper.setInnerElement( - Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(unregisterResponse).build()); + BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(UN_REGISTER_IN_DISTRIBUTION_ENGINE, "unregistration failed"); + responseWrapper.setInnerElement(Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(unregisterResponse).build()); } } catch (Exception e) { log.error("unregistered to topic failed", e); - Response errorResponse = buildErrorResponse( - getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); + Response errorResponse = buildErrorResponse(getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); responseWrapper.setInnerElement(errorResponse); - } finally { auditHandler.auditUnRegisterRequest(cambriaResponseWrapper.getInnerElement()); } } - private void updateResponseWrapper(Wrapper<CambriaErrorResponse> cambriaResponseWrapper, - CambriaErrorResponse currentResponse) { + private void updateResponseWrapper(Wrapper<CambriaErrorResponse> cambriaResponseWrapper, CambriaErrorResponse currentResponse) { if (cambriaResponseWrapper.isEmpty()) { cambriaResponseWrapper.setInnerElement(currentResponse); } else if (currentResponse.getOperationStatus() != CambriaOperationStatus.OK) { cambriaResponseWrapper.setInnerElement(currentResponse); - } - } - public static String getNotificationTopicName(String envName) { - DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager() - .getDistributionEngineConfiguration(); - return DistributionEngineInitTask.buildTopicName(config.getDistributionNotifTopicName(), envName); - - } - - public static String getStatusTopicName(String envName) { - DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager() - .getDistributionEngineConfiguration(); - return DistributionEngineInitTask.buildTopicName(config.getDistributionStatusTopicName(), envName); - - } - - protected CambriaErrorResponse unRegisterDistributionClientFromTopic(RegistrationRequest unRegistrationRequest, - SubscriberTypeEnum subscriberType, String topicName) { - - log.debug("unregistering client as {} , from topic: {}, using DistEnvPoints: {}", subscriberType, topicName, unRegistrationRequest.getDistEnvEndPoints()); + protected CambriaErrorResponse unRegisterDistributionClientFromTopic(RegistrationRequest unRegistrationRequest, SubscriberTypeEnum subscriberType, + String topicName) { + log.debug("unregistering client as {} , from topic: {}, using DistEnvPoints: {}", subscriberType, topicName, + unRegistrationRequest.getDistEnvEndPoints()); return cambriaHandler.unRegisterFromTopic(unRegistrationRequest.getDistEnvEndPoints(), unRegistrationRequest.getManagerApiPublicKey(), - unRegistrationRequest.getManagerApiSecretKey(), unRegistrationRequest.getApiPublicKey(), subscriberType, topicName); + unRegistrationRequest.getManagerApiSecretKey(), unRegistrationRequest.getApiPublicKey(), subscriberType, topicName); } private TopicRegistrationResponse buildTopicResponse(RegistrationRequest registrationRequest) { - DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager() - .getDistributionEngineConfiguration(); - String statusTopicName = DistributionEngineInitTask.buildTopicName(config.getDistributionStatusTopicName(), - registrationRequest.getDistrEnvName()); - String notificationTopicName = DistributionEngineInitTask.buildTopicName(config.getDistributionNotifTopicName(), - registrationRequest.getDistrEnvName()); - + DistributionEngineConfiguration config = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration(); + String statusTopicName = DistributionEngineInitTask + .buildTopicName(config.getDistributionStatusTopicName(), registrationRequest.getDistrEnvName()); + String notificationTopicName = DistributionEngineInitTask + .buildTopicName(config.getDistributionNotifTopicName(), registrationRequest.getDistrEnvName()); TopicRegistrationResponse topicResponse = new TopicRegistrationResponse(); topicResponse.setDistrNotificationTopicName(notificationTopicName); topicResponse.setDistrStatusTopicName(statusTopicName); return topicResponse; } - protected CambriaErrorResponse registerDistributionClientToTopic(Wrapper<Response> responseWrapper, - RegistrationRequest registrationRequest, SubscriberTypeEnum subscriberType, String topicName) { - + protected CambriaErrorResponse registerDistributionClientToTopic(Wrapper<Response> responseWrapper, RegistrationRequest registrationRequest, + SubscriberTypeEnum subscriberType, String topicName) { String errorMsg; - // Register for notifications as consumer if (subscriberType == SubscriberTypeEnum.CONSUMER) { errorMsg = "registration of subscriber to topic:" + topicName + " as consumer failed"; @@ -291,24 +237,21 @@ public class DistributionBusinessLogic { else { errorMsg = "registration of subscriber to topic:" + topicName + " as producer failed"; } - log.debug("registering client as {} , from topic: {}, using DistEnvPoints: {}", subscriberType, topicName, registrationRequest.getDistEnvEndPoints()); - CambriaErrorResponse registerToTopic = cambriaHandler.registerToTopic(registrationRequest.getDistEnvEndPoints(), - registrationRequest.getManagerApiPublicKey(), registrationRequest.getManagerApiSecretKey(), registrationRequest.getApiPublicKey(), - subscriberType, topicName); - + log.debug("registering client as {} , from topic: {}, using DistEnvPoints: {}", subscriberType, topicName, + registrationRequest.getDistEnvEndPoints()); + CambriaErrorResponse registerToTopic = cambriaHandler + .registerToTopic(registrationRequest.getDistEnvEndPoints(), registrationRequest.getManagerApiPublicKey(), + registrationRequest.getManagerApiSecretKey(), registrationRequest.getApiPublicKey(), subscriberType, topicName); if (registerToTopic.getOperationStatus() != CambriaOperationStatus.OK) { - Response failedRegistrationResponse = buildErrorResponse( - getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); - BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(REGISTER_IN_DISTRIBUTION_ENGINE, - errorMsg); + Response failedRegistrationResponse = buildErrorResponse(getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR)); + BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(REGISTER_IN_DISTRIBUTION_ENGINE, errorMsg); responseWrapper.setInnerElement(failedRegistrationResponse); } return registerToTopic; } protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) { - return Response.status(requestErrorWrapper.getStatus()) - .entity(gson.toJson(requestErrorWrapper.getRequestError())).build(); + return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build(); } public ResponseFormatManager getResponseFormatManager() { @@ -318,5 +261,4 @@ public class DistributionBusinessLogic { public IDistributionEngine getDistributionEngine() { return distributionEngine; } - } |