X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fconfignetconfconnector%2Foperations%2Feditconfig%2FMergeEditConfigStrategy.java;h=d3d5bd90a778231eaa03b11cdf1e6dd5606b98f0;hp=06befb0565067592b58465782d66caa74a568100;hb=250542ef463f05ba5c871c128afeb33c7af93ea8;hpb=237c7309b595b59641dfb4eb546412fd12f52d16 diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/MergeEditConfigStrategy.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/MergeEditConfigStrategy.java index 06befb0565..d3d5bd90a7 100644 --- a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/MergeEditConfigStrategy.java +++ b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/MergeEditConfigStrategy.java @@ -8,83 +8,112 @@ package org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; +import static java.util.Arrays.asList; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import javax.management.Attribute; +import javax.management.ObjectName; import org.opendaylight.controller.config.util.ConfigTransactionClient; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.confignetconfconnector.exception.NetconfConfigHandlingException; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement; -import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services; +import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.management.Attribute; -import javax.management.InstanceAlreadyExistsException; -import javax.management.InstanceNotFoundException; -import javax.management.ObjectName; -import java.util.Map; -import java.util.Map.Entry; - public class MergeEditConfigStrategy extends AbstractEditConfigStrategy { - private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class); - private final Multimap providedServices; + private static final Logger LOG = LoggerFactory.getLogger(MergeEditConfigStrategy.class); public MergeEditConfigStrategy() { - this.providedServices = HashMultimap.create(); - } - public MergeEditConfigStrategy(Multimap providedServices) { - this.providedServices = providedServices; } @Override void handleMissingInstance(Map configuration, ConfigTransactionClient ta, - String module, String instance, Services services) { - ObjectName on = null; - try { - on = ta.createModule(module, instance); - logger.info("New instance for {} {} created under name {}", module, instance, on); - addRefNames(services, providedServices, module, instance, ta, on); - executeStrategy(configuration, ta, on, services); - } catch (InstanceAlreadyExistsException e1) { - throw new IllegalStateException("Unable to create instance for " + module + " : " + instance); - } catch (InstanceNotFoundException e) { - throw new IllegalStateException("Unable to save default ref name for instance " + on, e); - } - } - - private void addRefNames(Services services, Multimap providedServices, String module, - String instance, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException { - for (Entry namespaceToService : providedServices.entries()) { - - if(services.hasRefName(namespaceToService.getKey(), - namespaceToService.getValue(), on)) - continue; - - String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(), - module, instance); - ta.saveServiceReference( - ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on); - } + String module, String instance, ServiceRegistryWrapper services) throws NetconfConfigHandlingException { + throw new NetconfConfigHandlingException( + String.format("Unable to handle missing instance, no missing instances should appear at this point, missing: %s : %s ", + module, + instance), + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.operation_failed, + NetconfDocumentedException.ErrorSeverity.error); } @Override - void executeStrategy(Map configuration, ConfigTransactionClient ta, ObjectName on, Services services) { + void executeStrategy(Map configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException { + for (Entry configAttributeEntry : configuration.entrySet()) { try { AttributeConfigElement ace = configAttributeEntry.getValue(); if (!ace.getResolvedValue().isPresent()) { - logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on); + LOG.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on); continue; } - Object value = ace.getResolvedValue().get(); - ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value)); - logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on); + Object toBeMergedIn = ace.getResolvedValue().get(); + // Get the existing values so we can merge the new values with them. + Attribute currentAttribute = ta.getAttribute(on, ace.getJmxName()); + Object oldValue = (currentAttribute != null ? currentAttribute.getValue() : null); + // Merge value with currentValue + toBeMergedIn = merge(oldValue, toBeMergedIn); + ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), toBeMergedIn)); + LOG.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), toBeMergedIn, on); } catch (Exception e) { - throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute " - + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e); + LOG.error("Error while merging objectnames of {}", on, e); + throw new NetconfConfigHandlingException(String.format("Unable to set attributes for %s, Error with attribute %s : %s ", + on, + configAttributeEntry.getKey(), + configAttributeEntry.getValue()), + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.operation_failed, + NetconfDocumentedException.ErrorSeverity.error); + } + } + } + + /** + * Merge value into current value + * Currently, this is only implemented for arrays of ObjectNames, but that is the + * most common case for which it is needed. + */ + protected Object merge(Object oldValue, Object toBeMergedIn) { + if (oldValue instanceof ObjectName[] && toBeMergedIn instanceof ObjectName[]) { + toBeMergedIn = mergeObjectNameArrays((ObjectName[]) oldValue, (ObjectName[]) toBeMergedIn); + } + return toBeMergedIn; + } + + /** + * Merge value into current values + * This implements for arrays of ObjectNames, but that is the + * most common case for which it is needed. + * + * @param oldValue - the new values to be merged into existing values + * @param toBeMergedIn - the existing values + * + * @return an ObjectName[] consisting the elements of currentValue with an elements from values not already present in currentValue added + * + */ + protected ObjectName[] mergeObjectNameArrays(ObjectName[] oldValue, ObjectName[] toBeMergedIn) { + List newValueList = new ArrayList<>(); + newValueList.addAll(asList(oldValue)); + /* + It is guaranteed that old values do not contain transaction name. + Since toBeMergedIn is filled using service references translated by ServiceRegistryWrapper, it + is also guaranteed that this list will not contain transaction names. + Run through the list of values to be merged. If we don't have them already, add them to the list. + */ + for (ObjectName objName : toBeMergedIn) { + if (!newValueList.contains(objName)) { + newValueList.add(objName); } } + return newValueList.toArray(new ObjectName[newValueList.size()]); } }