Change the header to SO
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoJsonProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.openecomp.mso.utils.CryptoUtils;
29
30 import com.fasterxml.jackson.core.JsonParseException;
31 import com.fasterxml.jackson.databind.JsonMappingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35
36 public class MsoJsonProperties extends AbstractMsoProperties {
37         
38         protected ObjectMapper mapper = new ObjectMapper();
39         
40         protected JsonNode jsonRootNode = mapper.createObjectNode();
41         
42         protected MsoJsonProperties() {
43                 
44         }
45
46         public synchronized JsonNode getJsonRootNode () {
47                 return this.jsonRootNode;
48         }
49
50         /**
51          * This method is used to get the text encrypted in the string value of the node.
52          * @param jsonNode The JsonNode containing the strig to decode
53          * @param defaultValue The default value in case of issue
54          * @param encryptionKey The encryption Key in AES 128 bits
55          * @return the String decrypted
56          */
57         public synchronized String getEncryptedProperty(JsonNode jsonNode, String defaultValue, String encryptionKey) {
58
59                 if (jsonNode.isTextual()) {
60                         try {
61                                 return CryptoUtils.decrypt(jsonNode.asText(), encryptionKey);
62                         } catch (GeneralSecurityException e) {
63                                 LOGGER.debug("Exception while decrypting property: " + jsonNode.asText(), e);
64                         }
65                 } 
66                 
67                 return defaultValue;
68         }
69         
70         /**
71          * This method load a properties file from a source path.
72          *
73          * @param propertiesPath The path to the file
74          * @throws IOException In case of issues during the opening
75          */
76         @Override
77         protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
78
79                 FileReader reader = null;
80         
81                 this.propertiesFileName = propertiesPath;
82                 
83                 try {
84                         // Clean
85                         this.jsonRootNode = mapper.createObjectNode();
86                         
87                         reader = new FileReader(propertiesPath);
88                         
89                         // Try a tree load
90                         this.jsonRootNode = mapper.readValue(reader, JsonNode.class);
91
92
93                 } finally {
94                         JsonNode reloadJsonProp = this.jsonRootNode.get(RELOAD_TIME_PROPERTY);
95                         if (reloadJsonProp != null) {
96                                 this.automaticRefreshInMinutes = reloadJsonProp.asInt(DEFAULT_RELOAD_TIME_MIN); 
97                         } else {
98                                 this.automaticRefreshInMinutes = DEFAULT_RELOAD_TIME_MIN;
99                         }
100                         
101                         // Always close the file
102                         try {
103                                 if (reader != null) {
104                                         reader.close();
105                                 }
106                         } catch (IOException e) {
107                                 LOGGER.debug("Exception while closing reader for file:" + propertiesFileName, e);
108                         }
109                 }
110         }
111
112         @Override
113         public synchronized MsoJsonProperties clone() {
114                 MsoJsonProperties msoCopy = new MsoJsonProperties();
115                 
116                 ObjectMapper newMapper = new ObjectMapper();
117                 try {
118                         msoCopy.jsonRootNode = newMapper.createObjectNode();
119                         msoCopy.jsonRootNode = newMapper.readValue(this.jsonRootNode.toString(), JsonNode.class);
120                 } catch (JsonParseException e) {
121                         LOGGER.debug("JsonParseException when cloning the object:" + this.propertiesFileName, e);
122                 } catch (JsonMappingException e) {
123                         LOGGER.debug("JsonMappingException when cloning the object:" + this.propertiesFileName, e);
124                 } catch (IOException e) {
125                         LOGGER.debug("IOException when cloning the object:" + this.propertiesFileName, e);
126                 } 
127                 
128                 msoCopy.propertiesFileName = this.propertiesFileName;
129                 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
130                 return msoCopy;
131         }
132
133
134
135         @Override
136         public int hashCode() {
137                 final int prime = 31;
138                 int result = 1;
139                 result = prime * result + ((jsonRootNode == null) ? 0 : jsonRootNode.hashCode());
140                 return result;
141         }
142
143         @Override
144         public boolean equals(Object obj) {
145                 if (this == obj)
146                         return true;
147                 if (obj == null)
148                         return false;
149                 if (getClass() != obj.getClass())
150                         return false;
151                 MsoJsonProperties other = (MsoJsonProperties) obj;
152                 if (jsonRootNode == null) {
153                         if (other.jsonRootNode != null)
154                                 return false;
155                 } else if (!jsonRootNode.equals(other.jsonRootNode))
156                         return false;
157                 return true;
158         }
159
160         @Override
161         public String toString() {
162                 StringBuffer response = new StringBuffer();
163                 response.append("Config file " + propertiesFileName + "(Timer:" + automaticRefreshInMinutes + "mins):"
164                                 + System.getProperty("line.separator"));
165                 response.append(this.jsonRootNode.toString());
166                 response.append(System.getProperty("line.separator"));
167                 response.append(System.getProperty("line.separator"));
168                 return response.toString();
169
170         }
171
172
173 }