Adding back-end support for UI filters
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / aai / config / ActiveInventoryConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.aai.config;
24
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Properties;
28
29 import javax.ws.rs.core.UriBuilder;
30
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.synchronizer.config.TaskProcessorConfig;
33 import org.onap.aai.sparky.util.ConfigHelper;
34 import org.onap.aai.sparky.util.Encryptor;
35 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
36 import org.onap.aai.cl.api.Logger;
37 import org.onap.aai.cl.eelf.LoggerFactory;
38
39 /**
40  * The Class ActiveInventoryConfig.
41  */
42 public class ActiveInventoryConfig {
43
44
45
46   public static final String CONFIG_FILE =
47       TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "aai.properties";
48   private static ActiveInventoryConfig instance;
49   private static final Logger LOG =
50       LoggerFactory.getInstance().getLogger(ActiveInventoryConfig.class);
51   private static final String HTTP_SCHEME = "http";
52   private static final String HTTPS_SCHEME = "https";
53
54   public static ActiveInventoryConfig getConfig() throws Exception {
55     if (instance == null) {
56       instance = new ActiveInventoryConfig();
57     }
58
59     return instance;
60   }
61
62   private ActiveInventoryRestConfig aaiRestConfig;
63   private ActiveInventorySslConfig aaiSslConfig;
64   private TaskProcessorConfig taskProcessorConfig;
65
66   /**
67    * Instantiates a new active inventory config.
68    *
69    * @throws Exception the exception
70    */
71   protected ActiveInventoryConfig() throws Exception {
72
73     Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
74     aaiRestConfig = new ActiveInventoryRestConfig(props);
75     aaiSslConfig = new ActiveInventorySslConfig(props, new Encryptor());
76
77     taskProcessorConfig = new TaskProcessorConfig();
78     taskProcessorConfig
79         .initializeFromProperties(ConfigHelper.getConfigWithPrefix("aai.taskProcessor", props));
80
81
82   }
83
84   protected ActiveInventoryConfig(Properties props) throws Exception {
85
86     aaiRestConfig = new ActiveInventoryRestConfig(props);
87     aaiSslConfig = new ActiveInventorySslConfig(props, new Encryptor());
88
89     taskProcessorConfig = new TaskProcessorConfig();
90     taskProcessorConfig
91         .initializeFromProperties(ConfigHelper.getConfigWithPrefix("aai.taskProcessor", props));
92
93
94   }
95
96   public TaskProcessorConfig getTaskProcessorConfig() {
97     return taskProcessorConfig;
98   }
99
100   public void setTaskProcessorConfig(TaskProcessorConfig taskProcessorConfig) {
101     this.taskProcessorConfig = taskProcessorConfig;
102   }
103
104
105   public ActiveInventoryRestConfig getAaiRestConfig() {
106     return aaiRestConfig;
107   }
108
109   public void setAaiRestConfig(ActiveInventoryRestConfig aaiRestConfig) {
110     this.aaiRestConfig = aaiRestConfig;
111   }
112
113   public ActiveInventorySslConfig getAaiSslConfig() {
114     return aaiSslConfig;
115   }
116
117   public void setAaiSslConfig(ActiveInventorySslConfig aaiSslConfig) {
118     this.aaiSslConfig = aaiSslConfig;
119   }
120
121   public String repairSelfLink(String selflink) {
122
123     if (selflink == null) {
124       return selflink;
125     }
126
127     UriBuilder builder = UriBuilder.fromPath(selflink).host(aaiRestConfig.getHost())
128         .port(Integer.parseInt(aaiRestConfig.getPort()));
129
130     switch (aaiRestConfig.getAuthenticationMode()) {
131
132       case SSL_BASIC:
133       case SSL_CERT: {
134         builder.scheme(HTTPS_SCHEME);
135         break;
136       }
137
138       default: {
139         builder.scheme(HTTP_SCHEME);
140       }
141     }
142
143     return builder.build().toString();
144
145   }
146
147   public static String extractResourcePath(String selflink) {
148     try {
149       return new URI(selflink).getPath();
150     } catch (URISyntaxException uriSyntaxException) {
151       LOG.error(AaiUiMsgs.ERROR_EXTRACTING_RESOURCE_PATH_FROM_LINK,
152           uriSyntaxException.getMessage());
153       return selflink;
154     }
155   }
156
157   /*
158    * (non-Javadoc)
159    * 
160    * @see java.lang.Object#toString()
161    */
162   @Override
163   public String toString() {
164     return "ActiveInventoryConfig [aaiRestConfig=" + aaiRestConfig + ", aaiSslConfig="
165         + aaiSslConfig + "]";
166   }
167
168   public URI getBaseUri() {
169     return UriBuilder.fromUri("https://" + aaiRestConfig.getHost() + ":" + aaiRestConfig.getPort()
170         + aaiRestConfig.getResourceBasePath()).build();
171   }
172
173 }