blob: 6e6a99622b9da1090f9793974d184db44bff6f4e [file] [log] [blame]
gyewan.an91d7e7e2019-01-17 15:12:48 +09001/*
gyewan.an3c99ee72019-02-18 15:53:55 +09002 * Copyright 2019-present Open Networking Foundation
gyewan.an91d7e7e2019-01-17 15:12:48 +09003 *
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 */
16
17package org.onosproject.netconf.ctl.impl;
18
gyewan.an3c99ee72019-02-18 15:53:55 +090019import org.onosproject.cluster.NodeId;
gyewan.an91d7e7e2019-01-17 15:12:48 +090020import org.onosproject.netconf.AbstractNetconfSession;
21import org.onosproject.netconf.NetconfController;
22import org.onosproject.netconf.NetconfDeviceInfo;
23import org.onosproject.netconf.NetconfException;
24import org.onosproject.netconf.NetconfProxyMessage;
25import org.onosproject.netconf.NetconfSession;
26import org.onosproject.netconf.NetconfSessionFactory;
27import org.slf4j.Logger;
28
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Set;
32import java.util.concurrent.CompletableFuture;
33import java.util.concurrent.ExecutionException;
34import java.util.concurrent.TimeUnit;
35import java.util.concurrent.TimeoutException;
36
37import static org.slf4j.LoggerFactory.getLogger;
38
39public class NetconfSessionProxyImpl extends AbstractNetconfSession {
40 protected final NetconfDeviceInfo deviceInfo;
41 protected final NetconfController netconfController;
gyewan.an3c99ee72019-02-18 15:53:55 +090042 protected final NodeId sessionNodeId;
gyewan.an91d7e7e2019-01-17 15:12:48 +090043 private static final Logger log = getLogger(NetconfSessionMinaImpl.class);
44
45 private static final int CONFIGURE_REPLY_TIMEOUT_SEC = 5;
46
47 public NetconfSessionProxyImpl(NetconfDeviceInfo deviceInfo,
gyewan.an3c99ee72019-02-18 15:53:55 +090048 NetconfController controller,
49 NodeId nodeId) {
gyewan.an91d7e7e2019-01-17 15:12:48 +090050 this.deviceInfo = deviceInfo;
51 this.netconfController = controller;
gyewan.an3c99ee72019-02-18 15:53:55 +090052 this.sessionNodeId = nodeId;
gyewan.an91d7e7e2019-01-17 15:12:48 +090053 }
54
55 private <T> CompletableFuture<T> executeAtMasterCompletableFuture(
56 NetconfProxyMessage proxyMessage)
57 throws NetconfException {
58 CompletableFuture<T> reply = netconfController.executeAtMaster(proxyMessage);
59 return reply;
60 }
61
62 private <T> T executeAtMaster(NetconfProxyMessage proxyMessage) throws NetconfException {
63 return executeAtMaster(proxyMessage, CONFIGURE_REPLY_TIMEOUT_SEC);
64 }
65
66 private <T> T executeAtMaster(NetconfProxyMessage proxyMessage, int timeout) throws NetconfException {
67 CompletableFuture<T> reply = executeAtMasterCompletableFuture(proxyMessage);
68 try {
69 return reply.get(timeout, TimeUnit.SECONDS);
70 } catch (InterruptedException e) {
71 Thread.currentThread().interrupt();
72 throw new NetconfException(e.getMessage(), e.getCause());
73 } catch (ExecutionException e) {
74 throw new NetconfException(e.getMessage(), e.getCause());
75 } catch (TimeoutException e) {
76 throw new NetconfException(e.getMessage(), e.getCause());
77 }
78
79 }
80
81 protected NetconfProxyMessage makeProxyMessage(NetconfProxyMessage.SubjectType subjectType, String request) {
82 return new DefaultNetconfProxyMessage(subjectType,
83 deviceInfo.getDeviceId(),
gyewan.an3c99ee72019-02-18 15:53:55 +090084 new ArrayList<>(Arrays.asList(request)),
85 sessionNodeId);
gyewan.an91d7e7e2019-01-17 15:12:48 +090086 }
87
88 @Override
89 public CompletableFuture<String> rpc(String request)
90 throws NetconfException {
91 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.RPC,
92 request);
93
94 return executeAtMasterCompletableFuture(proxyMessage);
95 }
96
97 @Override
98 public CompletableFuture<String> request(String request)
99 throws NetconfException {
100 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.REQUEST,
101 request);
102
103 return executeAtMasterCompletableFuture(proxyMessage);
104 }
105
106 @Override
107 public String requestSync(String request, int timeout)
108 throws NetconfException {
109 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.REQUEST_SYNC,
110 request);
111
112 return executeAtMaster(proxyMessage, timeout);
113 }
114
115 @Override
116 public String requestSync(String request)
117 throws NetconfException {
118
119 return requestSync(request, CONFIGURE_REPLY_TIMEOUT_SEC);
120 }
121
122 @Override
123 public void startSubscription(String filterSchema) throws NetconfException {
124 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.START_SUBSCRIPTION,
125 filterSchema);
126
127 executeAtMaster(proxyMessage);
128 }
129
130 @Override
131 public void endSubscription() throws NetconfException {
132 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.END_SUBSCRIPTION,
133 "");
134
135 executeAtMaster(proxyMessage);
136 }
137
138 @Override
139 public String getSessionId() {
140 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.GET_SESSION_ID,
141 "");
142 try {
143 return executeAtMaster(proxyMessage);
144 } catch (NetconfException e) {
145 log.error("Cannot get session id : {}", e);
146 return String.valueOf(-1);
147 }
148
149 }
150
151 @Override
152 public Set<String> getDeviceCapabilitiesSet() {
153 NetconfProxyMessage proxyMessage = makeProxyMessage(NetconfProxyMessage.SubjectType.GET_DEVICE_CAPABILITIES_SET,
154 "");
155 try {
156 return executeAtMaster(proxyMessage);
157 } catch (NetconfException e) {
158 log.error("Could not get device capabilities : {}", e);
159 return null;
160 }
161 }
162
163
164 @Override
165 public void setOnosCapabilities(Iterable<String> capabilities) {
166 ArrayList<String> capabilitiesList = new ArrayList<>();
167 capabilities.spliterator().forEachRemaining(c -> capabilitiesList.add(c));
168
169 NetconfProxyMessage proxyMessage =
170 new DefaultNetconfProxyMessage(
171 NetconfProxyMessage.SubjectType.SET_ONOS_CAPABILITIES,
gyewan.an3c99ee72019-02-18 15:53:55 +0900172 deviceInfo.getDeviceId(), capabilitiesList,
173 sessionNodeId);
gyewan.an91d7e7e2019-01-17 15:12:48 +0900174 try {
175 executeAtMaster(proxyMessage);
176 } catch (NetconfException e) {
177 log.error("Could not set onos capabilities : {}", e);
178 }
179 }
180
181
182 public static class ProxyNetconfSessionFactory implements NetconfSessionFactory {
183
184 @Override
185 public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo,
186 NetconfController netconfController) {
gyewan.an3c99ee72019-02-18 15:53:55 +0900187 return new NetconfSessionProxyImpl(netconfDeviceInfo,
188 netconfController,
189 netconfController.getLocalNodeId());
gyewan.an91d7e7e2019-01-17 15:12:48 +0900190 }
191 }
192}