Device config synchronizer

- initial sketch of Device Config Synchronizer outline (ONOS-6745)

Change-Id: I57c8ab6c3511f12c15e3501aa61498eb18264b27
diff --git a/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerComponent.java b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerComponent.java
new file mode 100644
index 0000000..ced2b41
--- /dev/null
+++ b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerComponent.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.d.config.sync.impl.netconf;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.d.config.sync.DeviceConfigSynchronizationProviderRegistry;
+import org.onosproject.d.config.sync.DeviceConfigSynchronizationProviderService;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.yang.model.SchemaContextProvider;
+import org.onosproject.yang.runtime.YangRuntimeService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Main component of Dynamic config synchronizer for NETCONF.
+ * <p>
+ * <ul>
+ * <li> bootstrap Active and Passive synchronization modules
+ * <li> start background anti-entropy mechanism for offline device configuration
+ * </ul>
+ */
+@Component(immediate = true)
+public class NetconfDeviceConfigSynchronizerComponent {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /**
+     * NETCONF dynamic config synchronizer provider ID.
+     */
+    public static final ProviderId PID =
+            new ProviderId("netconf", "org.onosproject.d.config.sync.netconf");
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceConfigSynchronizationProviderRegistry registry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetconfController netconfController;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected YangRuntimeService yangRuntimeService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected SchemaContextProvider schemaContextProvider;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    private NetconfDeviceConfigSynchronizerProvider provider;
+
+    private DeviceConfigSynchronizationProviderService providerService;
+
+
+    @Activate
+    protected void activate() {
+        provider = new NetconfDeviceConfigSynchronizerProvider(PID, new InnerNetconfContext());
+        providerService = registry.register(provider);
+
+        // TODO (Phase 2 or later)
+        //      listen to NETCONF events (new Device appeared, etc.)
+        //      for PASSIVE "state" synchronization upward
+
+        // TODO listen to DeviceEvents (Offline pre-configuration scenario)
+
+        // TODO background anti-entropy mechanism
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        registry.unregister(provider);
+        log.info("Stopped");
+    }
+
+    /**
+     * Context object to provide reference to OSGi services, etc.
+     */
+    @Beta
+    public static interface NetconfContext {
+
+        /**
+         * Returns DeviceConfigSynchronizationProviderService interface.
+         *
+         * @return DeviceConfigSynchronizationProviderService
+         */
+        DeviceConfigSynchronizationProviderService providerService();
+
+        SchemaContextProvider schemaContextProvider();
+
+        YangRuntimeService yangRuntime();
+
+        NetconfController netconfController();
+
+    }
+
+    class InnerNetconfContext implements NetconfContext {
+
+        @Override
+        public NetconfController netconfController() {
+            return netconfController;
+        }
+
+        @Override
+        public YangRuntimeService yangRuntime() {
+            return yangRuntimeService;
+        }
+
+        @Override
+        public SchemaContextProvider schemaContextProvider() {
+            return schemaContextProvider;
+        }
+
+        @Override
+        public DeviceConfigSynchronizationProviderService providerService() {
+            return providerService;
+        }
+    }
+}
diff --git a/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProvider.java b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProvider.java
new file mode 100644
index 0000000..876cceb
--- /dev/null
+++ b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProvider.java
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.d.config.sync.impl.netconf;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.onlab.util.XmlString;
+import org.onosproject.d.config.ResourceIds;
+import org.onosproject.d.config.sync.DeviceConfigSynchronizationProvider;
+import org.onosproject.d.config.sync.impl.netconf.NetconfDeviceConfigSynchronizerComponent.NetconfContext;
+import org.onosproject.d.config.sync.operation.SetRequest;
+import org.onosproject.d.config.sync.operation.SetRequest.Change;
+import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation;
+import org.onosproject.d.config.sync.operation.SetResponse;
+import org.onosproject.d.config.sync.operation.SetResponse.Code;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.DefaultResourceData;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.ResourceData;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.runtime.AnnotatedNodeInfo;
+import org.onosproject.yang.runtime.Annotation;
+import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
+import org.onosproject.yang.runtime.DefaultAnnotation;
+import org.onosproject.yang.runtime.DefaultCompositeData;
+import org.onosproject.yang.runtime.DefaultRuntimeContext;
+import org.onosproject.yang.runtime.RuntimeContext;
+import org.slf4j.Logger;
+import com.google.common.io.CharStreams;
+
+/**
+ * Dynamic config synchronizer provider for NETCONF.
+ * <p>
+ * <ul>
+ * <li> Converts POJO YANG into XML.
+ * <li> Adds NETCONF envelope around it.
+ * <li> Send request down to the device over NETCONF
+ * </ul>
+ */
+public class NetconfDeviceConfigSynchronizerProvider
+        extends AbstractProvider
+        implements DeviceConfigSynchronizationProvider {
+
+    private static final Logger log = getLogger(NetconfDeviceConfigSynchronizerProvider.class);
+
+    // TODO this should probably be defined on YRT Serializer side
+    /**
+     * {@link RuntimeContext} parameter Dataformat specifying XML.
+     */
+    private static final String DATAFORMAT_XML = "xml";
+
+    private static final String XMLNS_XC = "xmlns:xc";
+    private static final String NETCONF_1_0_BASE_NAMESPACE =
+                                    "urn:ietf:params:xml:ns:netconf:base:1.0";
+
+    /**
+     * Annotation to add xc namespace declaration.
+     * {@value #XMLNS_XC}={@value #NETCONF_1_0_BASE_NAMESPACE}
+     */
+    private static final DefaultAnnotation XMLNS_XC_ANNOTATION =
+                    new DefaultAnnotation(XMLNS_XC, NETCONF_1_0_BASE_NAMESPACE);
+
+    private static final String XC_OPERATION = "xc:operation";
+
+
+    private NetconfContext context;
+
+    // FIXME remove and let netconf southbound deal with message-id generation
+    private final AtomicInteger messageId = new AtomicInteger(1);
+
+    protected NetconfDeviceConfigSynchronizerProvider(ProviderId id,
+                                                      NetconfContext context) {
+        super(id);
+        this.context = checkNotNull(context);
+    }
+
+    @Override
+    public CompletableFuture<SetResponse> setConfiguration(DeviceId deviceId,
+                                                           SetRequest request) {
+        // sanity check and handle empty change?
+
+        // TODOs:
+        // - Construct convert request object into XML
+        // --  [FutureWork] may need to introduce behaviour for Device specific
+        //     workaround insertion
+
+        StringBuilder rpc = new StringBuilder();
+
+        // - Add NETCONF envelope
+        rpc.append("<rpc xmlns=\"").append(NETCONF_1_0_BASE_NAMESPACE).append("\" ")
+            .append("message-id=\"").append(messageId.getAndIncrement()).append("\">");
+
+        rpc.append("<edit-config>");
+        rpc.append("<target>");
+        // TODO directly writing to running for now
+        rpc.append("<running/>");
+        rpc.append("</target>\n");
+        rpc.append("<config ")
+            .append(XMLNS_XC).append("=\"").append(NETCONF_1_0_BASE_NAMESPACE).append("\">");
+        // TODO netconf SBI should probably be adding these envelopes once
+        // netconf SBI is in better shape
+        // TODO In such case netconf sbi need to define namespace externally visible.
+        // ("xc" in above instance)
+        // to be used to add operations on config tree nodes
+
+
+        // Convert change(s) into a DataNode tree
+        for (Change change : request.changes()) {
+
+            // TODO switch statement can probably be removed
+            switch (change.op()) {
+            case REPLACE:
+            case UPDATE:
+            case DELETE:
+                // convert DataNode -> ResourceData
+                ResourceData data = toResourceData(change);
+
+                // build CompositeData
+                DefaultCompositeData.Builder compositeData =
+                                        DefaultCompositeData.builder();
+
+                // add ResourceData
+                compositeData.resourceData(data);
+
+                // add AnnotatedNodeInfo operation
+                compositeData.addAnnotatedNodeInfo(toAnnotatedNodeInfo(change.op(), change.path()));
+
+                RuntimeContext yrtContext = new DefaultRuntimeContext.Builder()
+                                           .setDataFormat(DATAFORMAT_XML)
+                                           .addAnnotation(XMLNS_XC_ANNOTATION)
+                                           .build();
+                CompositeStream xml = context.yangRuntime().encode(compositeData.build(),
+                                                                   yrtContext);
+                try {
+                    CharStreams.copy(new InputStreamReader(xml.resourceData(), UTF_8), rpc);
+                } catch (IOException e) {
+                    log.error("IOException thrown", e);
+                    // FIXME handle error
+                }
+                break;
+
+            default:
+                log.error("Should never reach here. {}", change);
+                break;
+            }
+        }
+
+        // - close NETCONF envelope
+        // TODO eventually these should be handled by NETCONF SBI side
+        rpc.append('\n');
+        rpc.append("</config>");
+        rpc.append("</edit-config>");
+        rpc.append("</rpc>");
+
+        // - send requests down to the device
+        NetconfSession session = getNetconfSession(deviceId);
+        if (session == null) {
+            log.error("No session available for {}", deviceId);
+            return completedFuture(SetResponse.response(request,
+                                                        Code.FAILED_PRECONDITION,
+                                                        "No session for " + deviceId));
+        }
+        try {
+            // FIXME Netconf async API is currently screwed up, need to fix
+            // NetconfSession, etc.
+            CompletableFuture<String> response = session.request(rpc.toString());
+            log.info("TRACE: request:\n{}", XmlString.prettifyXml(rpc));
+            return response.handle((resp, err) -> {
+                if (err == null) {
+                    log.info("TRACE: reply:\n{}", XmlString.prettifyXml(resp));
+                    // FIXME check response properly
+                    return SetResponse.ok(request);
+                } else {
+                    return SetResponse.response(request, Code.UNKNOWN, err.getMessage());
+                }
+            });
+        } catch (NetconfException e) {
+            // TODO Handle error
+            log.error("NetconfException thrown", e);
+            return completedFuture(SetResponse.response(request, Code.UNKNOWN, e.getMessage()));
+
+        }
+    }
+
+    // overridable for ease of testing
+    /**
+     * Returns a session for the specified deviceId.
+     *
+     * @param deviceId for which we wish to retrieve a session
+     * @return a NetconfSession with the specified node
+     * or null if this node does not have the session to the specified Device.
+     */
+    protected NetconfSession getNetconfSession(DeviceId deviceId) {
+        NetconfDevice device = context.netconfController().getNetconfDevice(deviceId);
+        checkNotNull(device, "The specified deviceId could not be found by the NETCONF controller.");
+        NetconfSession session = device.getSession();
+        checkNotNull(session, "A session could not be retrieved for the specified deviceId.");
+        return session;
+    }
+
+    /**
+     * Creates AnnotatedNodeInfo for {@code node}.
+     *
+     * @param op operation
+     * @param parent resourceId
+     * @param node the node
+     * @return AnnotatedNodeInfo
+     */
+    static AnnotatedNodeInfo annotatedNodeInfo(Operation op,
+                                               ResourceId parent,
+                                               DataNode node) {
+        return DefaultAnnotatedNodeInfo.builder()
+                .resourceId(ResourceIds.resourceId(parent, node))
+                .addAnnotation(toAnnotation(op))
+                .build();
+    }
+
+    /**
+     * Creates AnnotatedNodeInfo for specified resource path.
+     *
+     * @param op operation
+     * @param path resourceId
+     * @return AnnotatedNodeInfo
+     */
+    static AnnotatedNodeInfo toAnnotatedNodeInfo(Operation op,
+                                               ResourceId path) {
+        return DefaultAnnotatedNodeInfo.builder()
+                .resourceId(path)
+                .addAnnotation(toAnnotation(op))
+                .build();
+    }
+
+    /**
+     * Transform DataNode into a ResourceData.
+     *
+     * @param change object
+     * @return ResourceData
+     */
+    static ResourceData toResourceData(Change change) {
+        DefaultResourceData.Builder builder = DefaultResourceData.builder();
+        builder.resourceId(change.path());
+        if (change.op() != Change.Operation.DELETE) {
+            DataNode dataNode = change.val();
+            if (dataNode instanceof InnerNode) {
+                ((InnerNode) dataNode).childNodes().values().forEach(builder::addDataNode);
+            } else {
+                log.error("Unexpected DataNode encountered", change);
+            }
+        }
+
+        return builder.build();
+    }
+
+    static Annotation toAnnotation(Operation op) {
+        switch (op) {
+        case DELETE:
+            return new DefaultAnnotation(XC_OPERATION, "remove");
+        case REPLACE:
+            return new DefaultAnnotation(XC_OPERATION, "replace");
+        case UPDATE:
+            return new DefaultAnnotation(XC_OPERATION, "merge");
+        default:
+            throw new IllegalArgumentException("Unknown operation " + op);
+        }
+    }
+
+}
diff --git a/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/package-info.java b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/package-info.java
new file mode 100644
index 0000000..bc3c75a
--- /dev/null
+++ b/apps/configsync-netconf/src/main/java/org/onosproject/d/config/sync/impl/netconf/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Implementation of NETCONF dynamic config synchronizer provider.
+ */
+package org.onosproject.d.config.sync.impl.netconf;
diff --git a/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java b/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java
new file mode 100644
index 0000000..1aa3411
--- /dev/null
+++ b/apps/configsync-netconf/src/test/java/org/onosproject/d/config/sync/impl/netconf/NetconfDeviceConfigSynchronizerProviderTest.java
@@ -0,0 +1,379 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.d.config.sync.impl.netconf;
+
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.stringContainsInOrder;
+import static org.junit.Assert.*;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.function.BiFunction;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.io.input.ReaderInputStream;
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.util.XmlString;
+import org.onosproject.d.config.sync.DeviceConfigSynchronizationProviderService;
+import org.onosproject.d.config.sync.impl.netconf.NetconfDeviceConfigSynchronizerComponent.NetconfContext;
+import org.onosproject.d.config.sync.operation.SetRequest;
+import org.onosproject.d.config.sync.operation.SetResponse;
+import org.onosproject.d.config.sync.operation.SetResponse.Code;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.onosproject.netconf.NetconfSessionAdapter;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.DataNode.Type;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.LeafNode;
+import org.onosproject.yang.model.ResourceData;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.SchemaContextProvider;
+import org.onosproject.yang.runtime.AnnotatedNodeInfo;
+import org.onosproject.yang.runtime.CompositeData;
+import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
+import org.onosproject.yang.runtime.DefaultAnnotation;
+import org.onosproject.yang.runtime.DefaultCompositeStream;
+import org.onosproject.yang.runtime.RuntimeContext;
+import org.onosproject.yang.runtime.YangRuntimeService;
+import com.google.common.io.CharSource;
+
+public class NetconfDeviceConfigSynchronizerProviderTest {
+
+    private static final ProviderId PID = new ProviderId("netconf", "test");
+    private static final DeviceId DID = DeviceId.deviceId("netconf:testDevice");
+
+    private static final String XMLNS_XC = "xmlns:xc";
+    private static final String NETCONF_1_0_BASE_NAMESPACE =
+                                    "urn:ietf:params:xml:ns:netconf:base:1.0";
+
+    private static final DefaultAnnotation XC_ANNOTATION =
+            new DefaultAnnotation(XMLNS_XC, NETCONF_1_0_BASE_NAMESPACE);
+
+    private static final DefaultAnnotation AN_XC_REPLACE_OPERATION =
+                        new DefaultAnnotation("xc:operation", "replace");
+
+    private static final DefaultAnnotation AN_XC_REMOVE_OPERATION =
+            new DefaultAnnotation("xc:operation", "remove");
+
+    /**
+     *  Yang namespace for test config data.
+     */
+    private static final String TEST_NS = "testNS";
+
+    private static final ResourceId RID_INTERFACES =
+            ResourceId.builder().addBranchPointSchema("interfaces", TEST_NS).build();
+
+    private NetconfDeviceConfigSynchronizerProvider sut;
+
+    private NetconfContext ncCtx;
+
+
+    // Set following accordingly to suite test scenario
+    NetconfSession testNcSession;
+    YangRuntimeService testYangRuntime;
+
+
+    @Before
+    public void setUp() throws Exception {
+
+        ncCtx = new TestNetconfContext();
+
+        sut = new NetconfDeviceConfigSynchronizerProvider(PID, ncCtx) {
+            // overriding to avoid mocking whole NetconController and all that.
+            @Override
+            protected NetconfSession getNetconfSession(DeviceId deviceId) {
+                assertEquals(DID, deviceId);
+                return testNcSession;
+            }
+        };
+    }
+
+    @Test
+    public void testReplaceOperation() throws Exception {
+        // plug drivers with assertions
+        testYangRuntime = onEncode((data, context) -> {
+            assertEquals("xml", context.getDataFormat());
+            assertThat(context.getProtocolAnnotations(), hasItem(XC_ANNOTATION));
+
+            //  assert CompositeData
+            ResourceData rData = data.resourceData();
+            List<AnnotatedNodeInfo> infos = data.annotatedNodesInfo();
+
+            ResourceId interfacesRid = RID_INTERFACES;
+            AnnotatedNodeInfo intfsAnnot = DefaultAnnotatedNodeInfo.builder()
+                    .resourceId(interfacesRid)
+                    .addAnnotation(AN_XC_REPLACE_OPERATION)
+                    .build();
+            assertThat("interfaces has replace operation", infos, hasItem(intfsAnnot));
+
+            // assertion for ResourceData.
+            assertEquals(RID_INTERFACES, rData.resourceId());
+            assertThat("has 1 child", rData.dataNodes(), hasSize(1));
+            assertThat("which is interface",
+                           rData.dataNodes().get(0).key().schemaId().name(),
+                           is("interface"));
+            // todo: assert the rest of the tree if it make sense.
+
+            // FIXME it's unclear what URI is expected here
+            String id = URI.create("netconf:testDevice").toString();
+
+            String inXml = deviceConfigAsXml("replace");
+
+            return toCompositeStream(id, inXml);
+        });
+        testNcSession = new TestEditNetconfSession();
+
+
+        // building test data
+        ResourceId interfacesId = RID_INTERFACES;
+        DataNode interfaces = deviceConfigNode();
+        SetRequest request = SetRequest.builder()
+                .replace(interfacesId, interfaces)
+                .build();
+
+        // test start
+        CompletableFuture<SetResponse> f = sut.setConfiguration(DID, request);
+        SetResponse response = f.get(5, TimeUnit.MINUTES);
+
+        assertEquals(Code.OK, response.code());
+        assertEquals(request.subjects(), response.subjects());
+    }
+
+
+    @Test
+    public void testDeleteOperation() throws Exception {
+        // plug drivers with assertions
+        testYangRuntime = onEncode((data, context) -> {
+            assertEquals("xml", context.getDataFormat());
+            assertThat(context.getProtocolAnnotations(), hasItem(XC_ANNOTATION));
+
+            //  assert CompositeData
+            ResourceData rData = data.resourceData();
+            List<AnnotatedNodeInfo> infos = data.annotatedNodesInfo();
+
+            ResourceId interfacesRid = RID_INTERFACES;
+            AnnotatedNodeInfo intfsAnnot = DefaultAnnotatedNodeInfo.builder()
+                    .resourceId(interfacesRid)
+                    .addAnnotation(AN_XC_REMOVE_OPERATION)
+                    .build();
+            assertThat("interfaces has replace operation", infos, hasItem(intfsAnnot));
+
+            // assertion for ResourceData.
+            assertEquals(RID_INTERFACES, rData.resourceId());
+            assertThat("has no child", rData.dataNodes(), hasSize(0));
+
+            // FIXME it's unclear what URI is expected here
+            String id = URI.create("netconf:testDevice").toString();
+
+            String inXml = deviceConfigAsXml("remove");
+
+            return toCompositeStream(id, inXml);
+        });
+        testNcSession = new TestEditNetconfSession();
+
+        // building test data
+        ResourceId interfacesId = RID_INTERFACES;
+        SetRequest request = SetRequest.builder()
+                .delete(interfacesId)
+                .build();
+
+        // test start
+        CompletableFuture<SetResponse> f = sut.setConfiguration(DID, request);
+
+        SetResponse response = f.get(5, TimeUnit.MINUTES);
+        assertEquals(Code.OK, response.code());
+        assertEquals(request.subjects(), response.subjects());
+    }
+
+    /**
+     * DataNode for testing.
+     *
+     * <pre>
+     *   +-interfaces
+     *      |
+     *      +- interface{intf-name="en0"}
+     *           |
+     *           +- speed = "10G"
+     *           +- state = "up"
+     *
+     * </pre>
+     * @return DataNode
+     */
+    private DataNode deviceConfigNode() {
+        InnerNode.Builder intfs = InnerNode.builder("interfaces", TEST_NS);
+        intfs.type(Type.SINGLE_INSTANCE_NODE);
+        InnerNode.Builder intf = intfs.createChildBuilder("interface", TEST_NS);
+        intf.type(Type.SINGLE_INSTANCE_LEAF_VALUE_NODE);
+        intf.addKeyLeaf("name", TEST_NS, "Ethernet0/0");
+        LeafNode.Builder speed = intf.createChildBuilder("mtu", TEST_NS, "1500");
+        speed.type(Type.SINGLE_INSTANCE_LEAF_VALUE_NODE);
+
+        intf.addNode(speed.build());
+        intfs.addNode(intf.build());
+        return intfs.build();
+    }
+
+    /**
+     * {@link #deviceConfigNode()} as XML.
+     *
+     * @param operation xc:operation value on {@code interfaces} node
+     * @return XML
+     */
+    private String deviceConfigAsXml(String operation) {
+        return  "<interfaces xmlns=\"http://example.com/schema/1.2/config\""
+                + " xc:operation=\"" + operation + "\">\n" +
+                "  <interface>\n" +
+                "    <name>Ethernet0/0</name>\n" +
+                "    <mtu>1500</mtu>\n" +
+                "  </interface>\n" +
+                "</interfaces>";
+    }
+
+    private String rpcReplyOk(int messageid) {
+        return "<rpc-reply message-id=\"" + messageid + "\"\n" +
+               "      xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
+               "   <ok/>\n" +
+               "</rpc-reply>";
+    }
+
+    private int fetchMessageId(String request) {
+        int messageid;
+        Pattern msgId = Pattern.compile("message-id=['\"]([0-9]+)['\"]");
+        Matcher matcher = msgId.matcher(request);
+        if (matcher.find()) {
+            messageid = Integer.parseInt(matcher.group(1));
+        } else {
+            messageid = -1;
+        }
+        return messageid;
+    }
+
+
+    protected CompositeStream toCompositeStream(String id, String inXml) {
+        try {
+            InputStream xml = new ReaderInputStream(
+                         CharSource.wrap(inXml)
+                             .openStream());
+
+            return new DefaultCompositeStream(id, xml);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Asserts that it received edit-config message and reply Ok.
+     */
+    private class TestEditNetconfSession extends NetconfSessionAdapter {
+        @Override
+        public CompletableFuture<String> request(String request)
+                throws NetconfException {
+            System.out.println("TestEditNetconfSession received:");
+            System.out.println(XmlString.prettifyXml(request));
+
+            // Extremely naive request rpc message check
+            assertThat(request, stringContainsInOrder(Arrays.asList(
+                                  "<rpc",
+                                  "<edit-config",
+                                  "<target",
+                                  "<config",
+
+                                  "</config>",
+                                  "</edit-config>",
+                                  "</rpc>")));
+
+            assertThat("XML namespace decl exists",
+                       request, Matchers.containsString("xmlns:xc"));
+
+            assertThat("netconf operation exists",
+                       request, Matchers.containsString("xc:operation"));
+
+            return CompletableFuture.completedFuture(rpcReplyOk(fetchMessageId(request)));
+        }
+    }
+
+    /**
+     * Creates mock YangRuntimeService.
+     *
+     * @param body to execute when {@link YangRuntimeService#encode(CompositeData, RuntimeContext)} was called.
+     * @return YangRuntimeService instance
+     */
+    TestYangRuntimeService onEncode(BiFunction<CompositeData, RuntimeContext, CompositeStream> body) {
+        return new TestYangRuntimeService() {
+            @Override
+            public CompositeStream encode(CompositeData internal,
+                                          RuntimeContext context) {
+                return body.apply(internal, context);
+            }
+        };
+    }
+
+    private abstract class TestYangRuntimeService implements YangRuntimeService {
+
+        @Override
+        public CompositeStream encode(CompositeData internal,
+                                      RuntimeContext context) {
+            fail("stub not implemented");
+            return null;
+        }
+        @Override
+        public CompositeData decode(CompositeStream external,
+                                    RuntimeContext context) {
+            fail("stub not implemented");
+            return null;
+        }
+    }
+
+    private final class TestNetconfContext implements NetconfContext {
+        @Override
+        public DeviceConfigSynchronizationProviderService providerService() {
+            fail("Add stub driver as necessary");
+            return null;
+        }
+
+        @Override
+        public SchemaContextProvider schemaContextProvider() {
+            fail("Add stub driver as necessary");
+            return null;
+        }
+
+        @Override
+        public YangRuntimeService yangRuntime() {
+            return testYangRuntime;
+        }
+
+        @Override
+        public NetconfController netconfController() {
+            fail("Add stub driver as necessary");
+            return null;
+        }
+    }
+
+}