462ffdf00c0bee005dac845306fe8b092c063730
[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     private NotifierFactory() {
47         // prevent instantiation
48     }
49
50     static {
51         INSTANCE = createInstance();
52     }
53
54     /**
55      * Returns a {@link Notifier} instance according to the provided configuration. If no configuration was not
56      * provided, or the given configuration is incorrect, then an instance with reduced functionality will be returned.
57      *
58      * @return available instance of {@link Notifier}
59      */
60     public static Notifier getInstance() {
61         return INSTANCE;
62     }
63
64     static Notifier createInstance() {
65
66         try {
67
68             String file = Objects.requireNonNull(System.getProperty(CONFIG_FILE_PROPERTY),
69                     "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY);
70
71             Object config = getNotificationConfiguration(file);
72             ObjectMapper mapper = new ObjectMapper();
73             HttpConfiguration httpConfig = mapper.convertValue(config, HttpConfiguration.class);
74
75             return new AsyncNotifier(new HttpTaskProducer(httpConfig));
76
77         } catch (Exception e) {
78             LOGGER.warn("Failed to initialize notifier. Notifications will not be sent", e);
79             return new UnsupportedConfigurationNotifier();
80         }
81     }
82
83     private static Object getNotificationConfiguration(String file) throws IOException {
84
85         Map<?, ?> configuration = Objects.requireNonNull(readConfigurationFile(file), "Configuration cannot be empty");
86         Object notificationConfig = configuration.get(CONFIG_SECTION);
87         if (notificationConfig == null) {
88             throw new EntryNotConfiguredException(CONFIG_SECTION + " section");
89         }
90
91         return notificationConfig;
92     }
93
94     private static Map<?, ?> readConfigurationFile(String file) throws IOException {
95
96         try (InputStream fileInput = new FileInputStream(file)) {
97             YamlUtil yamlUtil = new YamlUtil();
98             return yamlUtil.yamlToMap(fileInput);
99         }
100     }
101
102     static class UnsupportedConfigurationNotifier implements Notifier {
103
104         @Override
105         public void execute(Collection<String> itemIds, ItemAction action) {
106             throw new IllegalStateException("Cannot send notifications. The notifier was not properly initialized");
107         }
108     }
109 }