2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
19 import java.util.Collection;
20 import java.util.EnumMap;
22 import java.util.concurrent.Callable;
23 import java.util.function.BiFunction;
24 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier;
28 import org.openecomp.sdcrests.item.rest.services.catalog.notification.EntryNotConfiguredException;
29 import org.openecomp.sdcrests.item.rest.services.catalog.notification.Notifier;
30 import org.openecomp.sdcrests.item.types.ItemAction;
33 * Notifies the Catalog via an HTTP.
38 public class HttpTaskProducer
39 implements BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>>, Notifier {
41 private static final Logger LOGGER = LoggerFactory.getLogger(HttpTaskProducer.class);
43 private static final String CATALOG_HTTP_PROTOCOL = "HTTP";
44 private static final String CATALOG_HTTPS_PROTOCOL = "HTTPS";
45 private static final Map<ItemAction, String> ACTION_PATHS;
48 ACTION_PATHS = new EnumMap<>(ItemAction.class);
49 ACTION_PATHS.put(ItemAction.ARCHIVE, "archived");
50 ACTION_PATHS.put(ItemAction.RESTORE, "restored");
53 private final String notifyCatalogUrl;
56 * Initializes the producer from a provided configuration.
58 * @param config HTTP-specific configuration, cannot be null
60 public HttpTaskProducer(HttpConfiguration config) {
61 String protocol = ensureEntryConfigured(config.getCatalogBeProtocol(), "Protocol");
62 String host = ensureEntryConfigured(config.getCatalogBeFqdn(), "Catalog host");
63 String url = ensureEntryConfigured(config.getCatalogNotificationUrl(), "Notification URL");
64 String port = getPortConfiguration(protocol, config);
65 this.notifyCatalogUrl = String.format(url, protocol, host, port);
68 private static String ensureEntryConfigured(String value, String entryName) {
71 throw new EntryNotConfiguredException(entryName);
77 private static String getPortConfiguration(String protocol, HttpConfiguration config) {
79 if (CATALOG_HTTP_PROTOCOL.equalsIgnoreCase(protocol)) {
80 return ensureEntryConfigured(config.getCatalogBeHttpPort(), "HTTP port");
81 } else if (CATALOG_HTTPS_PROTOCOL.equalsIgnoreCase(protocol)) {
82 return ensureEntryConfigured(config.getCatalogBeSslPort(), "SSL port");
84 throw new IllegalArgumentException("Unsupported protocol: " + protocol);
89 public Callable<AsyncNotifier.NextAction> apply(Collection<String> itemIds, ItemAction action) {
90 return createNotificationTask(itemIds, action);
93 private HttpNotificationTask createNotificationTask(Collection<String> itemIds, ItemAction action) {
94 String userId = SessionContextProviderFactory.getInstance().createInterface().get().getUser().getUserId();
95 String notificationEndpoint = notifyCatalogUrl + getApiPath(action);
96 LOGGER.debug("Catalog notification URL: {}", notificationEndpoint);
97 return new HttpNotificationTask(notificationEndpoint, userId, itemIds);
100 static String getApiPath(ItemAction action) {
102 String path = ACTION_PATHS.get(action);
104 throw new IllegalArgumentException("Unsupported action: " + action.name());
111 public void execute(Collection<String> itemIds, ItemAction action) {
112 HttpNotificationTask task = createNotificationTask(itemIds, action);