2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.properties;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
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;
35 public class MsoJsonProperties extends AbstractMsoProperties {
37 protected ObjectMapper mapper = new ObjectMapper();
39 protected JsonNode jsonRootNode = mapper.createObjectNode();
41 protected MsoJsonProperties() {
45 public synchronized JsonNode getJsonRootNode () {
46 return this.jsonRootNode;
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
56 public synchronized String getEncryptedProperty(JsonNode jsonNode, String defaultValue, String encryptionKey) {
58 if (jsonNode.isTextual()) {
60 return CryptoUtils.decrypt(jsonNode.asText(), encryptionKey);
61 } catch (GeneralSecurityException e) {
62 LOGGER.debug("Exception while decrypting property: " + jsonNode.asText(), e);
70 * This method load a properties file from a source path.
72 * @param propertiesPath The path to the file
73 * @throws IOException In case of issues during the opening
76 protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
78 FileReader reader = null;
80 this.propertiesFileName = propertiesPath;
84 this.jsonRootNode = mapper.createObjectNode();
86 reader = new FileReader(propertiesPath);
89 this.jsonRootNode = mapper.readValue(reader, JsonNode.class);
93 JsonNode reloadJsonProp = this.jsonRootNode.get(RELOAD_TIME_PROPERTY);
94 if (reloadJsonProp != null) {
95 this.automaticRefreshInMinutes = reloadJsonProp.asInt(DEFAULT_RELOAD_TIME_MIN);
97 this.automaticRefreshInMinutes = DEFAULT_RELOAD_TIME_MIN;
100 // Always close the file
102 if (reader != null) {
105 } catch (IOException e) {
106 LOGGER.debug("Exception while closing reader for file:" + propertiesFileName, e);
112 public synchronized MsoJsonProperties clone() {
113 MsoJsonProperties msoCopy = new MsoJsonProperties();
115 ObjectMapper mapper = new ObjectMapper();
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);
127 msoCopy.propertiesFileName = this.propertiesFileName;
128 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
135 public int hashCode() {
136 final int prime = 31;
138 result = prime * result + ((jsonRootNode == null) ? 0 : jsonRootNode.hashCode());
143 public boolean equals(Object obj) {
148 if (getClass() != obj.getClass())
150 MsoJsonProperties other = (MsoJsonProperties) obj;
151 if (jsonRootNode == null) {
152 if (other.jsonRootNode != null)
154 } else if (!jsonRootNode.equals(other.jsonRootNode))
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();