6ebeeaa07ba86034991ffd584448135bd2ec5b13
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / MergeEditConfigStrategy.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 import javax.management.Attribute;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.util.ConfigTransactionClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.confignetconfconnector.exception.NetconfConfigHandlingException;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class MergeEditConfigStrategy extends AbstractEditConfigStrategy {
24
25     private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
26
27     public MergeEditConfigStrategy() {
28
29     }
30
31     @Override
32     void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
33             String module, String instance, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
34         throw new NetconfConfigHandlingException(
35                 String.format("Unable to handle missing instance, no missing instances should appear at this point, missing: %s : %s ",
36                         module,
37                         instance),
38                 NetconfDocumentedException.ErrorType.application,
39                 NetconfDocumentedException.ErrorTag.operation_failed,
40                 NetconfDocumentedException.ErrorSeverity.error);
41     }
42     @Override
43     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
44
45         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
46             try {
47                 AttributeConfigElement ace = configAttributeEntry.getValue();
48
49                 if (!ace.getResolvedValue().isPresent()) {
50                     logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
51                     continue;
52                 }
53
54                 Object value = ace.getResolvedValue().get();
55                 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
56                 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
57             } catch (Exception e) {
58                 throw new NetconfConfigHandlingException(String.format("Unable to set attributes for %s, Error with attribute %s : %s ",
59                         on,
60                         configAttributeEntry.getKey(),
61                         configAttributeEntry.getValue()),
62                         NetconfDocumentedException.ErrorType.application,
63                         NetconfDocumentedException.ErrorTag.operation_failed,
64                         NetconfDocumentedException.ErrorSeverity.error);
65             }
66         }
67     }
68 }