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