Migrate netconf-console to Karaf 4 way
[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.api.xml.XmlUtil;
14 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
15 import org.w3c.dom.Attr;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18
19 public class SimulatedEditConfig extends AbstractLastNetconfOperation {
20     private static final String DELETE_EDIT_CONFIG = "delete";
21     private static final String OPERATION = "operation";
22     private static final String REMOVE_EDIT_CONFIG = "remove";
23     private final DataList storage;
24
25     public SimulatedEditConfig(final String netconfSessionIdForReporting, final DataList storage) {
26         super(netconfSessionIdForReporting);
27         this.storage = storage;
28     }
29
30     @Override
31     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
32             throws DocumentedException {
33         final XmlElement configElementData = operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
34
35         containsDelete(configElementData);
36         if (containsDelete(configElementData)) {
37             storage.resetConfigList();
38         } else {
39             storage.setConfigList(configElementData.getChildElements());
40         }
41
42         return XmlUtil.createElement(document, XmlNetconfConstants.OK);
43     }
44
45     @Override
46     protected String getOperationName() {
47         return "edit-config";
48     }
49
50     private boolean containsDelete(final XmlElement element) {
51         for (final Attr o : element.getAttributes().values()) {
52             if (o.getLocalName().equals(OPERATION)
53                     && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue()
54                             .equals(REMOVE_EDIT_CONFIG))) {
55                 return true;
56             }
57
58         }
59
60         for (final XmlElement xmlElement : element.getChildElements()) {
61             if (containsDelete(xmlElement)) {
62                 return true;
63             }
64
65         }
66
67         return false;
68     }
69 }