blob: 6fbf6318354d55cebbb7bae6fbbef338949b7fd3 [file] [log] [blame]
David Bainbridge7526c452018-04-20 14:14:37 -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.ciena.c5162.netconf;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.HashMap;
22import java.util.Map;
23import java.util.concurrent.CompletableFuture;
24
25import javax.xml.xpath.XPath;
26import javax.xml.xpath.XPathConstants;
27import javax.xml.xpath.XPathExpressionException;
28import javax.xml.xpath.XPathFactory;
29
30import org.onosproject.drivers.netconf.TemplateManager;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.behaviour.PortAdmin;
33import org.onosproject.net.driver.AbstractHandlerBehaviour;
34import org.onosproject.netconf.NetconfController;
35import org.onosproject.netconf.NetconfException;
36import org.onosproject.netconf.NetconfSession;
37import org.slf4j.Logger;
38import org.w3c.dom.Node;
39
40/**
41 * Handles port administration for Ciena 5162 devices using the NETCONF
42 * protocol.
43 */
44public class Ciena5162PortAdmin extends AbstractHandlerBehaviour implements PortAdmin {
45
46 private static final Logger log = getLogger(Ciena5162PortAdmin.class);
47 private static final TemplateManager TEMPLATE_MANAGER = new TemplateManager();
48
49 static {
50 TEMPLATE_MANAGER.load(Ciena5162PortAdmin.class, "/templates/requests/%s.j2", "logicalPort", "port-admin-state");
51 }
52
53 /**
54 * Sets the administrative state of the given port to the given value.
55 *
56 * @param number
57 * port number
58 * @param value
59 * state, true for enabled, false for disabled
60 * @return true if successfully set
61 */
62 private CompletableFuture<Boolean> setAdminState(PortNumber number, Boolean value) {
63 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
64 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
65
66 try {
67 Map<String, Object> templateContext = new HashMap<String, Object>();
68 templateContext.put("port-number", number.toLong());
69 templateContext.put("admin-state", value.toString());
70 Node req = (Node) TEMPLATE_MANAGER.doRequest(session, "port-admin-state", templateContext, "/",
71 XPathConstants.NODE);
72 XPath xp = XPathFactory.newInstance().newXPath();
73
74 // If OK element exists then it worked.
75 Node ok = (Node) xp.evaluate("/rpc-reply/ok", req, XPathConstants.NODE);
76 return CompletableFuture.completedFuture(ok != null);
77 } catch (XPathExpressionException | NetconfException e) {
78 log.error("Unable to set port admin state for port {} to {}", number, handler().data().deviceId(), value,
79 e);
80 }
81 return CompletableFuture.completedFuture(false);
82
83 }
84
85 @Override
86 public CompletableFuture<Boolean> enable(PortNumber number) {
87 return setAdminState(number, true);
88 }
89
90 @Override
91 public CompletableFuture<Boolean> disable(PortNumber number) {
92 return setAdminState(number, false);
93 }
94
95 @Override
96 public CompletableFuture<Boolean> isEnabled(PortNumber number) {
97 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
98 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
99
100 try {
101 Map<String, Object> templateContext = new HashMap<String, Object>();
102 templateContext.put("port-number", number.toString());
103 Node port = TEMPLATE_MANAGER.doRequest(session, "logicalPort", templateContext);
104 XPath xp = XPathFactory.newInstance().newXPath();
105 return CompletableFuture.completedFuture(Boolean.valueOf(xp.evaluate("admin-status/text()", port)));
106 } catch (XPathExpressionException | NetconfException e) {
107 log.error("Unable to query port state for port {} from device {}", number, handler().data().deviceId(), e);
108 }
109 return CompletableFuture.completedFuture(false);
110 }
111
112}