blob: 5a86e8695218369e1add3604e36dcb93beaf281c [file] [log] [blame]
David K. Bainbridge1a10d622018-05-07 12:32:27 -07001/*
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.drivers.netconf;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.nio.charset.StandardCharsets;
23import java.util.HashMap;
24import java.util.Map;
25
26import javax.xml.namespace.QName;
27import javax.xml.parsers.DocumentBuilder;
28import javax.xml.parsers.DocumentBuilderFactory;
29import javax.xml.xpath.XPath;
30import javax.xml.xpath.XPathFactory;
31
32import org.apache.commons.io.IOUtils;
33import org.onosproject.net.DeviceId;
34import org.onosproject.netconf.NetconfDevice;
35import org.onosproject.netconf.NetconfException;
36import org.onosproject.netconf.NetconfSession;
37import org.slf4j.Logger;
38import org.w3c.dom.Document;
39
40import com.google.common.io.Resources;
41
42public class MockTemplateRequestDriver implements TemplateRequestDriver {
43 private static final Logger log = getLogger(MockTemplateRequestDriver.class);
44 private static final DeviceId DEFAULT_RESPONSES_ID = DeviceId.deviceId("mock:default:1234");
45
46 private Map<DeviceId, Map<String, String>> responses = new HashMap<DeviceId, Map<String, String>>();
47
48 private Map<NetconfSession, DeviceId> sessionMap = new HashMap<NetconfSession, DeviceId>();
49
50 @Override
51 public Object doRequest(NetconfSession session, String templateName, Map<String, Object> templateContext,
52 String baseXPath, QName returnType) throws NetconfException {
53
54 try {
55 DeviceId deviceId = sessionMap.get(session);
56 Map<String, String> deviceResponses = responses.get(deviceId);
57 String responseTemplate = null;
58 if (deviceResponses != null) {
59 responseTemplate = deviceResponses.get(templateName);
60 }
61 if (responseTemplate == null) {
62 deviceResponses = responses.get(DEFAULT_RESPONSES_ID);
63 if (deviceResponses != null) {
64 responseTemplate = deviceResponses.get(templateName);
65 }
66 }
67 if (responseTemplate == null) {
68 throw new Exception(
69 String.format("Reponse template '%s' for device '%s' not found", templateName, deviceId));
70 }
71 InputStream resp = IOUtils.toInputStream(responseTemplate, StandardCharsets.UTF_8);
72 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
73 DocumentBuilder builder = builderFactory.newDocumentBuilder();
74 Document document = builder.parse(resp);
75 XPath xp = XPathFactory.newInstance().newXPath();
76 return xp.evaluate(baseXPath, document, returnType);
77 } catch (Exception e) {
78 NetconfException ne = new NetconfException(e.getMessage(), e);
79 throw ne;
80 }
81 }
82
83 public void load(Class<? extends Object> reference, String pattern, DeviceId id, String... reponseNames) {
84 for (String name : reponseNames) {
85 String key = name;
86 String resource;
87
88 // If the template name begins with a '/', then assume it is a full path
89 // specification
90 if (name.charAt(0) == '/') {
91 int start = name.lastIndexOf('/') + 1;
92 int end = name.lastIndexOf('.');
93 if (end == -1) {
94 key = name.substring(start);
95 } else {
96 key = name.substring(start, end);
97 }
98 resource = name;
99 } else {
100 resource = String.format(pattern, name);
101 }
102
103 log.debug("LOAD RESPONSE TEMPLATE: '{}' as '{}' from '{}'", name, key, resource);
104
105 try {
106 DeviceId use = id;
107 if (use == null) {
108 use = DEFAULT_RESPONSES_ID;
109 }
110 Map<String, String> deviceResponses = responses.get(use);
111 if (deviceResponses == null) {
112 deviceResponses = new HashMap<String, String>();
113 responses.put(use, deviceResponses);
114 }
115 deviceResponses.put(name,
116 Resources.toString(Resources.getResource(reference, resource), StandardCharsets.UTF_8));
117 } catch (IOException ioe) {
118 log.error("Unable to load NETCONF response template '{}' from '{}'", key, resource, ioe);
119 }
120 }
121 }
122
123 public void setDeviceMap(Map<DeviceId, NetconfDevice> devicesMap) {
124 // sessionMap.clear();
125
126 for (Map.Entry<DeviceId, NetconfDevice> entry : devicesMap.entrySet()) {
127 sessionMap.put(entry.getValue().getSession(), entry.getKey());
128 }
129 }
130}