blob: 74f0f3bf4d41278e1c6eff7598d499a4939bfa9d [file] [log] [blame]
Ai Hamanobd51cdd2018-10-18 11:30:07 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.odtn.behaviour;
17
18import com.google.common.io.CharSource;
19import org.onlab.util.XmlString;
20import org.onosproject.net.DeviceId;
21import org.onosproject.netconf.NetconfController;
22import org.onosproject.netconf.NetconfDevice;
23import org.onosproject.netconf.NetconfException;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26import org.w3c.dom.Document;
27import org.w3c.dom.Element;
28
29import javax.xml.parsers.DocumentBuilderFactory;
30import javax.xml.parsers.ParserConfigurationException;
31import java.util.List;
32import java.util.Optional;
33
34import static org.onlab.osgi.DefaultServiceDirectory.getService;
35import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence;
36import static org.onosproject.odtn.utils.YangToolUtil.toDocument;
37
38/**
39 * Utility class for NETCONF driver.
40 */
41public abstract class AbstractOdtnTerminalDeviceDriver {
42
43 protected final Logger log = LoggerFactory.getLogger(getClass());
44
45 private static final String ENAME_ENVELOPE_RPC = "rpc";
46 private static final String ENAME_ENVELOPE_EDITCONFIG = "edit-config";
47 private static final String ENAME_ENVELOPE_TARGET = "target";
48 private static final String ENAME_ENVELOPE_RUNNING = "running";
49 private static final String ENAME_ENVELOPE_CONFIG = "config";
50
51 private static final String NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0";
52 private static final String CONFIG_NAMESPACE_NS = "http://www.w3.org/2000/xmlns/";
53 private static final String CONFIG_NS_PRIFIX = "xmlns:xc";
54
55 protected Document buildEditConfigBody(List<CharSequence> nodes) {
56
57 Document doc;
58 try {
59 doc = DocumentBuilderFactory.newInstance()
60 .newDocumentBuilder().newDocument();
61 } catch (ParserConfigurationException e) {
62 log.error("Unexpected error", e);
63 throw new IllegalStateException(e);
64 }
65
66 Element appendRoot = addEditConfigEnvelope(doc);
67
68 for (CharSequence node : nodes) {
69 Document ldoc = toDocument(CharSource.wrap(node));
70 Element cfgRoot = ldoc.getDocumentElement();
71
72 // move (or copy) node to another Document
73 appendRoot.appendChild(Optional.ofNullable(doc.adoptNode(cfgRoot))
74 .orElseGet(() -> doc.importNode(cfgRoot, true)));
75 }
76
77 log.info("XML:\n{}", XmlString.prettifyXml(toCharSequence(doc)));
78 return doc;
79 }
80
81 protected void configureDevice(DeviceId did, Document doc) {
82
83 NetconfController ctr = getService(NetconfController.class);
84 Optional.ofNullable(ctr.getNetconfDevice(did))
85 .map(NetconfDevice::getSession)
86 .ifPresent(session -> {
87 try {
hiroki096259b2018-12-07 09:33:24 -080088 session.rpc(toCharSequence(doc).toString()).join();
Ai Hamanobd51cdd2018-10-18 11:30:07 +090089 } catch (NetconfException e) {
90 log.error("Exception thrown", e);
91 }
92 });
93 }
94
95 public Element addEditConfigEnvelope(Document doc) {
96
97 // netconf rpc boilerplate part without message-id
98 // rpc
99 // +- edit-config
100 // +- target
101 // | +- running
102 // +- config
103 Element rpc = doc.createElementNS(NAMESPACE, ENAME_ENVELOPE_RPC);
104 doc.appendChild(rpc);
105 Element editConfig = doc.createElement(ENAME_ENVELOPE_EDITCONFIG);
106 rpc.appendChild(editConfig);
107 Element target = doc.createElement(ENAME_ENVELOPE_TARGET);
108 editConfig.appendChild(target);
109 target.appendChild(doc.createElement(ENAME_ENVELOPE_RUNNING));
110
111 Element config = doc.createElement(ENAME_ENVELOPE_CONFIG);
112 config.setAttributeNS(CONFIG_NAMESPACE_NS, CONFIG_NS_PRIFIX, NAMESPACE);
113 editConfig.appendChild(config);
114
115 return config;
116 }
117}