2 * Copyright © 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;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Collection;
25 import java.util.Objects;
26 import org.onap.sdc.tosca.services.YamlUtil;
27 import org.openecomp.sdc.logging.api.Logger;
28 import org.openecomp.sdc.logging.api.LoggerFactory;
29 import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpConfiguration;
30 import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpTaskProducer;
31 import org.openecomp.sdcrests.item.types.ItemAction;
34 * Creates an instance of {@link Notifier}, initialized according to current configuration.
35 * The configuration must be passed via the {@link #CONFIG_FILE_PROPERTY} JVM argument.
37 public class NotifierFactory {
39 private static final Logger LOGGER = LoggerFactory.getLogger(NotifierFactory.class);
41 private static final String CONFIG_FILE_PROPERTY = "configuration.yaml";
42 private static final String CONFIG_SECTION = "catalogNotificationsConfig";
44 private static final Notifier INSTANCE;
47 INSTANCE = createInstance();
51 * Returns a {@link Notifier} instance according to the provided configuration. If no configuration was not
52 * provided, or the given configuration is incorrect, then an instance with reduced functionality will be returned.
54 * @return available instance of {@link Notifier}
56 public static Notifier getInstance() {
60 static Notifier createInstance() {
64 String file = Objects.requireNonNull(System.getProperty(CONFIG_FILE_PROPERTY),
65 "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY);
67 Object config = getNotificationConfiguration(file);
68 ObjectMapper mapper = new ObjectMapper();
69 HttpConfiguration httpConfig = mapper.convertValue(config, HttpConfiguration.class);
71 return new AsyncNotifier(new HttpTaskProducer(httpConfig));
73 } catch (Exception e) {
74 LOGGER.warn("Failed to initialize notifier. Notifications will not be sent", e);
75 return new UnsupportedConfigurationNotifier();
79 private static Object getNotificationConfiguration(String file) throws IOException {
81 Map<?, ?> configuration = Objects.requireNonNull(readConfigurationFile(file), "Configuration cannot be empty");
82 Object notificationConfig = configuration.get(CONFIG_SECTION);
83 if (notificationConfig == null) {
84 throw new EntryNotConfiguredException(CONFIG_SECTION + " section");
87 return notificationConfig;
90 private static Map<?, ?> readConfigurationFile(String file) throws IOException {
92 try (InputStream fileInput = new FileInputStream(file)) {
93 YamlUtil yamlUtil = new YamlUtil();
94 return yamlUtil.yamlToMap(fileInput);
98 static class UnsupportedConfigurationNotifier implements Notifier {
101 public void execute(Collection<String> itemIds, ItemAction action) {
102 throw new IllegalStateException("Cannot send notifications. The notifier was not properly initialized");