2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig;
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;
20 import javax.management.Attribute;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.ObjectName;
24 import java.util.Map.Entry;
26 public class MergeEditConfigStrategy extends AbstractEditConfigStrategy {
28 private static final Logger logger = LoggerFactory.getLogger(MergeEditConfigStrategy.class);
29 private final Multimap<String, String> providedServices;
31 public MergeEditConfigStrategy() {
32 this.providedServices = HashMultimap.create();
35 public MergeEditConfigStrategy(Multimap<String, String> providedServices) {
36 this.providedServices = providedServices;
40 void handleMissingInstance(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta,
41 String module, String instance, ServiceRegistryWrapper services) {
42 throw new IllegalStateException(
43 "Unable to handle missing instance, no missing instances should appear at this point, missing: "
44 + module + ":" + instance);
47 private void addRefNames(ServiceRegistryWrapper services, Multimap<String, String> providedServices, ConfigTransactionClient ta, ObjectName on) throws InstanceNotFoundException {
48 for (Entry<String, String> namespaceToService : providedServices.entries()) {
50 if(services.hasRefName(namespaceToService.getKey(),
51 namespaceToService.getValue(), on))
54 String refName = services.getNewDefaultRefName(namespaceToService.getKey(), namespaceToService.getValue(),
55 ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
56 ta.saveServiceReference(
57 ta.getServiceInterfaceName(namespaceToService.getKey(), namespaceToService.getValue()), refName, on);
62 void executeStrategy(Map<String, AttributeConfigElement> configuration, ConfigTransactionClient ta, ObjectName on, ServiceRegistryWrapper services) {
64 addRefNames(services, providedServices, ta, on);
65 } catch (InstanceNotFoundException e) {
66 throw new IllegalStateException("Unable to save default ref name for instance " + on, e);
69 for (Entry<String, AttributeConfigElement> configAttributeEntry : configuration.entrySet()) {
71 AttributeConfigElement ace = configAttributeEntry.getValue();
73 if (!ace.getResolvedValue().isPresent()) {
74 logger.debug("Skipping attribute {} for {}", configAttributeEntry.getKey(), on);
78 Object value = ace.getResolvedValue().get();
79 ta.setAttribute(on, ace.getJmxName(), new Attribute(ace.getJmxName(), value));
80 logger.debug("Attribute {} set to {} for {}", configAttributeEntry.getKey(), value, on);
81 } catch (Exception e) {
82 throw new IllegalStateException("Unable to set attributes for " + on + ", Error with attribute "
83 + configAttributeEntry.getKey() + ":" + configAttributeEntry.getValue(), e);