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