Merge "On openflow plugin restart, NPE in tx poller"
[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.util.ConfigTransactionClient;
14 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.management.Attribute;
20 import javax.management.InstanceAlreadyExistsException;
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, Services services) {
43         ObjectName on = null;
44         try {
45             on = ta.createModule(module, instance);
46             logger.debug("New instance for {} {} created under name {}", module, instance, on);
47             addRefNames(services, providedServices, module, instance, ta, on);
48             executeStrategy(configuration, ta, on, services);
49         } catch (InstanceAlreadyExistsException e) {
50             logger.warn("Error creating instance {}:{}, replace failed", module, instance, e);
51             throw new IllegalStateException("Unable to create new instance for " + module + " : " + instance, e);
52         } catch (InstanceNotFoundException e) {
53             throw new IllegalStateException("Unable to save default ref name for instance " + on, e);
54         }
55     }
56
57     private void addRefNames(Services services, Multimap<String, String> providedServices, String module,
58                              String instance, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
59         for (Entry<String, String> namespaceToService : providedServices.entries()) {
60
61             if(services.hasRefName(namespaceToService.getKey(),
62                     namespaceToService.getValue(), on))
63                 continue;
64
65             String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
66                     module, instance);
67             ta.saveServiceReference(
68                     ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
69         }
70     }
71     @Override
72     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, Services services) {
73         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
74             try {
75                 AttributeConfigElement ace = configAttributeEntry.getValue();
76
77                 if (!ace.getResolvedValue().isPresent()) {
78                     Object value = ace.getResolvedDefaultValue();
79                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
80                     logger.debug("Attribute {} set to default value {} for {}", configAttributeEntry.getKey(), value,
81                             on);
82                 } else {
83                     Object value = ace.getResolvedValue().get();
84                     ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
85                     logger.debug("Attribute {} set to value {} for {}", configAttributeEntry.getKey(), value, on);
86                 }
87
88             } catch (Exception e) {
89                 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
90                         + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);
91             }
92         }
93     }
94 }