Merge "Bug 451 - Fix netconf exception handling"
[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 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;
21
22 import javax.management.Attribute;
23 import javax.management.InstanceNotFoundException;
24 import javax.management.ObjectName;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 public class MergeEditConfigStrategy extends AbstractEditConfigStrategy {
29
30     private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
31     private final Multimap<String, String> providedServices;
32
33     public MergeEditConfigStrategy() {
34         this.providedServices = HashMultimap.create();
35     }
36
37     public MergeEditConfigStrategy(Multimap<String, String> providedServices) {
38         this.providedServices = providedServices;
39     }
40
41     @Override
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 ",
46                         module,
47                         instance),
48                 NetconfDocumentedException.ErrorType.application,
49                 NetconfDocumentedException.ErrorTag.operation_failed,
50                 NetconfDocumentedException.ErrorSeverity.error);
51     }
52
53     private void addRefNames(ServiceRegistryWrapper services, Multimap<String, String> providedServices, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
54         for (Entry<String, String> namespaceToService : providedServices.entries()) {
55
56             if(services.hasRefName(namespaceToService.getKey(),
57                     namespaceToService.getValue(), on))
58                 continue;
59
60             String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
61                     ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
62             ta.saveServiceReference(
63                     ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
64         }
65     }
66
67     @Override
68     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
69         try {
70             addRefNames(services, providedServices, ta, on);
71         } catch (InstanceNotFoundException e) {
72             throw new NetconfConfigHandlingException(String.format("Unable to save default ref name for instance %s. Instance was not found.",e),
73                     NetconfDocumentedException.ErrorType.application,
74                     NetconfDocumentedException.ErrorTag.operation_failed,
75                     NetconfDocumentedException.ErrorSeverity.error);
76         }
77
78         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
79             try {
80                 AttributeConfigElement ace = configAttributeEntry.getValue();
81
82                 if (!ace.getResolvedValue().isPresent()) {
83                     logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
84                     continue;
85                 }
86
87                 Object value = ace.getResolvedValue().get();
88                 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
89                 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
90             } catch (Exception e) {
91                 throw new NetconfConfigHandlingException(String.format("Unable to set attributes for %s, Error with attribute %s : %s ",
92                         on,
93                         configAttributeEntry.getKey(),
94                         configAttributeEntry.getValue()),
95                         NetconfDocumentedException.ErrorType.application,
96                         NetconfDocumentedException.ErrorTag.operation_failed,
97                         NetconfDocumentedException.ErrorSeverity.error);
98             }
99         }
100     }
101 }