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.openecomp.mso.utils.CryptoUtils;
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;
36 public class MsoJsonProperties extends AbstractMsoProperties {
38 protected ObjectMapper mapper = new ObjectMapper();
40 protected JsonNode jsonRootNode = mapper.createObjectNode();
42 protected MsoJsonProperties() {
46 public synchronized JsonNode getJsonRootNode () {
47 return this.jsonRootNode;
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
57 public synchronized String getEncryptedProperty(JsonNode jsonNode, String defaultValue, String encryptionKey) {
59 if (jsonNode.isTextual()) {
61 return CryptoUtils.decrypt(jsonNode.asText(), encryptionKey);
62 } catch (GeneralSecurityException e) {
63 LOGGER.debug("Exception while decrypting property: " + jsonNode.asText(), e);
71 * This method load a properties file from a source path.
73 * @param propertiesPath The path to the file
74 * @throws IOException In case of issues during the opening
77 protected synchronized void loadPropertiesFile(String propertiesPath) throws IOException {
79 FileReader reader = null;
81 this.propertiesFileName = propertiesPath;
85 this.jsonRootNode = mapper.createObjectNode();
87 reader = new FileReader(propertiesPath);
90 this.jsonRootNode = mapper.readValue(reader, JsonNode.class);
94 JsonNode reloadJsonProp = this.jsonRootNode.get(RELOAD_TIME_PROPERTY);
95 if (reloadJsonProp != null) {
96 this.automaticRefreshInMinutes = reloadJsonProp.asInt(DEFAULT_RELOAD_TIME_MIN);
98 this.automaticRefreshInMinutes = DEFAULT_RELOAD_TIME_MIN;
101 // Always close the file
103 if (reader != null) {
106 } catch (IOException e) {
107 LOGGER.debug("Exception while closing reader for file:" + propertiesFileName, e);
113 public synchronized MsoJsonProperties clone() {
114 MsoJsonProperties msoCopy = new MsoJsonProperties();
116 ObjectMapper newMapper = new ObjectMapper();
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);
128 msoCopy.propertiesFileName = this.propertiesFileName;
129 msoCopy.automaticRefreshInMinutes = this.automaticRefreshInMinutes;
136 public int hashCode() {
137 final int prime = 31;
139 result = prime * result + ((jsonRootNode == null) ? 0 : jsonRootNode.hashCode());
144 public boolean equals(Object obj) {
149 if (getClass() != obj.getClass())
151 MsoJsonProperties other = (MsoJsonProperties) obj;
152 if (jsonRootNode == null) {
153 if (other.jsonRootNode != null)
155 } else if (!jsonRootNode.equals(other.jsonRootNode))
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();