Skip to content

Commit 0208519

Browse files
rakillenddsharpe
authored andcommitted
JIRA WDT-371 - Use variable map for model merge to find matching names (#455)
1 parent 78de94d commit 0208519

File tree

8 files changed

+264
-36
lines changed

8 files changed

+264
-36
lines changed

core/src/main/python/create.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,26 @@ def main(args):
385385
model_context = ModelContext(_program_name, dict())
386386
tool_exit.end(model_context, exit_code)
387387

388+
variable_map = {}
389+
try:
390+
if model_context.get_variable_file():
391+
variable_map = variables.load_variables(model_context.get_variable_file())
392+
except VariableException, ex:
393+
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
394+
class_name=_class_name, method_name=_method_name)
395+
cla_helper.clean_up_temp_files()
396+
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
397+
388398
model_file_value = model_context.get_model_file()
389399
try:
390-
model = cla_helper.merge_model_files(model_file_value)
400+
model = cla_helper.merge_model_files(model_file_value, variable_map)
391401
except TranslateException, te:
392402
__logger.severe('WLSDPLY-20009', _program_name, model_file_value, te.getLocalizedMessage(), error=te,
393403
class_name=_class_name, method_name=_method_name)
394404
cla_helper.clean_up_temp_files()
395405
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
396406

397407
try:
398-
variable_map = {}
399-
if model_context.get_variable_file():
400-
variable_map = variables.load_variables(model_context.get_variable_file())
401408
variables.substitute(model, variable_map, model_context)
402409
except VariableException, ex:
403410
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,

core/src/main/python/deploy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,26 @@ def main(args):
409409
model_context = ModelContext(_program_name, dict())
410410
tool_exit.end(model_context, exit_code)
411411

412+
variable_map = {}
413+
try:
414+
if model_context.get_variable_file():
415+
variable_map = variables.load_variables(model_context.get_variable_file())
416+
except VariableException, ex:
417+
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
418+
class_name=_class_name, method_name=_method_name)
419+
cla_helper.clean_up_temp_files()
420+
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
421+
412422
model_file_value = model_context.get_model_file()
413423
try:
414-
model_dictionary = cla_helper.merge_model_files(model_file_value)
424+
model_dictionary = cla_helper.merge_model_files(model_file_value, variable_map)
415425
except TranslateException, te:
416426
__logger.severe('WLSDPLY-09014', _program_name, model_file_value, te.getLocalizedMessage(), error=te,
417427
class_name=_class_name, method_name=_method_name)
418428
cla_helper.clean_up_temp_files()
419429
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
420430

421431
try:
422-
variable_map = {}
423-
if model_context.get_variable_file():
424-
variable_map = variables.load_variables(model_context.get_variable_file())
425432
variables.substitute(model_dictionary, variable_map, model_context)
426433
except VariableException, ex:
427434
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,

core/src/main/python/update.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,26 @@ def main(args):
427427
model_context = ModelContext(_program_name, dict())
428428
tool_exit.end(model_context, exit_code)
429429

430+
variable_map = {}
431+
try:
432+
if model_context.get_variable_file():
433+
variable_map = variables.load_variables(model_context.get_variable_file())
434+
except VariableException, ex:
435+
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
436+
class_name=_class_name, method_name=_method_name)
437+
cla_helper.clean_up_temp_files()
438+
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
439+
430440
model_file_value = model_context.get_model_file()
431441
try:
432-
model_dictionary = cla_helper.merge_model_files(model_file_value)
442+
model_dictionary = cla_helper.merge_model_files(model_file_value, variable_map)
433443
except TranslateException, te:
434444
__logger.severe('WLSDPLY-09014', _program_name, model_file_value, te.getLocalizedMessage(), error=te,
435445
class_name=_class_name, method_name=_method_name)
436446
cla_helper.clean_up_temp_files()
437447
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
438448

439449
try:
440-
variable_map = {}
441-
if model_context.get_variable_file():
442-
variable_map = variables.load_variables(model_context.get_variable_file())
443450
variables.substitute(model_dictionary, variable_map, model_context)
444451
except VariableException, ex:
445452
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,

core/src/main/python/validate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ def __perform_model_file_validation(model_file_name, model_context):
175175
class_name=_class_name, method_name=_method_name)
176176

177177
try:
178-
model_dictionary = cla_helper.merge_model_files(model_file_name)
179178
model_validator = Validator(model_context, logger=__logger)
180-
model_validator.validate_in_standalone_mode(model_dictionary, model_context.get_variable_file(),
179+
variable_map = model_validator.load_variables(model_context.get_variable_file())
180+
model_dictionary = cla_helper.merge_model_files(model_file_name, variable_map)
181+
model_validator.validate_in_standalone_mode(model_dictionary, variable_map,
181182
model_context.get_archive_file_name())
182183
except TranslateException, te:
183184
__logger.severe('WLSDPLY-20009', _program_name, model_file_name, te.getLocalizedMessage(),

core/src/main/python/wlsdeploy/tool/validate/validator.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(self, model_context, aliases=None, logger=None, wlst_mode=None, dom
9696
self._model_file_name = self._model_context.get_model_file()
9797
return
9898

99-
def validate_in_standalone_mode(self, model_dict, variables_file_name=None, archive_file_name=None):
99+
def validate_in_standalone_mode(self, model_dict, variable_map, archive_file_name=None):
100100
"""
101101
Performs model file validate and returns a ValidationResults object.
102102
@@ -105,8 +105,7 @@ def validate_in_standalone_mode(self, model_dict, variables_file_name=None, arch
105105
ones, followed by error-related ones.
106106
107107
:param model_dict: A Python dictionary of the model to be validated
108-
:param variables_file_name: Path to file containing variable substitution data used with model file.
109-
Defaults to None.
108+
:param variable_map: Map used for variable substitution
110109
:param archive_file_name: Path to file containing binaries associated with the model file.
111110
Defaults to None.
112111
:raises ValidationException: if an AliasException is raised during an invocation of an aliases API call.
@@ -119,9 +118,9 @@ def validate_in_standalone_mode(self, model_dict, variables_file_name=None, arch
119118
# actually require changes to be made to the cloned model dictionary
120119
cloned_model_dict = copy.deepcopy(model_dict)
121120

122-
self._logger.entering(variables_file_name, archive_file_name, class_name=_class_name, method_name=_method_name)
121+
self._logger.entering(archive_file_name, class_name=_class_name, method_name=_method_name)
123122
self._validation_mode = _ValidationModes.STANDALONE
124-
self.__validate_model_file(cloned_model_dict, variables_file_name, archive_file_name)
123+
self.__validate_model_file(cloned_model_dict, variable_map, archive_file_name)
125124

126125
self._logger.exiting(class_name=_class_name, method_name=_method_name)
127126

@@ -156,7 +155,8 @@ def validate_in_tool_mode(self, model_dict, variables_file_name=None, archive_fi
156155
self._logger.entering(variables_file_name, archive_file_name, class_name=_class_name, method_name=_method_name)
157156
return_code = Validator.ReturnCode.STOP
158157
self._validation_mode = _ValidationModes.TOOL
159-
self.__validate_model_file(cloned_model_dict, variables_file_name, archive_file_name)
158+
variable_map = self.load_variables(variables_file_name)
159+
self.__validate_model_file(cloned_model_dict, variable_map, archive_file_name)
160160

161161
status = Validator.ValidationStatus.VALID
162162

@@ -223,23 +223,20 @@ def __get_attribute_log_value(self, attribute_name, attribute_value, attribute_i
223223
result = '<masked>'
224224
return result
225225

226-
def __validate_model_file(self, model_dict, variables_file_name, archive_file_name):
226+
def __validate_model_file(self, model_dict, variables_map, archive_file_name):
227227
_method_name = '__validate_model_file'
228228

229229
self.__pre_validation_setup(model_dict, archive_file_name)
230230

231-
self._logger.entering(variables_file_name, archive_file_name, class_name=_class_name, method_name=_method_name)
231+
self._logger.entering(archive_file_name, class_name=_class_name, method_name=_method_name)
232232
self._logger.info('WLSDPLY-05002', _ValidationModes.from_value(self._validation_mode), self._wls_version,
233233
WlstModes.from_value(self._wlst_mode), class_name=_class_name, method_name=_method_name)
234234

235235
if self._model_file_name is not None:
236236
self._logger.info('WLSDPLY-05003', self._model_file_name, class_name=_class_name, method_name=_method_name)
237237

238238
try:
239-
if variables_file_name is not None:
240-
self._logger.info('WLSDPLY-05004', variables_file_name, class_name=_class_name,
241-
method_name=_method_name)
242-
self._variable_properties = variables.load_variables(variables_file_name)
239+
self._variable_properties = variables_map
243240
variables.substitute(model_dict, self._variable_properties, self._model_context)
244241
except VariableException, ve:
245242
ex = exception_helper.create_validate_exception('WLSDPLY-20004', 'validateModel',
@@ -270,6 +267,26 @@ def __validate_model_file(self, model_dict, variables_file_name, archive_file_na
270267
self._logger.exiting(class_name=_class_name, method_name=_method_name)
271268
return
272269

270+
def load_variables(self, variables_file_name):
271+
"""
272+
Load the variables properties from the specified file.
273+
:param variables_file_name: the name of the variables file, or None if not specified
274+
:return: the variables properties
275+
"""
276+
_method_name = 'load_variables'
277+
278+
try:
279+
if variables_file_name is not None:
280+
self._logger.info('WLSDPLY-05004', variables_file_name, class_name=_class_name,
281+
method_name=_method_name)
282+
return variables.load_variables(variables_file_name)
283+
return {}
284+
except VariableException, ve:
285+
ex = exception_helper.create_validate_exception('WLSDPLY-20004', 'validateModel',
286+
ve.getLocalizedMessage(), error=ve)
287+
self._logger.throwing(ex, class_name=_class_name, method_name=_method_name)
288+
raise ex
289+
273290
def __pre_validation_setup(self, model_dict, archive_file_name):
274291
"""
275292
Performs pre-validation setup activities. These include things like:

core/src/main/python/wlsdeploy/util/cla_helper.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from wlsdeploy.exception import exception_helper
1111
from wlsdeploy.logging.platform_logger import PlatformLogger
1212
from wlsdeploy.util import cla_utils
13+
from wlsdeploy.util import variables
1314
from wlsdeploy.util.cla_utils import CommandLineArgUtil
1415
from wlsdeploy.util.model_translator import FileToPython
1516

@@ -115,10 +116,11 @@ def clean_up_temp_files():
115116
__tmp_model_dir = None
116117

117118

118-
def merge_model_files(model_file_value):
119+
def merge_model_files(model_file_value, variable_map=None):
119120
"""
120121
Merge the model files specified by the model file value.
121122
It may be a single file, or a comma-separated list of files.
123+
:param variable_map: variables to be used for name resolution, or None
122124
:param model_file_value: the value specified as a command argument
123125
:return: the merge model dictionary
124126
"""
@@ -127,24 +129,50 @@ def merge_model_files(model_file_value):
127129

128130
for model_file in model_files:
129131
model = FileToPython(model_file, True).parse()
130-
_merge_dictionaries(merged_model, model)
132+
_merge_dictionaries(merged_model, model, variable_map)
131133

132134
return merged_model
133135

134136

135-
def _merge_dictionaries(dictionary, new_dictionary):
137+
def _merge_dictionaries(dictionary, new_dictionary, variable_map):
136138
"""
137139
Merge the values from the new dictionary to the existing one.
140+
Use variables to resolve keys.
138141
:param dictionary: the existing dictionary
139142
:param new_dictionary: the new dictionary to be merged
143+
:param variable_map: variables to be used for name resolution, or None
140144
"""
141-
for key in new_dictionary:
142-
new_value = new_dictionary[key]
143-
if key not in dictionary:
144-
dictionary[key] = new_value
145+
for new_key in new_dictionary:
146+
new_value = new_dictionary[new_key]
147+
dictionary_key = _find_dictionary_key(dictionary, new_key, variable_map)
148+
if dictionary_key is None:
149+
dictionary[new_key] = new_value
145150
else:
146-
value = dictionary[key]
151+
value = dictionary[dictionary_key]
147152
if isinstance(value, dict) and isinstance(new_value, dict):
148-
_merge_dictionaries(value, new_value)
153+
_merge_dictionaries(value, new_value, variable_map)
149154
else:
150-
dictionary[key] = new_value
155+
dictionary[new_key] = new_value
156+
157+
158+
def _find_dictionary_key(dictionary, new_key, variable_map):
159+
"""
160+
Find the specified key in the specified dictionary.
161+
If a direct match is not found, and a variable map is specified, perform a more thorough check,
162+
taking into account variable substitution.
163+
:param dictionary: the dictionary to be searched
164+
:param new_key: the key being checked
165+
:param variable_map: variables to be used for name resolution, or None
166+
:return: the corresponding key from the dictionary
167+
"""
168+
if new_key in dictionary:
169+
return new_key
170+
171+
if variable_map is not None:
172+
actual_new_key = variables.substitute_key(new_key, variable_map)
173+
for key in dictionary.keys():
174+
actual_key = variables.substitute_key(key, variable_map)
175+
if actual_key == actual_new_key:
176+
return key
177+
178+
return None

core/src/main/python/wlsdeploy/util/variables.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,22 @@ def _read_value_from_file(file_path, model_context):
266266
raise ex
267267

268268
return str(line).strip()
269+
270+
271+
def substitute_key(text, variables):
272+
"""
273+
Substitute any @@PROP values in the text and return.
274+
If the corresponding variable is not found, leave the @@PROP value in place.
275+
The deprecated ${} notation is not resolved.
276+
:param text: the text to be evaluated
277+
:param variables: the variable map
278+
:return: the substituted text value
279+
"""
280+
tokens = _property_pattern.findall(text)
281+
if tokens:
282+
for token in tokens:
283+
key = token[7:-2]
284+
if key in variables:
285+
value = variables[key]
286+
text = text.replace(token, value)
287+
return text

0 commit comments

Comments
 (0)