Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / EditConfig.java
1 /*
2  * Copyright (c) 2013 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.confignetconfconnector.operations.editconfig;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.controller.config.api.ValidationException;
16 import org.opendaylight.controller.config.facade.xml.ConfigExecution;
17 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
18 import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
21 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
22 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
23 import org.opendaylight.controller.config.util.xml.XmlElement;
24 import org.opendaylight.controller.config.util.xml.XmlUtil;
25 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31
32 public class EditConfig extends AbstractConfigNetconfOperation {
33
34     private static final Logger LOG = LoggerFactory.getLogger(EditConfig.class);
35
36     private EditConfigXmlParser editConfigXmlParser;
37
38     public EditConfig(final ConfigSubsystemFacade configSubsystemFacade, final String netconfSessionIdForReporting) {
39         super(configSubsystemFacade, netconfSessionIdForReporting);
40         this.editConfigXmlParser = new EditConfigXmlParser();
41     }
42
43     @VisibleForTesting
44     Element getResponseInternal(final Document document,
45             final ConfigExecution configExecution) throws DocumentedException {
46
47         try {
48             getConfigSubsystemFacade().executeConfigExecution(configExecution);
49         } catch (ValidationException e) {
50             LOG.warn("Test phase for {} failed", EditConfigXmlParser.EDIT_CONFIG, e);
51             final Map<String, String> errorInfo = new HashMap<>();
52             errorInfo.put(ErrorTag.operation_failed.name(), e.getMessage());
53             throw new DocumentedException("Test phase: " + e.getMessage(), e, ErrorType.application,
54                     ErrorTag.operation_failed, ErrorSeverity.error, errorInfo);
55         }
56
57         LOG.trace("Operation {} successful", EditConfigXmlParser.EDIT_CONFIG);
58
59         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
60     }
61
62     @Override
63     protected String getOperationName() {
64         return EditConfigXmlParser.EDIT_CONFIG;
65     }
66
67     @Override
68     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException {
69         ConfigExecution configExecution;
70         // FIXME config mapping getter works on dynamic yang store service and so does later executeConfigExecution method
71         // They might have different view of current yangs in ODL and might cause race conditions
72         Config cfg = getConfigSubsystemFacade().getConfigMapping();
73         configExecution = editConfigXmlParser.fromXml(xml, cfg);
74
75         return getResponseInternal(document, configExecution);
76     }
77
78 }