2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.facade.xml.strategy;
11 import static java.util.Arrays.asList;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import javax.management.Attribute;
18 import javax.management.ObjectName;
19 import org.opendaylight.controller.config.facade.xml.exception.ConfigHandlingException;
20 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
21 import org.opendaylight.controller.config.facade.xml.mapping.config.ServiceRegistryWrapper;
22 import org.opendaylight.controller.config.util.ConfigTransactionClient;
23 import org.opendaylight.controller.config.util.xml.DocumentedException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class MergeEditConfigStrategy extends AbstractEditConfigStrategy {
29 private static final Logger LOG = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
31 public MergeEditConfigStrategy() {
35 void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
36 String module, String instance, ServiceRegistryWrapper services) throws ConfigHandlingException {
37 throw new ConfigHandlingException(String.format(
38 "Unable to handle missing instance, no missing instances should "
39 + "appear at this point, missing: %s : %s ",
40 module, instance), DocumentedException.ErrorType.APPLICATION,
41 DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorSeverity.ERROR);
45 @SuppressWarnings("IllegalCatch")
46 void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on,
47 ServiceRegistryWrapper services) throws ConfigHandlingException {
49 for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
51 AttributeConfigElement ace = configAttributeEntry.getValue();
53 if (!ace.getResolvedValue().isPresent()) {
54 LOG.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
58 Object toBeMergedIn = ace.getResolvedValue().get();
59 // Get the existing values so we can merge the new values with them.
60 Attribute currentAttribute = ta.getAttribute(on, ace.getJmxName());
61 Object oldValue = currentAttribute != null ? currentAttribute.getValue() : null;
62 // Merge value with currentValue
63 toBeMergedIn = merge(oldValue, toBeMergedIn);
64 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), toBeMergedIn));
65 LOG.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), toBeMergedIn, on);
66 } catch (RuntimeException e) {
67 LOG.error("Error while merging object names of {}", on, e);
68 throw new ConfigHandlingException(String.format("Unable to set attributes for %s, "
69 + "Error with attribute %s : %s ",
71 configAttributeEntry.getKey(),
72 configAttributeEntry.getValue()), e,
73 DocumentedException.ErrorType.APPLICATION,
74 DocumentedException.ErrorTag.OPERATION_FAILED,
75 DocumentedException.ErrorSeverity.ERROR);
81 * Merge value into current value Currently, this is only implemented for arrays
82 * of ObjectNames, but that is the most common case for which it is needed.
84 protected Object merge(Object oldValue, Object toBeMergedIn) {
85 if (oldValue instanceof ObjectName[] && toBeMergedIn instanceof ObjectName[]) {
86 toBeMergedIn = mergeObjectNameArrays((ObjectName[]) oldValue, (ObjectName[]) toBeMergedIn);
92 * Merge value into current values This implements for arrays of ObjectNames,
93 * but that is the most common case for which it is needed.
96 * - the new values to be merged into existing values
98 * - the existing values
100 * @return an ObjectName[] consisting the elements of currentValue with an
101 * elements from values not already present in currentValue added
104 protected ObjectName[] mergeObjectNameArrays(ObjectName[] oldValue, ObjectName[] toBeMergedIn) {
105 List<ObjectName> newValueList = new ArrayList<>();
106 newValueList.addAll(asList(oldValue));
108 * It is guaranteed that old values do not contain transaction name. Since
109 * toBeMergedIn is filled using service references translated by
110 * ServiceRegistryWrapper, it is also guaranteed that this list will not contain
111 * transaction names. Run through the list of values to be merged. If we don't
112 * have them already, add them to the list.
114 for (ObjectName objName : toBeMergedIn) {
115 if (!newValueList.contains(objName)) {
116 newValueList.add(objName);
119 return newValueList.toArray(new ObjectName[newValueList.size()]);