Merge "Add service reference binding to config registry."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / ReplaceEditConfigStrategy.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig;
10
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import javax.management.Attribute;
15 import javax.management.InstanceAlreadyExistsException;
16 import javax.management.ObjectName;
17
18 import org.opendaylight.controller.config.util.ConfigTransactionClient;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ReplaceEditConfigStrategy extends AbstractEditConfigStrategy {
24
25     private static final Logger logger = LoggerFactory.getLogger(ReplaceEditConfigStrategy.class);
26
27     @Override
28     void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
29             String module, String instance) {
30         try {
31             ObjectName on = ta.createModule(module, instance);
32             logger.debug("New instance for {} {} created under name {}", module, instance, on);
33             executeStrategy(configuration, ta, on);
34         } catch (InstanceAlreadyExistsException e) {
35             logger.warn("Error creating instance {}:{}, replace failed", module, instance, e);
36             throw new IllegalStateException("Unable to create new instance for " + module + " : " + instance, e);
37         }
38     }
39
40     @Override
41     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on) {
42         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
43             try {
44                 AttributeConfigElement ace = configAttributeEntry.getValue();
45
46                 if (!ace.getResolvedValue().isPresent()) {
47                     Object value = ace.getResolvedDefaultValue();
48                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
49                     logger.debug("Attribute {} set to default value {} for {}", configAttributeEntry.getKey(), value,
50                             on);
51                 } else {
52                     Object value = ace.getResolvedValue().get();
53                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
54                     logger.debug("Attribute {} set to value {} for {}", configAttributeEntry.getKey(), value, on);
55                 }
56
57             } catch (Exception e) {
58                 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
59                         + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);
60             }
61         }
62     }
63 }