Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / rpc / SimulatedEditConfig.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.test.tool.rpc;
9
10 import org.opendaylight.netconf.api.DocumentedException;
11 import org.opendaylight.netconf.api.xml.XmlElement;
12 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
13 import org.opendaylight.netconf.server.api.operations.AbstractLastNetconfOperation;
14 import org.w3c.dom.Attr;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 public class SimulatedEditConfig extends AbstractLastNetconfOperation {
19     private static final String DELETE_EDIT_CONFIG = "delete";
20     private static final String OPERATION = "operation";
21     private static final String REMOVE_EDIT_CONFIG = "remove";
22     private final DataList storage;
23
24     public SimulatedEditConfig(final String netconfSessionIdForReporting, final DataList storage) {
25         super(netconfSessionIdForReporting);
26         this.storage = storage;
27     }
28
29     @Override
30     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
31             throws DocumentedException {
32         final XmlElement configElementData = operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
33
34         containsDelete(configElementData);
35         if (containsDelete(configElementData)) {
36             storage.resetConfigList();
37         } else {
38             storage.setConfigList(configElementData.getChildElements());
39         }
40
41         return document.createElement(XmlNetconfConstants.OK);
42     }
43
44     @Override
45     protected String getOperationName() {
46         return "edit-config";
47     }
48
49     private boolean containsDelete(final XmlElement element) {
50         for (final Attr o : element.getAttributes().values()) {
51             if (o.getLocalName().equals(OPERATION)
52                     && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue()
53                             .equals(REMOVE_EDIT_CONFIG))) {
54                 return true;
55             }
56
57         }
58
59         for (final XmlElement xmlElement : element.getChildElements()) {
60             if (containsDelete(xmlElement)) {
61                 return true;
62             }
63
64         }
65
66         return false;
67     }
68 }