d210dc21afcf6877b1480151fc55102655676500
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
18
19 import java.util.Collection;
20 import java.util.EnumMap;
21 import java.util.Map;
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;
31
32 /**
33  * Notifies the Catalog via an HTTP.
34  *
35  * @author evitaliy
36  * @since 21 Nov 2018
37  */
38 public class HttpTaskProducer
39         implements BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>>, Notifier {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(HttpTaskProducer.class);
42
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;
46
47     static {
48         ACTION_PATHS = new EnumMap<>(ItemAction.class);
49         ACTION_PATHS.put(ItemAction.ARCHIVE, "archived");
50         ACTION_PATHS.put(ItemAction.RESTORE, "restored");
51     }
52
53     private final String notifyCatalogUrl;
54
55     /**
56      * Initializes the producer from a provided configuration.
57      *
58      * @param config HTTP-specific configuration, cannot be null
59      */
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);
66     }
67
68     private static String ensureEntryConfigured(String value, String entryName) {
69
70         if (value == null) {
71             throw new EntryNotConfiguredException(entryName);
72         }
73
74         return value;
75     }
76
77     private static String getPortConfiguration(String protocol, HttpConfiguration config) {
78
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");
83         } else {
84             throw new IllegalArgumentException("Unsupported protocol: " + protocol);
85         }
86     }
87
88     @Override
89     public Callable<AsyncNotifier.NextAction> apply(Collection<String> itemIds, ItemAction action) {
90         return createNotificationTask(itemIds, action);
91     }
92
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);
98     }
99
100     static String getApiPath(ItemAction action) {
101
102         String path = ACTION_PATHS.get(action);
103         if (path == null) {
104             throw new IllegalArgumentException("Unsupported action: " + action.name());
105         }
106
107         return path;
108     }
109
110     @Override
111     public void execute(Collection<String> itemIds, ItemAction action) {
112         HttpNotificationTask task = createNotificationTask(itemIds, action);
113         task.call();
114     }
115
116 }