Merge "Bug 116 - Revisit SanityTest"
[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.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 MergeEditConfigStrategy extends AbstractEditConfigStrategy {
27
28     private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
29     private final Multimap<String, String> providedServices;
30
31     public MergeEditConfigStrategy() {
32         this.providedServices = HashMultimap.create();
33     }
34
35     public MergeEditConfigStrategy(Multimap<String, String> providedServices) {
36         this.providedServices = providedServices;
37     }
38
39     @Override
40     void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
41                                String module, String instance, Services services) {
42         ObjectName on = null;
43         try {
44             on = ta.createModule(module, instance);
45             logger.info("New instance for {} {} created under name {}", module, instance, on);
46             addRefNames(services, providedServices, module, instance, ta, on);
47             executeStrategy(configuration, ta, on, services);
48         } catch (InstanceAlreadyExistsException e1) {
49             throw new IllegalStateException("Unable to create instance for " + module + " : " + instance);
50         } catch (InstanceNotFoundException e) {
51             throw new IllegalStateException("Unable to save default ref name for instance " + on, e);
52         }
53     }
54
55     private void addRefNames(Services services, Multimap<String, String> providedServices, String module,
56             String instance, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
57         for (Entry<String, String> namespaceToService : providedServices.entries()) {
58
59             if(services.hasRefName(namespaceToService.getKey(),
60                     namespaceToService.getValue(), on))
61                 continue;
62
63             String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
64                     module, instance);
65             ta.saveServiceReference(
66                     ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
67         }
68     }
69
70     @Override
71     void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, Services services) {
72         for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
73             try {
74                 AttributeConfigElement ace = configAttributeEntry.getValue();
75
76                 if (!ace.getResolvedValue().isPresent()) {
77                     logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
78                     continue;
79                 }
80
81                 Object value = ace.getResolvedValue().get();
82                 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
83                 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
84             } catch (Exception e) {
85                 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
86                         + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);
87             }
88         }
89     }
90 }