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.concurrent.Callable;
21 import java.util.function.BiFunction;
22 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier;
26 import org.openecomp.sdcrests.item.rest.services.catalog.notification.EntryNotConfiguredException;
27 import org.openecomp.sdcrests.item.rest.services.catalog.notification.Notifier;
28 import org.openecomp.sdcrests.item.types.ItemAction;
31 * Notifies the Catalog via an HTTP.
36 public class HttpTaskProducer
37 implements BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>>, Notifier {
39 private static final Logger LOGGER = LoggerFactory.getLogger(HttpTaskProducer.class);
41 private static final String CATALOG_HTTP_PROTOCOL = "HTTP";
42 private static final String CATALOG_HTTPS_PROTOCOL = "HTTPS";
44 private final String notifyCatalogUrl;
47 * Initializes the producer from a provided configuration.
49 * @param config HTTP-specific configuration, cannot be null
51 public HttpTaskProducer(HttpConfiguration config) {
52 String protocol = ensureEntryConfigured(config.getCatalogBeProtocol(), "Protocol");
53 String host = ensureEntryConfigured(config.getCatalogBeFqdn(), "Catalog host");
54 String url = ensureEntryConfigured(config.getCatalogNotificationUrl(), "Notification URL");
55 String port = getPortConfiguration(protocol, config);
56 this.notifyCatalogUrl = String.format(url, protocol, host, port);
59 private static String ensureEntryConfigured(String value, String entryName) {
62 throw new EntryNotConfiguredException(entryName);
68 private static String getPortConfiguration(String protocol, HttpConfiguration config) {
70 if (CATALOG_HTTP_PROTOCOL.equalsIgnoreCase(protocol)) {
71 return ensureEntryConfigured(config.getCatalogBeHttpPort(), "HTTP port");
72 } else if (CATALOG_HTTPS_PROTOCOL.equalsIgnoreCase(protocol)) {
73 return ensureEntryConfigured(config.getCatalogBeSslPort(), "SSL port");
75 throw new IllegalArgumentException("Unsupported protocol: " + protocol);
80 public Callable<AsyncNotifier.NextAction> apply(Collection<String> itemIds, ItemAction action) {
81 return createNotificationTask(itemIds, action);
84 private static String getEndpoint(ItemAction action) {
86 if (action == ItemAction.ARCHIVE) {
88 } else if (action == ItemAction.RESTORE) {
91 throw new IllegalArgumentException("Unsupported action: " + action.name());
96 public void execute(Collection<String> itemIds, ItemAction action) {
97 HttpNotificationTask task = createNotificationTask(itemIds, action);
101 private HttpNotificationTask createNotificationTask(Collection<String> itemIds, ItemAction action) {
102 String userId = SessionContextProviderFactory.getInstance().createInterface().get().getUser().getUserId();
103 String notificationEndpoint = notifyCatalogUrl + getEndpoint(action);
104 LOGGER.debug("Catalog notification URL: " + notificationEndpoint);
105 return new HttpNotificationTask(notificationEndpoint, userId, itemIds);