Merge "Resolve Bug:853 - remove groovy from config code generator."
[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
61             String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
62                     ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
63             ta.saveServiceReference(
64                     ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
65         }
66     }
67
68     @Override
69     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) throws NetconfConfigHandlingException {
70         try {
71             addRefNames(services, providedServices, ta, on);
72         } catch (InstanceNotFoundException e) {
73             throw new NetconfConfigHandlingException(String.format("Unable to save default ref name for instance %s. Instance was not found.",e),
74                     NetconfDocumentedException.ErrorType.application,
75                     NetconfDocumentedException.ErrorTag.operation_failed,
76                     NetconfDocumentedException.ErrorSeverity.error);
77         }
78
79         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
80             try {
81                 AttributeConfigElement ace = configAttributeEntry.getValue();
82
83                 if (!ace.getResolvedValue().isPresent()) {
84                     logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
85                     continue;
86                 }
87
88                 Object value = ace.getResolvedValue().get();
89                 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
90                 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
91             } catch (Exception e) {
92                 throw new NetconfConfigHandlingException(String.format("Unable to set attributes for %s, Error with attribute %s : %s ",
93                         on,
94                         configAttributeEntry.getKey(),
95                         configAttributeEntry.getValue()),
96                         NetconfDocumentedException.ErrorType.application,
97                         NetconfDocumentedException.ErrorTag.operation_failed,
98                         NetconfDocumentedException.ErrorSeverity.error);
99             }
100         }
101     }
102 }