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