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 ReplaceEditConfigStrategy extends AbstractEditConfigStrategy {
30 private static final Logger logger = LoggerFactory.getLogger(ReplaceEditConfigStrategy.class);
32 private final Multimap<String, String> providedServices;
34 public ReplaceEditConfigStrategy() {
35 this.providedServices = HashMultimap.create();
38 public ReplaceEditConfigStrategy(Multimap<String, String> providedServices) {
39 this.providedServices = providedServices;
43 void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
44 String module, String instance, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
45 throw new NetconfConfigHandlingException(
46 String.format("Unable to handle missing instance, no missing instances should appear at this point, missing: %s : %s ",
49 NetconfDocumentedException.ErrorType.application,
50 NetconfDocumentedException.ErrorTag.operation_failed,
51 NetconfDocumentedException.ErrorSeverity.error);
54 private void addRefNames(ServiceRegistryWrapper services, Multimap<String, String> providedServices, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
55 for (Entry<String, String> namespaceToService : providedServices.entries()) {
57 if(services.hasRefName(namespaceToService.getKey(),
58 namespaceToService.getValue(), on)){
62 String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
63 ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
64 ta.saveServiceReference(
65 ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
70 void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
72 addRefNames(services, providedServices, ta, on);
73 } catch (InstanceNotFoundException e) {
74 throw new NetconfConfigHandlingException(String.format("Unable to save default ref name for instance %s. Instance not found. ",on),
75 NetconfDocumentedException.ErrorType.application,
76 NetconfDocumentedException.ErrorTag.operation_failed,
77 NetconfDocumentedException.ErrorSeverity.error);
80 for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
82 AttributeConfigElement ace = configAttributeEntry.getValue();
84 if (!ace.getResolvedValue().isPresent()) {
85 Object value = ace.getResolvedDefaultValue();
86 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
87 logger.debug("Attribute {} set to default value {} for {}", configAttributeEntry.getKey(), value,
90 Object value = ace.getResolvedValue().get();
91 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
92 logger.debug("Attribute {} set to value {} for {}", configAttributeEntry.getKey(), value, on);
95 } catch (Exception e) {
96 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
97 + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);