BUG 2610: Delete config:service instance via restconf 22/21322/3
authorTomas Cere <tcere@cisco.com>
Thu, 28 May 2015 07:56:12 +0000 (09:56 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 4 Jun 2015 10:40:54 +0000 (10:40 +0000)
Adds support for removing service instances to config subsystem.

Change-Id: I92211a67d1e74ca4421bd0158f2c3600fabc18cc
Signed-off-by: Tomas Cere <tcere@cisco.com>
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/config/Services.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfig.java

index a49eda3..74655f9 100644 (file)
@@ -23,11 +23,13 @@ import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
 public final class Services {
 
+    private static final String EMPTY_PROVIDER = "";
     private static final String PROVIDER_KEY = "provider";
     private static final String NAME_KEY = "name";
     public static final String TYPE_KEY = "type";
@@ -68,9 +70,11 @@ public final class Services {
                     }
 
                     String refName = refEntry.getKey();
-
-                    ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
-                    refNameToInstance.put(refName, serviceInstance);
+                    //we want to compare reference not value of the provider
+                    refNameToInstance.put(refName, refEntry.getValue() == EMPTY_PROVIDER
+                            //provider name cannot be EMPTY_PROVIDER instance unless we are executing delete
+                            ? ServiceInstance.EMPTY_SERVICE_INSTANCE
+                            : ServiceInstance.fromString(refEntry.getValue()));
 
                 }
             }
@@ -114,12 +118,21 @@ public final class Services {
                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
                 String refName = nameElement.getTextContent();
 
-                XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
-                String providerName = providerElement.getTextContent();
+                if (!ModifyAction.DELETE.toString().toLowerCase().equals(
+                        instance.getAttribute(
+                                XmlNetconfConstants.OPERATION_ATTR_KEY,
+                                XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)))
+                {
+                    XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
+                    String providerName = providerElement.getTextContent();
 
-                instance.checkUnrecognisedElements(nameElement, providerElement);
+                    instance.checkUnrecognisedElements(nameElement, providerElement);
 
-                innerMap.put(refName, providerName);
+                    innerMap.put(refName, providerName);
+                } else {
+                    //since this is a delete we dont have a provider name - we want empty service instance
+                    innerMap.put(refName, EMPTY_PROVIDER);
+                }
             }
         }
 
@@ -161,6 +174,8 @@ public final class Services {
     }
 
     public static final class ServiceInstance {
+        public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
+
         public ServiceInstance(String moduleName, String instanceName) {
             this.moduleName = moduleName;
             this.instanceName = instanceName;
index 7948bb6..ab2a081 100644 (file)
@@ -36,6 +36,7 @@ import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementDefinition;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
+import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services.ServiceInstance;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfigXmlParser.EditConfigExecution;
 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
@@ -155,11 +156,16 @@ public class EditConfig extends AbstractConfigNetconfOperation {
                 for (Map.Entry<String, Services.ServiceInstance> refNameToServiceEntry : refNameToInstance.entrySet()) {
                     ObjectName on = refNameToServiceEntry.getValue().getObjectName(ta.getTransactionName());
                     try {
-                        ObjectName saved = ta.saveServiceReference(qnameOfService, refNameToServiceEntry.getKey(), on);
-                        LOG.debug("Saving service {} with on {} under name {} with service on {}", qnameOfService,
-                                on, refNameToServiceEntry.getKey(), saved);
+                        if (ServiceInstance.EMPTY_SERVICE_INSTANCE == refNameToServiceEntry.getValue()) {
+                            ta.removeServiceReference(qnameOfService, refNameToServiceEntry.getKey());
+                            LOG.debug("Removing service {} with name {}", qnameOfService, refNameToServiceEntry.getKey());
+                        } else {
+                            ObjectName saved = ta.saveServiceReference(qnameOfService, refNameToServiceEntry.getKey(), on);
+                            LOG.debug("Saving service {} with on {} under name {} with service on {}", qnameOfService,
+                                    on, refNameToServiceEntry.getKey(), saved);
+                        }
                     } catch (InstanceNotFoundException e) {
-                        throw new NetconfDocumentedException(String.format("Unable to save ref name " + refNameToServiceEntry.getKey() + " for instance " + on, e),
+                        throw new NetconfDocumentedException(String.format("Unable to edit ref name " + refNameToServiceEntry.getKey() + " for instance " + on, e),
                                 ErrorType.application,
                                 ErrorTag.operation_failed,
                                 ErrorSeverity.error);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.