From d378c37fbd1ecec7b43394926f1ca32a695e07de Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 22 Mar 2021 15:33:06 +0000 Subject: Reformat openecomp-be Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3449 Change-Id: I13e02322f8e00820cc5a1d85752caaeda9bf10d1 --- .../catalog/notification/AsyncNotifier.java | 26 +++--------------- .../notification/EntryNotConfiguredException.java | 1 - .../services/catalog/notification/Notifier.java | 1 - .../catalog/notification/NotifierFactory.java | 29 +++++++------------- .../notification/http/HttpConfiguration.java | 1 - .../notification/http/HttpNotificationTask.java | 31 ++++------------------ .../notification/http/HttpTaskProducer.java | 27 +++++++------------ 7 files changed, 26 insertions(+), 90 deletions(-) (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java index 872c61e480..acdd1ae5aa 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification; import java.util.Collection; @@ -38,21 +37,17 @@ import org.openecomp.sdcrests.item.types.ItemAction; public class AsyncNotifier implements Notifier { private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(1); - private static final int DEFAULT_NUM_OF_RETRIES = 2; private static final long DEFAULT_INTERVAL = 5000; - private final BiFunction, ItemAction, Callable> taskProducer; private final int numberOfRetries; private final long retryInterval; - AsyncNotifier(BiFunction, ItemAction, Callable> taskProducer) { this(taskProducer, DEFAULT_NUM_OF_RETRIES, DEFAULT_INTERVAL); } - AsyncNotifier(BiFunction, ItemAction, Callable> taskProducer, int numOfRetries, - long retryInterval) { + AsyncNotifier(BiFunction, ItemAction, Callable> taskProducer, int numOfRetries, long retryInterval) { this.taskProducer = taskProducer; this.numberOfRetries = numOfRetries; this.retryInterval = retryInterval; @@ -60,30 +55,22 @@ public class AsyncNotifier implements Notifier { @Override public void execute(Collection itemIds, ItemAction action) { - Callable worker = taskProducer.apply(itemIds, action); - RetryingTask retryingTask = new RetryingTask(worker, numberOfRetries, retryInterval, EXECUTOR_SERVICE); - EXECUTOR_SERVICE.submit(LoggingContext.copyToCallable(retryingTask)); } - public enum NextAction { - RETRY, DONE - } + public enum NextAction {RETRY, DONE} static class RetryingTask implements Callable { private static final Logger LOGGER = LoggerFactory.getLogger(RetryingTask.class); - private final Callable worker; private final long delay; private final ScheduledExecutorService scheduler; private final AtomicInteger retries; - RetryingTask(Callable worker, int numOfRetries, long delay, - ScheduledExecutorService scheduler) { - + RetryingTask(Callable worker, int numOfRetries, long delay, ScheduledExecutorService scheduler) { this.worker = Objects.requireNonNull(worker); this.retries = new AtomicInteger(requirePositiveRetries(numOfRetries)); this.delay = requirePositiveDelay(delay); @@ -91,38 +78,31 @@ public class AsyncNotifier implements Notifier { } private int requirePositiveRetries(int number) { - if (number < 1) { throw new IllegalArgumentException("Number of retries must be positive"); } - return number; } private long requirePositiveDelay(long number) { - if (number < 1) { throw new IllegalArgumentException("Delay must be positive"); } - return number; } @Override public Void call() throws Exception { - NextAction next = worker.call(); if (next == NextAction.DONE) { LOGGER.debug("Task successful: {}. Not going to retry", worker); return null; } - int attempts = retries.decrementAndGet(); if (attempts < 1) { LOGGER.warn("Exhausted number of retries for task {}, exiting", worker); return null; } - scheduler.schedule(this, delay, TimeUnit.MILLISECONDS); return null; } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/EntryNotConfiguredException.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/EntryNotConfiguredException.java index 070164afe6..53ccac7503 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/EntryNotConfiguredException.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/EntryNotConfiguredException.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification; /** diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/Notifier.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/Notifier.java index 9143de4212..dae07ebc05 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/Notifier.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/Notifier.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification; import java.util.Collection; diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactory.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactory.java index 462ffdf00c..f0bb1648dc 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactory.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactory.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification; import com.fasterxml.jackson.databind.ObjectMapper; @@ -31,29 +30,27 @@ import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpT import org.openecomp.sdcrests.item.types.ItemAction; /** - * Creates an instance of {@link Notifier}, initialized according to current configuration. - * The configuration must be passed via the {@link #CONFIG_FILE_PROPERTY} JVM argument. + * Creates an instance of {@link Notifier}, initialized according to current configuration. The configuration must be passed via the {@link + * #CONFIG_FILE_PROPERTY} JVM argument. */ public class NotifierFactory { private static final Logger LOGGER = LoggerFactory.getLogger(NotifierFactory.class); - private static final String CONFIG_FILE_PROPERTY = "configuration.yaml"; private static final String CONFIG_SECTION = "catalogNotificationsConfig"; - private static final Notifier INSTANCE; - private NotifierFactory() { - // prevent instantiation - } - static { INSTANCE = createInstance(); } + private NotifierFactory() { + // prevent instantiation + } + /** - * Returns a {@link Notifier} instance according to the provided configuration. If no configuration was not - * provided, or the given configuration is incorrect, then an instance with reduced functionality will be returned. + * Returns a {@link Notifier} instance according to the provided configuration. If no configuration was not provided, or the given configuration + * is incorrect, then an instance with reduced functionality will be returned. * * @return available instance of {@link Notifier} */ @@ -62,18 +59,13 @@ public class NotifierFactory { } static Notifier createInstance() { - try { - String file = Objects.requireNonNull(System.getProperty(CONFIG_FILE_PROPERTY), - "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY); - + "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY); Object config = getNotificationConfiguration(file); ObjectMapper mapper = new ObjectMapper(); HttpConfiguration httpConfig = mapper.convertValue(config, HttpConfiguration.class); - return new AsyncNotifier(new HttpTaskProducer(httpConfig)); - } catch (Exception e) { LOGGER.warn("Failed to initialize notifier. Notifications will not be sent", e); return new UnsupportedConfigurationNotifier(); @@ -81,18 +73,15 @@ public class NotifierFactory { } private static Object getNotificationConfiguration(String file) throws IOException { - Map configuration = Objects.requireNonNull(readConfigurationFile(file), "Configuration cannot be empty"); Object notificationConfig = configuration.get(CONFIG_SECTION); if (notificationConfig == null) { throw new EntryNotConfiguredException(CONFIG_SECTION + " section"); } - return notificationConfig; } private static Map readConfigurationFile(String file) throws IOException { - try (InputStream fileInput = new FileInputStream(file)) { YamlUtil yamlUtil = new YamlUtil(); return yamlUtil.yamlToMap(fileInput); diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpConfiguration.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpConfiguration.java index 4403bd840b..e10cd5d8c3 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpConfiguration.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification.http; import lombok.AllArgsConstructor; diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpNotificationTask.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpNotificationTask.java index c88ac4ecce..1904bb5072 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpNotificationTask.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpNotificationTask.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification.http; import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE; @@ -42,8 +41,8 @@ import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier; /** - * HTTP client for notifying the Catalog of an action on items. The items are referenced by their IDs. The client can - * run multiple times, in which case only failed IDs will be re-attempted. + * HTTP client for notifying the Catalog of an action on items. The items are referenced by their IDs. The client can run multiple times, in which + * case only failed IDs will be re-attempted. * * @author evitaliy * @since 21 Nov 2018 @@ -52,10 +51,8 @@ import org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotif class HttpNotificationTask implements Callable { private static final Logger LOGGER = LoggerFactory.getLogger(HttpNotificationTask.class); - private static final String APPLICATION_JSON = ContentType.APPLICATION_JSON.getMimeType(); private static final String USER_ID_HEADER_PARAM = "USER_ID"; - private final String endpoint; private final String userId; private volatile Collection itemIds; @@ -68,59 +65,41 @@ class HttpNotificationTask implements Callable { @Override public synchronized AsyncNotifier.NextAction call() { - try (CloseableHttpClient client = HttpClients.createDefault()) { - HttpPost request = createPostRequest(endpoint, itemIds, userId); - try (CloseableHttpResponse response = client.execute(request)) { - StatusLine status = response.getStatusLine(); - - LOGGER.debug("Catalog notification on VSP IDs: {}, endpoint: {}, response: {}", - itemIds, endpoint, status); - + LOGGER.debug("Catalog notification on VSP IDs: {}, endpoint: {}, response: {}", itemIds, endpoint, status); itemIds = getFailedIds(itemIds, response.getEntity()); - - if ((status.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) - && (itemIds != null) && !itemIds.isEmpty()) { - + if ((status.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) && (itemIds != null) && !itemIds.isEmpty()) { LOGGER.debug("Catalog notification on VSP IDs {} failed. Endpoint: {}. Retry", itemIds, endpoint); return RETRY; } - return DONE; } - } catch (Exception e) { LOGGER.error("Catalog notification on VSP IDs {} failed. Endpoint: {}", itemIds, endpoint, e); return DONE; } } - private HttpPost createPostRequest(String postUrl, Collection itemIds, String userId) - throws UnsupportedEncodingException { - + private HttpPost createPostRequest(String postUrl, Collection itemIds, String userId) throws UnsupportedEncodingException { HttpPost request = new HttpPost(postUrl); - request.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON); request.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); request.addHeader(USER_ID_HEADER_PARAM, userId); - HttpEntity entity = new StringEntity(JsonUtil.object2Json(itemIds)); request.setEntity(entity); return request; } private Collection getFailedIds(Collection itemIds, HttpEntity responseBody) { - try { NotificationResponse response = JsonUtil.json2Object(responseBody.getContent(), NotificationResponse.class); return response != null ? response.failedIds : null; } catch (Exception e) { LOGGER.error("Error getting failed IDs from response", e); } - return itemIds; } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducer.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducer.java index d210dc21af..694aa9241c 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducer.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducer.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdcrests.item.rest.services.catalog.notification.http; import java.util.Collection; @@ -35,11 +34,9 @@ import org.openecomp.sdcrests.item.types.ItemAction; * @author evitaliy * @since 21 Nov 2018 */ -public class HttpTaskProducer - implements BiFunction, ItemAction, Callable>, Notifier { +public class HttpTaskProducer implements BiFunction, ItemAction, Callable>, Notifier { private static final Logger LOGGER = LoggerFactory.getLogger(HttpTaskProducer.class); - private static final String CATALOG_HTTP_PROTOCOL = "HTTP"; private static final String CATALOG_HTTPS_PROTOCOL = "HTTPS"; private static final Map ACTION_PATHS; @@ -66,16 +63,13 @@ public class HttpTaskProducer } private static String ensureEntryConfigured(String value, String entryName) { - if (value == null) { throw new EntryNotConfiguredException(entryName); } - return value; } private static String getPortConfiguration(String protocol, HttpConfiguration config) { - if (CATALOG_HTTP_PROTOCOL.equalsIgnoreCase(protocol)) { return ensureEntryConfigured(config.getCatalogBeHttpPort(), "HTTP port"); } else if (CATALOG_HTTPS_PROTOCOL.equalsIgnoreCase(protocol)) { @@ -85,6 +79,14 @@ public class HttpTaskProducer } } + static String getApiPath(ItemAction action) { + String path = ACTION_PATHS.get(action); + if (path == null) { + throw new IllegalArgumentException("Unsupported action: " + action.name()); + } + return path; + } + @Override public Callable apply(Collection itemIds, ItemAction action) { return createNotificationTask(itemIds, action); @@ -97,20 +99,9 @@ public class HttpTaskProducer return new HttpNotificationTask(notificationEndpoint, userId, itemIds); } - static String getApiPath(ItemAction action) { - - String path = ACTION_PATHS.get(action); - if (path == null) { - throw new IllegalArgumentException("Unsupported action: " + action.name()); - } - - return path; - } - @Override public void execute(Collection itemIds, ItemAction action) { HttpNotificationTask task = createNotificationTask(itemIds, action); task.call(); } - } -- cgit 1.2.3-korg