Fix sonar warnings in config-util.
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / strategy / ReplaceEditConfigStrategy.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.strategy;
10
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import javax.management.Attribute;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.facade.xml.exception.ConfigHandlingException;
16 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
17 import org.opendaylight.controller.config.facade.xml.mapping.config.ServiceRegistryWrapper;
18 import org.opendaylight.controller.config.util.ConfigTransactionClient;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ReplaceEditConfigStrategy extends AbstractEditConfigStrategy {
24
25     private static final Logger LOG = LoggerFactory.getLogger(ReplaceEditConfigStrategy.class);
26
27     @Override
28     void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
29                                String module, String instance, ServiceRegistryWrapper services) throws
30         ConfigHandlingException {
31         throw new ConfigHandlingException(
32                 String.format("Unable to handle missing instance, no missing instances should appear at this point, missing: %s : %s ",
33                         module,
34                         instance),
35                 DocumentedException.ErrorType.APPLICATION,
36                 DocumentedException.ErrorTag.OPERATION_FAILED,
37                 DocumentedException.ErrorSeverity.ERROR);
38     }
39
40     @Override
41     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws
42         ConfigHandlingException {
43         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
44             try {
45                 AttributeConfigElement ace = configAttributeEntry.getValue();
46
47                 if (!ace.getResolvedValue().isPresent()) {
48                     Object value = ace.getResolvedDefaultValue();
49                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
50                     LOG.debug("Attribute {} set to default value {} for {}", configAttributeEntry.getKey(), value,
51                             on);
52                 } else {
53                     Object value = ace.getResolvedValue().get();
54                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
55                     LOG.debug("Attribute {} set to value {} for {}", configAttributeEntry.getKey(), value, on);
56                 }
57
58             } catch (Exception e) {
59                 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
60                         + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);
61             }
62         }
63     }
64 }