[ONOS-6410] flexible configuration datastore specifier.

- added completer to netconf-config-get

Change-Id: I7cc88637bd51d9f4bea7d906346ffacfbd8706a6
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/DatastoreId.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/DatastoreId.java
new file mode 100644
index 0000000..b0df547
--- /dev/null
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/DatastoreId.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.netconf;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import org.onlab.util.Identifier;
+
+/**
+ * Identifier object to specify datastore.
+ */
+public class DatastoreId extends Identifier<String> {
+
+    /**
+     * A configuration datastore holding
+     * the complete configuration currently active on the device.  The
+     * running configuration datastore always exists.
+     */
+    public static final DatastoreId RUNNING = datastore("running");
+
+    /**
+     * A configuration datastore that
+     * can be manipulated without impacting the device's current
+     * configuration and that can be committed to the running
+     * configuration datastore.  Not all devices support a candidate
+     * configuration datastore.
+     */
+    public static final DatastoreId CANDIDATE = datastore("candidate");
+
+    /**
+     * The configuration datastore
+     * holding the configuration loaded by the device when it boots.
+     * Only present on devices that separate the startup configuration
+     * datastore from the running configuration datastore.
+     */
+    public static final DatastoreId STARTUP = datastore("startup");
+
+    /**
+     * Returns datastore identifier object.
+     *
+     * @param name of the datastore
+     * @return identifier
+     */
+    public static DatastoreId datastore(String name) {
+        return new DatastoreId(name);
+    }
+
+    DatastoreId(String name) {
+        super(name);
+        checkArgument(!name.isEmpty());
+    }
+
+    /**
+     * Returns datastore name as XML tag.
+     * @return xml tag
+     */
+    public String asXml() {
+        return "<" + id() + "/>";
+    }
+
+    @Override
+    public final String toString() {
+        return id();
+    }
+
+}
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
index 4fffde6..6638e32 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
@@ -93,31 +93,75 @@
     String requestSync(String request) throws NetconfException;
 
     /**
-     * Retrives the specified configuration.
+     * Retrieves the specified configuration.
      *
      * @param netconfTargetConfig the type of configuration to retrieve.
      * @return specified configuration.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      */
-    String getConfig(TargetConfig netconfTargetConfig) throws NetconfException;
+    default String getConfig(DatastoreId netconfTargetConfig) throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return getConfig(netconfTargetConfig.id());
+    }
 
     /**
-     * Retrives the specified configuration.
+     * Retrieves the specified configuration.
      *
      * @param netconfTargetConfig the type of configuration to retrieve.
      * @return specified configuration.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      * @deprecated - 1.10.0 Kingfisher use method overload that accepts
-     * org.onosproject.netconf.TargetConfig enum parameter instead
+     * org.onosproject.netconf.TargetConfiguration parameter instead
      */
     @Deprecated
-    String getConfig(String netconfTargetConfig) throws NetconfException;
+    default String getConfig(TargetConfig netconfTargetConfig) throws NetconfException {
+        return getConfig(TargetConfig.toDatastoreId(netconfTargetConfig));
+    }
+
+    /**
+     * Retrieves the specified configuration.
+     *
+     * @param netconfTargetConfig the type of configuration to retrieve.
+     * @return specified configuration.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration parameter instead
+     */
+    @Deprecated
+    default String getConfig(String netconfTargetConfig) throws NetconfException {
+        return getConfig(TargetConfig.toDatastoreId(netconfTargetConfig));
+    }
 
 
     /**
-     * Retrives part of the specivied configuration based on the filterSchema.
+     * Retrieves part of the specified configuration based on the filterSchema.
+     *
+     * @param netconfTargetConfig       the type of configuration to retrieve.
+     * @param configurationFilterSchema XML schema to filter the configuration
+     *                                  elements we are interested in
+     * @return device running configuration.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default String getConfig(DatastoreId netconfTargetConfig,
+                             String configurationFilterSchema)
+            throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return getConfig(netconfTargetConfig.id(), configurationFilterSchema);
+    }
+
+
+    /**
+     * Retrieves part of the specified configuration based on the filterSchema.
      *
      * @param netconfTargetConfig       the type of configuration to retrieve.
      * @param configurationFilterSchema XML schema to filter the configuration
@@ -129,11 +173,14 @@
      * org.onosproject.netconf.TargetConfig enum parameter instead
      */
     @Deprecated
-    String getConfig(String netconfTargetConfig, String configurationFilterSchema)
-            throws NetconfException;
+    default String getConfig(String netconfTargetConfig, String configurationFilterSchema)
+            throws NetconfException {
+        return getConfig(TargetConfig.toDatastoreId(netconfTargetConfig),
+                         configurationFilterSchema);
+    }
 
     /**
-     * Retrives part of the specivied configuration based on the filterSchema.
+     * Retrieves part of the specified configuration based on the filterSchema.
      *
      * @param netconfTargetConfig       the type of configuration to retrieve.
      * @param configurationFilterSchema XML schema to filter the configuration
@@ -141,13 +188,19 @@
      * @return device running configuration.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfig enum parameter instead
      */
-    String getConfig(TargetConfig netconfTargetConfig, String configurationFilterSchema)
-            throws NetconfException;
+    @Deprecated
+    default String getConfig(TargetConfig netconfTargetConfig, String configurationFilterSchema)
+            throws NetconfException {
+        return getConfig(TargetConfig.toDatastoreId(netconfTargetConfig),
+                         configurationFilterSchema);
+    }
 
 
     /**
-     * Retrives part of the specified configuration based on the filterSchema.
+     * Retrieves part of the specified configuration based on the filterSchema.
      *
      * @param newConfiguration configuration to set
      * @return true if the configuration was edited correctly
@@ -158,7 +211,26 @@
     boolean editConfig(String newConfiguration) throws NetconfException;
 
     /**
-     * Retrives part of the specified configuration based on the filterSchema.
+     * Retrieves part of the specified configuration based on the filterSchema.
+     *
+     * @param netconfTargetConfig the targetConfiguration to change
+     * @param mode                selected mode to change the configuration
+     * @param newConfiguration    configuration to set
+     * @return true if the configuration was edited correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default boolean editConfig(DatastoreId netconfTargetConfig, String mode, String newConfiguration)
+            throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return editConfig(netconfTargetConfig.id(), mode, newConfiguration);
+    }
+
+    /**
+     * Retrieves part of the specified configuration based on the filterSchema.
      *
      * @param netconfTargetConfig the targetConfiguration to change
      * @param mode                selected mode to change the configuration
@@ -167,14 +239,18 @@
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      * @deprecated - 1.10.0 Kingfisher use method overload that accepts
-     * org.onosproject.netconf.TargetConfig enum parameter instead
+     * org.onosproject.netconf.TargetConfiguration enum parameter instead
      */
     @Deprecated
-    boolean editConfig(String netconfTargetConfig, String mode, String newConfiguration)
-            throws NetconfException;
+    default boolean editConfig(String netconfTargetConfig, String mode, String newConfiguration)
+            throws NetconfException {
+        return editConfig(TargetConfig.toDatastoreId(netconfTargetConfig),
+                          mode,
+                          newConfiguration);
+    }
 
     /**
-     * Retrives part of the specified configuration based on the filterSchema.
+     * Retrieves part of the specified configuration based on the filterSchema.
      *
      * @param netconfTargetConfig the targetConfiguration to change
      * @param mode                selected mode to change the configuration
@@ -182,9 +258,56 @@
      * @return true if the configuration was edited correctly
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration enum parameter instead
      */
-    boolean editConfig(TargetConfig netconfTargetConfig, String mode, String newConfiguration)
-            throws NetconfException;
+    @Deprecated
+    default boolean editConfig(TargetConfig netconfTargetConfig, String mode, String newConfiguration)
+            throws NetconfException {
+        return editConfig(TargetConfig.toDatastoreId(netconfTargetConfig),
+                          mode,
+                          newConfiguration);
+    }
+
+    /**
+     * Copies the configuration between configuration datastores.
+     * <p>
+     * The target configuration can't be the running one
+     *
+     * @param destination configuration datastore
+     * @param source configuration datastore
+     * @return true if the configuration was copied correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default boolean copyConfig(DatastoreId destination, DatastoreId source)
+            throws NetconfException {
+        // default implementation provided for backward compatibility
+        // but this API should be implemented overriding the default
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return copyConfig(destination.id(), source.id());
+    }
+
+    /**
+     * Copies the new configuration, an Url or a complete configuration xml tree
+     * to the target configuration.
+     * The target configuration can't be the running one
+     *
+     * @param netconfTargetConfig the type of configuration to retrieve.
+     * @param newConfiguration configuration XML to set or URL tag to the configuration
+     * @return true if the configuration was copied correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default boolean copyConfig(DatastoreId netconfTargetConfig, String newConfiguration)
+            throws NetconfException {
+        // default implementation provided for backward compatibility
+        // but this API should be implemented overriding the default
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return copyConfig(netconfTargetConfig.id(), newConfiguration);
+    }
 
     /**
      * Copies the new configuration, an Url or a complete configuration xml tree
@@ -196,10 +319,7 @@
      * @return true if the configuration was copied correctly
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
-     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
-     * org.onosproject.netconf.TargetConfig enum parameter instead
      */
-    @Deprecated
     boolean copyConfig(String netconfTargetConfig, String newConfiguration)
             throws NetconfException;
 
@@ -213,9 +333,45 @@
      * @return true if the configuration was copied correctly
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration enum parameter instead
      */
-    boolean copyConfig(TargetConfig netconfTargetConfig, String newConfiguration)
-            throws NetconfException;
+    @Deprecated
+    default boolean copyConfig(TargetConfig netconfTargetConfig, String newConfiguration)
+            throws NetconfException {
+        return copyConfig(TargetConfig.toDatastoreId(netconfTargetConfig), newConfiguration);
+    }
+
+    /**
+     * Deletes part of the specified configuration based on the filterSchema.
+     *
+     * @param netconfTargetConfig the name of the configuration to delete
+     * @return true if the configuration was copied correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default boolean deleteConfig(DatastoreId netconfTargetConfig) throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return deleteConfig(netconfTargetConfig.id());
+    }
+
+    /**
+     * Deletes part of the specified configuration based on the filterSchema.
+     *
+     * @param netconfTargetConfig the name of the configuration to delete
+     * @return true if the configuration was deleted correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration enum parameter instead
+     */
+    @Deprecated
+    default boolean deleteConfig(String netconfTargetConfig) throws NetconfException {
+        return deleteConfig(TargetConfig.toDatastoreId(netconfTargetConfig));
+    }
 
     /**
      * Deletes part of the specified configuration based on the filterSchema.
@@ -225,20 +381,12 @@
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      * @deprecated - 1.10.0 Kingfisher use method overload that accepts
-     * org.onosproject.netconf.TargetConfig enum parameter instead
+     * org.onosproject.netconf.TargetConfiguration enum parameter instead
      */
     @Deprecated
-    boolean deleteConfig(String netconfTargetConfig) throws NetconfException;
-
-    /**
-     * Deletes part of the specified configuration based on the filterSchema.
-     *
-     * @param netconfTargetConfig the name of the configuration to delete
-     * @return true if the configuration was copied correctly
-     * @throws NetconfException when there is a problem in the communication process on
-     * the underlying connection
-     */
-    boolean deleteConfig(TargetConfig netconfTargetConfig) throws NetconfException;
+    default boolean deleteConfig(TargetConfig netconfTargetConfig) throws NetconfException {
+        return deleteConfig(TargetConfig.toDatastoreId(netconfTargetConfig));
+    }
 
     /**
      * Starts subscription to the device's notifications.
@@ -266,12 +414,49 @@
     /**
      * Locks the specified configuration.
      *
-     * @param configType type of configuration to be locked
+     * @param datastore configuration datastore to be locked
      * @return true if successful.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      */
-    boolean lock(String configType) throws NetconfException;
+    default boolean lock(DatastoreId datastore) throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return lock(datastore.id());
+    }
+
+    /**
+     * Locks the specified configuration.
+     *
+     * @param configType type of configuration to be locked
+     * @return true if successful.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration parameter instead
+     */
+    @Deprecated
+    default boolean lock(String configType) throws NetconfException {
+        return lock(TargetConfig.toDatastoreId(configType));
+    }
+
+    /**
+     * Unlocks the specified configuration.
+     *
+     * @param datastore configuration datastore to be unlocked
+     * @return true if successful.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default boolean unlock(DatastoreId datastore) throws NetconfException {
+        // default implementation provided for backward compatibility
+        // this API is the one, which should be implemented
+        // TODO default implementation here should be removed after
+        // deprecation of the other 2 variants.
+        return unlock(datastore.id());
+    }
 
     /**
      * Unlocks the specified configuration.
@@ -280,8 +465,13 @@
      * @return true if successful.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
+     * @deprecated - 1.10.0 Kingfisher use method overload that accepts
+     * org.onosproject.netconf.TargetConfiguration parameter instead
      */
-    boolean unlock(String configType) throws NetconfException;
+    @Deprecated
+    default boolean unlock(String configType) throws NetconfException {
+        return unlock(TargetConfig.toDatastoreId(configType));
+    }
 
     /**
      * Locks the running configuration.
@@ -290,7 +480,9 @@
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      */
-    boolean lock() throws NetconfException;
+    default boolean lock() throws NetconfException {
+        return lock(DatastoreId.RUNNING);
+    }
 
     /**
      * Unlocks the running configuration.
@@ -299,7 +491,9 @@
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
      */
-    boolean unlock() throws NetconfException;
+    default boolean unlock() throws NetconfException {
+        return unlock(DatastoreId.RUNNING);
+    }
 
     /**
      * Closes the Netconf session with the device.
@@ -372,6 +566,7 @@
         Logger log = LoggerFactory.getLogger(NetconfSession.class);
         log.error("Not implemented/exposed by the underlying session implementation");
     }
+
     /**
      * Sets the ONOS side capabilities.
      *
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
index 94c5a35..5257535 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
@@ -16,9 +16,12 @@
 
 package org.onosproject.netconf;
 
-// TODO Revisit if we this class should be Enum.
-// According to NETCONF RFC,
-// various additional configuration datastores may be defined by capabilities.
+/**
+ * @deprecated in 1.10.0 use TargetConfiguration instead
+ * According to NETCONF RFC,
+ * various additional configuration datastores may be defined by capabilities.
+ */
+@Deprecated
 public enum TargetConfig {
     RUNNING("running"),
     CANDIDATE("candidate"),
@@ -34,6 +37,23 @@
         return valueOf(targetConfig.toUpperCase());
     }
 
+    public static DatastoreId toDatastoreId(String cfg) {
+        return toDatastoreId(toTargetConfig(cfg));
+    }
+
+    public static DatastoreId toDatastoreId(TargetConfig cfg) {
+        switch (cfg) {
+        case CANDIDATE:
+            return DatastoreId.CANDIDATE;
+        case RUNNING:
+            return DatastoreId.RUNNING;
+        case STARTUP:
+            return DatastoreId.STARTUP;
+        default:
+            return DatastoreId.datastore(cfg.name);
+        }
+    }
+
     @Override
     public String toString() {
         return this.name;
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
index fe1d9f9..4a53fb3 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
@@ -16,6 +16,7 @@
 package org.onosproject.netconf.cli.impl;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.netconf.DatastoreId.datastore;
 
 import java.io.IOException;
 
@@ -26,7 +27,6 @@
 import org.onosproject.netconf.NetconfController;
 import org.onosproject.netconf.NetconfDevice;
 import org.onosproject.netconf.NetconfSession;
-import org.onosproject.netconf.TargetConfig;
 
 /**
  * Command that gets the configuration of the specified type from the specified
@@ -42,7 +42,7 @@
     String uri = null;
 
     @Argument(index = 1, name = "cfgType",
-              description = "Configuration datastore name (RUNNING, etc.)",
+              description = "Configuration datastore name (running, etc.)",
               required = true, multiValued = false)
     String cfgType = null;
 
@@ -69,7 +69,7 @@
         }
 
         try {
-            String res = session.getConfig(TargetConfig.toTargetConfig(cfgType));
+            String res = session.getConfig(datastore(cfgType.toLowerCase()));
             print("%s", res);
         } catch (IOException e) {
             log.error("Configuration could not be retrieved", e);
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/DatastoreIdCompleter.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/DatastoreIdCompleter.java
new file mode 100644
index 0000000..3981bed
--- /dev/null
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/DatastoreIdCompleter.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.netconf.cli.impl.completers;
+
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.netconf.DatastoreId;
+
+/**
+ * Completer for predefined {@link DatastoreId}.
+ *
+ */
+public class DatastoreIdCompleter extends AbstractChoicesCompleter {
+
+    @Override
+    protected List<String> choices() {
+        // TODO: Ideally candidates should be chosen based on Device capability
+        return Arrays.asList(DatastoreId.class.getFields())
+                .stream()
+                .filter(f -> f.getType() == DatastoreId.class)
+                .filter(f -> Modifier.isStatic(f.getModifiers()))
+                .map(f -> {
+                    try {
+                        return (DatastoreId) f.get(null);
+                    } catch (IllegalArgumentException | IllegalAccessException e) {
+                        return null;
+                    }
+                })
+                .filter(Objects::nonNull)
+                .map(DatastoreId::id)
+                .collect(Collectors.toList());
+    }
+
+}
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/package-info.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/package-info.java
new file mode 100644
index 0000000..1a8d488
--- /dev/null
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/completers/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * CLI Completers for NETCONF related commands.
+ */
+package org.onosproject.netconf.cli.impl.completers;
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionImpl.java
index b808612..00fe4dc 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionImpl.java
@@ -25,7 +25,7 @@
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
 import org.onosproject.netconf.NetconfSessionFactory;
-import org.onosproject.netconf.TargetConfig;
+import org.onosproject.netconf.DatastoreId;
 import org.onosproject.netconf.FilteringNetconfDeviceOutputEventListener;
 import org.onosproject.netconf.NetconfDeviceInfo;
 import org.onosproject.netconf.NetconfDeviceOutputEvent;
@@ -465,22 +465,13 @@
     }
 
     @Override
-    public String getConfig(TargetConfig netconfTargetConfig) throws NetconfException {
+    public String getConfig(DatastoreId netconfTargetConfig) throws NetconfException {
         return getConfig(netconfTargetConfig, null);
     }
 
     @Override
-    public String getConfig(String netconfTargetConfig) throws NetconfException {
-        return getConfig(TargetConfig.toTargetConfig(netconfTargetConfig));
-    }
-
-    @Override
-    public String getConfig(String netconfTargetConfig, String configurationFilterSchema) throws NetconfException {
-        return getConfig(TargetConfig.toTargetConfig(netconfTargetConfig), configurationFilterSchema);
-    }
-
-    @Override
-    public String getConfig(TargetConfig netconfTargetConfig, String configurationSchema) throws NetconfException {
+    public String getConfig(DatastoreId netconfTargetConfig,
+                            String configurationSchema) throws NetconfException {
         StringBuilder rpc = new StringBuilder(XML_HEADER);
         rpc.append("<rpc ");
         rpc.append(MESSAGE_ID_STRING);
@@ -512,13 +503,7 @@
     }
 
     @Override
-    public boolean editConfig(String netconfTargetConfig, String mode, String newConfiguration)
-            throws NetconfException {
-        return editConfig(TargetConfig.toTargetConfig(netconfTargetConfig), mode, newConfiguration);
-    }
-
-    @Override
-    public boolean editConfig(TargetConfig netconfTargetConfig, String mode, String newConfiguration)
+    public boolean editConfig(DatastoreId netconfTargetConfig, String mode, String newConfiguration)
             throws NetconfException {
         newConfiguration = newConfiguration.trim();
         StringBuilder rpc = new StringBuilder(XML_HEADER);
@@ -550,27 +535,61 @@
     }
 
     @Override
-    public boolean copyConfig(String netconfTargetConfig, String newConfiguration) throws NetconfException {
-        return copyConfig(TargetConfig.toTargetConfig(netconfTargetConfig), newConfiguration);
+    public boolean copyConfig(DatastoreId destination,
+                              DatastoreId source)
+            throws NetconfException {
+        return bareCopyConfig(destination.asXml(), source.asXml());
     }
 
     @Override
-    public boolean copyConfig(TargetConfig netconfTargetConfig, String newConfiguration)
+    public boolean copyConfig(DatastoreId netconfTargetConfig,
+                              String newConfiguration)
             throws NetconfException {
-        newConfiguration = newConfiguration.trim();
-        if (!newConfiguration.startsWith("<config>")) {
-            newConfiguration = "<config>" + newConfiguration
-                    + "</config>";
+        return bareCopyConfig(netconfTargetConfig.asXml(),
+                              normalizeCopyConfigParam(newConfiguration));
+    }
+
+    @Override
+    public boolean copyConfig(String netconfTargetConfig,
+                              String newConfiguration) throws NetconfException {
+        return bareCopyConfig(normalizeCopyConfigParam(netconfTargetConfig),
+                              normalizeCopyConfigParam(newConfiguration));
+    }
+
+    /**
+     * Normalize String parameter passed to copy-config API.
+     * <p>
+     * Provided for backward compatibility purpose
+     *
+     * @param input passed to copyConfig API
+     * @return XML likely to be suitable for copy-config source or target
+     */
+    private static CharSequence normalizeCopyConfigParam(String input) {
+        input = input.trim();
+        if (input.startsWith("<url")) {
+            return input;
+        } else if (!input.startsWith("<")) {
+            // assume it is a datastore name
+            return DatastoreId.datastore(input).asXml();
+        } else if (!input.startsWith("<config>")) {
+            return "<config>" + input + "</config>";
         }
+        return input;
+    }
+
+    private boolean bareCopyConfig(CharSequence target,
+                                   CharSequence source)
+            throws NetconfException {
+
         StringBuilder rpc = new StringBuilder(XML_HEADER);
         rpc.append(RPC_OPEN);
         rpc.append(NETCONF_BASE_NAMESPACE).append(">\n");
         rpc.append("<copy-config>");
         rpc.append("<target>");
-        rpc.append("<").append(netconfTargetConfig).append("/>");
+        rpc.append(target);
         rpc.append("</target>");
         rpc.append("<source>");
-        rpc.append(newConfiguration);
+        rpc.append(source);
         rpc.append("</source>");
         rpc.append("</copy-config>");
         rpc.append("</rpc>");
@@ -579,13 +598,8 @@
     }
 
     @Override
-    public boolean deleteConfig(String netconfTargetConfig) throws NetconfException {
-        return deleteConfig(TargetConfig.toTargetConfig(netconfTargetConfig));
-    }
-
-    @Override
-    public boolean deleteConfig(TargetConfig netconfTargetConfig) throws NetconfException {
-        if (netconfTargetConfig.equals(TargetConfig.RUNNING)) {
+    public boolean deleteConfig(DatastoreId netconfTargetConfig) throws NetconfException {
+        if (netconfTargetConfig.equals(DatastoreId.RUNNING)) {
             log.warn("Target configuration for delete operation can't be \"running\"",
                      netconfTargetConfig);
             return false;
@@ -603,13 +617,13 @@
     }
 
     @Override
-    public boolean lock(String configType) throws NetconfException {
+    public boolean lock(DatastoreId configType) throws NetconfException {
         StringBuilder rpc = new StringBuilder(XML_HEADER);
         rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
         rpc.append("<lock>");
         rpc.append("<target>");
         rpc.append("<");
-        rpc.append(configType);
+        rpc.append(configType.id());
         rpc.append("/>");
         rpc.append("</target>");
         rpc.append("</lock>");
@@ -620,13 +634,13 @@
     }
 
     @Override
-    public boolean unlock(String configType) throws NetconfException {
+    public boolean unlock(DatastoreId configType) throws NetconfException {
         StringBuilder rpc = new StringBuilder(XML_HEADER);
         rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
         rpc.append("<unlock>");
         rpc.append("<target>");
         rpc.append("<");
-        rpc.append(configType);
+        rpc.append(configType.id());
         rpc.append("/>");
         rpc.append("</target>");
         rpc.append("</unlock>");
@@ -637,16 +651,6 @@
     }
 
     @Override
-    public boolean lock() throws NetconfException {
-        return lock("running");
-    }
-
-    @Override
-    public boolean unlock() throws NetconfException {
-        return unlock("running");
-    }
-
-    @Override
     public boolean close() throws NetconfException {
         return close(false);
     }
diff --git a/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 72e3087..f40d464 100644
--- a/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -29,6 +29,7 @@
             <action class="org.onosproject.netconf.cli.impl.NetconfConfigGetCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
+                <ref component-id="targetConfigurationsCompleter"/>
                 <null/>
             </completers>
         </command>
@@ -44,5 +45,6 @@
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
+    <bean id="targetConfigurationsCompleter" class="org.onosproject.netconf.cli.impl.completers.DatastoreIdCompleter"/>
 
 </blueprint>
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfSessionImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfSessionImplTest.java
index 846642c..0602c90 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfSessionImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfSessionImplTest.java
@@ -21,8 +21,8 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assert.assertFalse;
-import static org.onosproject.netconf.TargetConfig.RUNNING;
-import static org.onosproject.netconf.TargetConfig.CANDIDATE;
+import static org.onosproject.netconf.DatastoreId.CANDIDATE;
+import static org.onosproject.netconf.DatastoreId.RUNNING;
 
 import java.io.File;
 import java.util.Arrays;
@@ -46,10 +46,10 @@
 import org.junit.Test;
 import org.onlab.junit.TestTools;
 import org.onlab.packet.Ip4Address;
-import org.onosproject.netconf.TargetConfig;
 import org.onosproject.netconf.NetconfDeviceInfo;
 import org.onosproject.netconf.NetconfException;
 import org.onosproject.netconf.NetconfSession;
+import org.onosproject.netconf.DatastoreId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -163,7 +163,7 @@
         assertNotNull("Incorrect sessionId", session1.getSessionId());
         try {
             assertTrue("NETCONF edit-config command failed",
-                       session1.editConfig(TargetConfig.RUNNING.toString(),
+                       session1.editConfig(DatastoreId.RUNNING,
                                            null, SAMPLE_REQUEST));
         } catch (NetconfException e) {
             e.printStackTrace();
@@ -192,7 +192,7 @@
         assertNotNull("Incorrect sessionId", session1.getSessionId());
         try {
             assertFalse("NETCONF delete-config command failed",
-                        session1.deleteConfig(TargetConfig.RUNNING));
+                        session1.deleteConfig(DatastoreId.RUNNING));
         } catch (NetconfException e) {
             e.printStackTrace();
             fail("NETCONF delete-config test failed: " + e.getMessage());
@@ -206,16 +206,49 @@
         assertNotNull("Incorrect sessionId", session1.getSessionId());
         try {
             assertTrue("NETCONF copy-config command failed",
-                       session1.copyConfig(TargetConfig.RUNNING.toString(),
-                                           "candidate"));
+                       session1.copyConfig(DatastoreId.RUNNING,
+                                           DatastoreId.CANDIDATE));
         } catch (NetconfException e) {
             e.printStackTrace();
-            fail("NETCONF edit-config test failed: " + e.getMessage());
+            fail("NETCONF copy-config test failed: " + e.getMessage());
         }
         log.info("Finishing copy-config async");
     }
 
     @Test
+    public void testCopyConfigXml() {
+        log.info("Starting copy-config XML async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertTrue("NETCONF copy-config command failed",
+                       session1.copyConfig(DatastoreId.RUNNING,
+                                           "<configuration><device-specific/></configuration>"));
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF copy-config test failed: " + e.getMessage());
+        }
+        log.info("Finishing copy-config XML async");
+    }
+
+    // remove test when ready to dump bare XML String API.
+    @Test
+    public void testCopyConfigBareXml() {
+        log.info("Starting copy-config bare XML async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertTrue("NETCONF copy-config command failed",
+                       session1.copyConfig(DatastoreId.RUNNING,
+                                           "<config>"
+                                           + "<configuration><device-specific/></configuration>"
+                                         + "</config>"));
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF copy-config test failed: " + e.getMessage());
+        }
+        log.info("Finishing copy-config bare XML async");
+    }
+
+    @Test
     public void testGetConfigRequest() {
         log.info("Starting get-config async");
         assertNotNull("Incorrect sessionId", session1.getSessionId());
@@ -392,9 +425,9 @@
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc message-id=\")[0-9]*(\") *(xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
                     + "(<edit-config>)\\R?"
-                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
-                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
-                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                    + "(<target>\\R?((<" + DatastoreId.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + DatastoreId.RUNNING.toString() + "/>)|"
+                                    + "(<" + DatastoreId.STARTUP.toString() + "/>))\\R?</target>)\\R?"
                     + "(<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
                     + ".*"
                     + "(</config>)\\R?(</edit-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
@@ -405,9 +438,9 @@
                                     + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "
                                     + "message-id=\")[0-9]*(\">)\\R?"
                                     + "(<lock>)\\R?"
-                                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
-                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
-                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                                    + "(<target>\\R?((<" + DatastoreId.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + DatastoreId.RUNNING.toString() + "/>)|"
+                                    + "(<" + DatastoreId.STARTUP.toString() + "/>))\\R?</target>)\\R?"
                                     + "(</lock>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
     public static final Pattern UNLOCK_REQ_PATTERN =
@@ -415,32 +448,39 @@
                                     + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "
                                     + "message-id=\")[0-9]*(\">)\\R?"
                                     + "(<unlock>)\\R?"
-                                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
-                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
-                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                                    + "(<target>\\R?((<" + DatastoreId.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + DatastoreId.RUNNING.toString() + "/>)|"
+                                    + "(<" + DatastoreId.STARTUP.toString() + "/>))\\R?</target>)\\R?"
                                     + "(</unlock>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
     public static final Pattern COPY_CONFIG_REQ_PATTERN =
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\")[0-9]*(\">)\\R?"
                     + "(<copy-config>)\\R?"
-                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
-                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
-                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
-                                    + "(<source>)\\R?(<config>)(("
-                                    + TargetConfig.CANDIDATE.toString() + ")|("
-                                    + TargetConfig.RUNNING.toString() + ")|("
-                                    + TargetConfig.STARTUP.toString()
-                                    + "))(</config>)\\R?(</source>)\\R?"
-                                    + "(</copy-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
+                    + "(<target>\\R?"
+                    + "("
+                        + "(<" + DatastoreId.CANDIDATE.toString() + "/>)|"
+                        + "(<" + DatastoreId.RUNNING.toString() + "/>)|"
+                        + "(<" + DatastoreId.STARTUP.toString() + "/>)"
+                    + ")\\R?"
+                    + "</target>)\\R?"
+                    + "(<source>)\\R?"
+                    + "("
+                        + "(<config>)(.*)(</config>)|"
+                        + "(<" + DatastoreId.CANDIDATE.toString() + "/>)|"
+                        + "(<" + DatastoreId.RUNNING.toString() + "/>)|"
+                        + "(<" + DatastoreId.STARTUP.toString() + "/>)"
+                    + ")\\R?"
+                    + "(</source>)\\R?"
+                    + "(</copy-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
     public static final Pattern GET_CONFIG_REQ_PATTERN =
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc message-id=\")[0-9]*(\"  xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
                     + "(<get-config>)\\R?" + "(<source>)\\R?((<"
-                                    + TargetConfig.CANDIDATE.toString()
-                                    + "/>)|(<" + TargetConfig.RUNNING.toString()
-                                    + "/>)|(<" + TargetConfig.STARTUP.toString()
+                                    + DatastoreId.CANDIDATE.toString()
+                                    + "/>)|(<" + DatastoreId.RUNNING.toString()
+                                    + "/>)|(<" + DatastoreId.STARTUP.toString()
                                     + "/>))\\R?(</source>)\\R?"
                     + "(<filter type=\"subtree\">).*(</filter>)\\R?"
                     + "(</get-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
@@ -460,10 +500,10 @@
 
     public class NCCopyConfigCallable implements Callable<Boolean> {
         private NetconfSession session;
-        private TargetConfig target;
+        private DatastoreId target;
         private String source;
 
-        public NCCopyConfigCallable(NetconfSession session, TargetConfig target, String source) {
+        public NCCopyConfigCallable(NetconfSession session, DatastoreId target, String source) {
             this.session = session;
             this.target = target;
             this.source = source;