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