a7f1e9c7fbca5c34b15a168434f5af3bdebbbd34
[sdc.git] /
1 /*
2  * Copyright © 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;
18
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;
24 import java.util.Map;
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;
32
33 /**
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.
36  */
37 public class NotifierFactory {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(NotifierFactory.class);
40
41     private static final String CONFIG_FILE_PROPERTY = "configuration.yaml";
42     private static final String CONFIG_SECTION = "catalogNotificationsConfig";
43
44     private static final Notifier INSTANCE;
45
46     static {
47         INSTANCE = createInstance();
48     }
49
50     /**
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.
53      *
54      * @return available instance of {@link Notifier}
55      */
56     public static Notifier getInstance() {
57         return INSTANCE;
58     }
59
60     static Notifier createInstance() {
61
62         try {
63
64             String file = Objects.requireNonNull(System.getProperty(CONFIG_FILE_PROPERTY),
65                     "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY);
66
67             Object config = getNotificationConfiguration(file);
68             ObjectMapper mapper = new ObjectMapper();
69             HttpConfiguration httpConfig = mapper.convertValue(config, HttpConfiguration.class);
70
71             return new AsyncNotifier(new HttpTaskProducer(httpConfig));
72
73         } catch (Exception e) {
74             LOGGER.warn("Failed to initialize notifier. Notifications will not be sent", e);
75             return new UnsupportedConfigurationNotifier();
76         }
77     }
78
79     private static Object getNotificationConfiguration(String file) throws IOException {
80
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");
85         }
86
87         return notificationConfig;
88     }
89
90     private static Map<?, ?> readConfigurationFile(String file) throws IOException {
91
92         try (InputStream fileInput = new FileInputStream(file)) {
93             YamlUtil yamlUtil = new YamlUtil();
94             return yamlUtil.yamlToMap(fileInput);
95         }
96     }
97
98     static class UnsupportedConfigurationNotifier implements Notifier {
99
100         @Override
101         public void execute(Collection<String> itemIds, ItemAction action) {
102             throw new IllegalStateException("Cannot send notifications. The notifier was not properly initialized");
103         }
104     }
105 }