Renaming openecomp to onap
[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  * 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   private static final Logger LOG = LoggerFactory.getInstance().getLogger(
49               ActiveInventoryConfig.class);
50   private static final String HTTP_SCHEME = "http";
51   private static final String HTTPS_SCHEME = "https";
52
53   public static ActiveInventoryConfig getConfig() throws Exception {
54     if (instance == null) {
55       instance = new ActiveInventoryConfig();
56     }
57
58     return instance;
59   }
60
61   private ActiveInventoryRestConfig aaiRestConfig;
62   private ActiveInventorySslConfig aaiSslConfig;
63   private TaskProcessorConfig taskProcessorConfig;
64
65   /**
66    * Instantiates a new active inventory config.
67    *
68    * @throws Exception the exception
69    */
70   protected ActiveInventoryConfig() throws Exception {
71
72     Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
73     aaiRestConfig = new ActiveInventoryRestConfig(props);
74     aaiSslConfig = new ActiveInventorySslConfig(props, new Encryptor());
75
76     taskProcessorConfig = new TaskProcessorConfig();
77     taskProcessorConfig
78         .initializeFromProperties(ConfigHelper.getConfigWithPrefix("aai.taskProcessor", props));
79
80
81   }
82   
83   protected ActiveInventoryConfig(Properties props) throws Exception {
84
85     aaiRestConfig = new ActiveInventoryRestConfig(props);
86     aaiSslConfig = new ActiveInventorySslConfig(props, new Encryptor());
87
88     taskProcessorConfig = new TaskProcessorConfig();
89     taskProcessorConfig
90         .initializeFromProperties(ConfigHelper.getConfigWithPrefix("aai.taskProcessor", props));
91
92
93   }
94
95   public TaskProcessorConfig getTaskProcessorConfig() {
96     return taskProcessorConfig;
97   }
98
99   public void setTaskProcessorConfig(TaskProcessorConfig taskProcessorConfig) {
100     this.taskProcessorConfig = taskProcessorConfig;
101   }
102
103
104   public ActiveInventoryRestConfig getAaiRestConfig() {
105     return aaiRestConfig;
106   }
107
108   public void setAaiRestConfig(ActiveInventoryRestConfig aaiRestConfig) {
109     this.aaiRestConfig = aaiRestConfig;
110   }
111
112   public ActiveInventorySslConfig getAaiSslConfig() {
113     return aaiSslConfig;
114   }
115
116   public void setAaiSslConfig(ActiveInventorySslConfig aaiSslConfig) {
117     this.aaiSslConfig = aaiSslConfig;
118   }
119   
120   public String repairSelfLink(String selflink) {
121
122     if (selflink == null) {
123       return selflink;
124     }
125
126     UriBuilder builder = UriBuilder.fromPath(selflink).host(aaiRestConfig.getHost())
127         .port(Integer.parseInt(aaiRestConfig.getPort()));
128
129     switch (aaiRestConfig.getAuthenticationMode()) {
130
131       case SSL_BASIC:
132       case SSL_CERT: {
133         builder.scheme(HTTPS_SCHEME);
134         break;
135       }
136
137       default: {
138         builder.scheme(HTTP_SCHEME);
139       }
140     }
141
142     return builder.build().toString();
143
144   }
145   
146   public static String extractResourcePath(String selflink) {
147             try {
148               return new URI(selflink).getPath();
149             } catch (URISyntaxException uriSyntaxException) {
150               LOG.error(AaiUiMsgs.ERROR_EXTRACTING_RESOURCE_PATH_FROM_LINK, uriSyntaxException.getMessage());
151               return selflink;
152             }
153           }
154   
155   /* (non-Javadoc)
156    * @see java.lang.Object#toString()
157    */
158   @Override
159   public String toString() {
160     return "ActiveInventoryConfig [aaiRestConfig=" + aaiRestConfig + ", aaiSslConfig="
161         + aaiSslConfig + "]";
162   }
163
164   public URI getBaseUri() {
165     return UriBuilder.fromUri("https://" + aaiRestConfig.getHost() + ":" + aaiRestConfig.getPort()
166         + aaiRestConfig.getResourceBasePath()).build();
167   }
168
169 }