323c4dbe997422702f046e863ee939213275489b
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoJsonProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.properties;
22
23
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27
28 import org.codehaus.jackson.JsonNode;
29 import org.codehaus.jackson.JsonParseException;
30 import org.codehaus.jackson.map.JsonMappingException;
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.openecomp.mso.utils.CryptoUtils;
33
34
35 public class MsoJsonProperties extends AbstractMsoProperties {
36         
37         protected ObjectMapper mapper = new ObjectMapper();
38         
39         protected JsonNode jsonRootNode = mapper.createObjectNode();
40         
41         protected MsoJsonProperties() {
42                 
43         }
44
45         public synchronized JsonNode getJsonRootNode () {
46                 return this.jsonRootNode;
47         }
48
49         /**
50          * This method is used to get the text encrypted in the string value of the node.
51          * @param jsonNode The JsonNode containing the strig to decode
52          * @param defaultValue The default value in case of issue
53          * @param encryptionKey The encryption Key in AES 128 bits
54          * @return the String decrypted
55          */
56         public synchronized String getEncryptedProperty(JsonNode jsonNode, String defaultValue, String encryptionKey) {
57
58                 if (jsonNode.isTextual()) {
59                         try {
60                                 return CryptoUtils.decrypt(jsonNode.asText(), encryptionKey);
61                         } catch (GeneralSecurityException e) {
62                                 LOGGER.debug("Exception while decrypting property: " + jsonNode.asText(), e);
63                         }
64                 } 
65                 
66                 return defaultValue;
67         }
68         
69         /**
70          * This method load a properties file from a source path.
71          *
72          * @param propertiesPath The path to the file
73          * @throws IOException In case of issues during the opening
74          */
75         @Override
76         protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
77
78                 FileReader reader = null;
79         
80                 this.propertiesFileName = propertiesPath;
81                 
82                 try {
83                         // Clean
84                         this.jsonRootNode = mapper.createObjectNode();
85                         
86                         reader = new FileReader(propertiesPath);
87                         
88                         // Try a tree load
89                         this.jsonRootNode = mapper.readValue(reader, JsonNode.class);
90
91
92                 } finally {
93                         JsonNode reloadJsonProp = this.jsonRootNode.get(RELOAD_TIME_PROPERTY);
94                         if (reloadJsonProp != null) {
95                                 this.automaticRefreshInMinutes = reloadJsonProp.asInt(DEFAULT_RELOAD_TIME_MIN); 
96                         } else {
97                                 this.automaticRefreshInMinutes = DEFAULT_RELOAD_TIME_MIN;
98                         }
99                         
100                         // Always close the file
101                         try {
102                                 if (reader != null) {
103                                         reader.close();
104                                 }
105                         } catch (IOException e) {
106                                 LOGGER.debug("Exception while closing reader for file:" + propertiesFileName, e);
107                         }
108                 }
109         }
110
111         @Override
112         public synchronized MsoJsonProperties clone() {
113                 MsoJsonProperties msoCopy = new MsoJsonProperties();
114                 
115                 ObjectMapper mapper = new ObjectMapper();
116                 try {
117                         msoCopy.jsonRootNode = mapper.createObjectNode();
118                         msoCopy.jsonRootNode = mapper.readValue(this.jsonRootNode.toString(), JsonNode.class);
119                 } catch (JsonParseException e) {
120                         LOGGER.debug("JsonParseException when cloning the object:" + this.propertiesFileName, e);
121                 } catch (JsonMappingException e) {
122                         LOGGER.debug("JsonMappingException when cloning the object:" + this.propertiesFileName, e);
123                 } catch (IOException e) {
124                         LOGGER.debug("IOException when cloning the object:" + this.propertiesFileName, e);
125                 } 
126                 
127                 msoCopy.propertiesFileName = this.propertiesFileName;
128                 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
129                 return msoCopy;
130         }
131
132
133
134         @Override
135         public int hashCode() {
136                 final int prime = 31;
137                 int result = 1;
138                 result = prime * result + ((jsonRootNode == null) ? 0 : jsonRootNode.hashCode());
139                 return result;
140         }
141
142         @Override
143         public boolean equals(Object obj) {
144                 if (this == obj)
145                         return true;
146                 if (obj == null)
147                         return false;
148                 if (getClass() != obj.getClass())
149                         return false;
150                 MsoJsonProperties other = (MsoJsonProperties) obj;
151                 if (jsonRootNode == null) {
152                         if (other.jsonRootNode != null)
153                                 return false;
154                 } else if (!jsonRootNode.equals(other.jsonRootNode))
155                         return false;
156                 return true;
157         }
158
159         @Override
160         public String toString() {
161                 StringBuffer response = new StringBuffer();
162                 response.append("Config file " + propertiesFileName + "(Timer:" + automaticRefreshInMinutes + "mins):"
163                                 + System.getProperty("line.separator"));
164                 response.append(this.jsonRootNode.toString());
165                 response.append(System.getProperty("line.separator"));
166                 response.append(System.getProperty("line.separator"));
167                 return response.toString();
168
169         }
170
171
172 }