Merge "Use Java 7 new method getLoopbackAddress in ClusterManager"
[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.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import javax.management.Attribute;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.ObjectName;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 public class ReplaceEditConfigStrategy extends AbstractEditConfigStrategy {
27
28     private static final Logger logger = LoggerFactory.getLogger(ReplaceEditConfigStrategy.class);
29
30     private final Multimap<String, String> providedServices;
31
32     public ReplaceEditConfigStrategy() {
33         this.providedServices = HashMultimap.create();
34     }
35
36     public ReplaceEditConfigStrategy(Multimap<String, String> providedServices) {
37         this.providedServices = providedServices;
38     }
39
40     @Override
41     void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
42                                String module, String instance, ServiceRegistryWrapper services) {
43         throw new IllegalStateException(
44                 "Unable to handle missing instance, no missing instances should appear at this point, missing: "
45                         + module + ":" + instance);
46     }
47
48     private void addRefNames(ServiceRegistryWrapper services, Multimap<String, String> providedServices, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
49         for (Entry<String, String> namespaceToService : providedServices.entries()) {
50
51             if(services.hasRefName(namespaceToService.getKey(),
52                     namespaceToService.getValue(), on))
53                 continue;
54
55             String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
56                     ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
57             ta.saveServiceReference(
58                     ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
59         }
60     }
61
62     @Override
63     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) {
64         try {
65             addRefNames(services, providedServices, ta, on);
66         } catch (InstanceNotFoundException e) {
67             throw new IllegalStateException("Unable to save default ref name for instance " + on, e);
68         }
69
70         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
71             try {
72                 AttributeConfigElement ace = configAttributeEntry.getValue();
73
74                 if (!ace.getResolvedValue().isPresent()) {
75                     Object value = ace.getResolvedDefaultValue();
76                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
77                     logger.debug("Attribute {} set to default value {} for {}", configAttributeEntry.getKey(), value,
78                             on);
79                 } else {
80                     Object value = ace.getResolvedValue().get();
81                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
82                     logger.debug("Attribute {} set to value {} for {}", configAttributeEntry.getKey(), value, on);
83                 }
84
85             } catch (Exception e) {
86                 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
87                         + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);
88             }
89         }
90     }
91 }