c6abd346ffe83652ea93f5927689a46e8dbb0e5a
[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.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;
29
30 /**
31  * Notifies the Catalog via an HTTP.
32  *
33  * @author evitaliy
34  * @since 21 Nov 2018
35  */
36 public class HttpTaskProducer
37         implements BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>>, Notifier {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(HttpTaskProducer.class);
40
41     private static final String CATALOG_HTTP_PROTOCOL = "HTTP";
42     private static final String CATALOG_HTTPS_PROTOCOL = "HTTPS";
43
44     private final String notifyCatalogUrl;
45
46     /**
47      * Initializes the producer from a provided configuration.
48      *
49      * @param config HTTP-specific configuration, cannot be null
50      */
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);
57     }
58
59     private static String ensureEntryConfigured(String value, String entryName) {
60
61         if (value == null) {
62             throw new EntryNotConfiguredException(entryName);
63         }
64
65         return value;
66     }
67
68     private static String getPortConfiguration(String protocol, HttpConfiguration config) {
69
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");
74         } else {
75             throw new IllegalArgumentException("Unsupported protocol: " + protocol);
76         }
77     }
78
79     @Override
80     public Callable<AsyncNotifier.NextAction> apply(Collection<String> itemIds, ItemAction action) {
81         return createNotificationTask(itemIds, action);
82     }
83
84     private static String getEndpoint(ItemAction action) {
85
86         if (action == ItemAction.ARCHIVE) {
87             return "archived";
88         } else if (action == ItemAction.RESTORE) {
89             return "restored";
90         } else {
91             throw new IllegalArgumentException("Unsupported action: " + action.name());
92         }
93     }
94
95     @Override
96     public void execute(Collection<String> itemIds, ItemAction action) {
97         HttpNotificationTask task = createNotificationTask(itemIds, action);
98         task.call();
99     }
100
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);
106     }
107 }