2 * Copyright (c) 2013 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.netconf.confignetconfconnector.operations.editconfig;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.Multimap;
13 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
14 import org.opendaylight.controller.config.util.ConfigTransactionClient;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.confignetconfconnector.exception.NetconfConfigHandlingException;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import javax.management.Attribute;
23 import javax.management.InstanceNotFoundException;
24 import javax.management.ObjectName;
26 import java.util.Map.Entry;
28 public class MergeEditConfigStrategy extends AbstractEditConfigStrategy {
30 private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
31 private final Multimap<String, String> providedServices;
33 public MergeEditConfigStrategy() {
34 this.providedServices = HashMultimap.create();
37 public MergeEditConfigStrategy(Multimap<String, String> providedServices) {
38 this.providedServices = providedServices;
42 void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
43 String module, String instance, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
44 throw new NetconfConfigHandlingException(
45 String.format("Unable to handle missing instance, no missing instances should appear at this point, missing: %s : %s ",
48 NetconfDocumentedException.ErrorType.application,
49 NetconfDocumentedException.ErrorTag.operation_failed,
50 NetconfDocumentedException.ErrorSeverity.error);
53 private void addRefNames(ServiceRegistryWrapper services, Multimap<String, String> providedServices, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
54 for (Entry<String, String> namespaceToService : providedServices.entries()) {
56 if(services.hasRefName(namespaceToService.getKey(),
57 namespaceToService.getValue(), on)){
61 String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
62 ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
63 ta.saveServiceReference(
64 ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
69 void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
71 addRefNames(services, providedServices, ta, on);
72 } catch (InstanceNotFoundException e) {
73 throw new NetconfConfigHandlingException(String.format("Unable to save default ref name for instance %s. Instance was not found.",e),
74 NetconfDocumentedException.ErrorType.application,
75 NetconfDocumentedException.ErrorTag.operation_failed,
76 NetconfDocumentedException.ErrorSeverity.error);
79 for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
81 AttributeConfigElement ace = configAttributeEntry.getValue();
83 if (!ace.getResolvedValue().isPresent()) {
84 logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
88 Object value = ace.getResolvedValue().get();
89 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
90 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
91 } catch (Exception e) {
92 throw new NetconfConfigHandlingException(String.format("Unable to set attributes for %s, Error with attribute %s : %s ",
94 configAttributeEntry.getKey(),
95 configAttributeEntry.getValue()),
96 NetconfDocumentedException.ErrorType.application,
97 NetconfDocumentedException.ErrorTag.operation_failed,
98 NetconfDocumentedException.ErrorSeverity.error);