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