Merge changes Ic434bf4a,I05a3fb18,I47a3783d,I8234bbfd
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / 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.controller.netconf.test.tool;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
15 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfigXmlParser;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21
22 class SimulatedEditConfig extends AbstractConfigNetconfOperation {
23     private static final String DELETE_EDIT_CONFIG = "delete";
24     private static final String OPERATION = "operation";
25     private static final String REMOVE_EDIT_CONFIG = "remove";
26     private final DataList storage;
27
28     SimulatedEditConfig(final String netconfSessionIdForReporting, final DataList storage) {
29         super(null, netconfSessionIdForReporting);
30         this.storage = storage;
31     }
32
33     @Override
34     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
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 EditConfigXmlParser.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 }