gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 1 | /* |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 2 | * Copyright 2019-present Open Networking Foundation |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.netconf.ctl.impl; |
| 18 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 19 | import org.onosproject.cluster.NodeId; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 20 | import org.onosproject.netconf.AbstractNetconfSession; |
| 21 | import org.onosproject.netconf.NetconfController; |
| 22 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 23 | import org.onosproject.netconf.NetconfException; |
| 24 | import org.onosproject.netconf.NetconfProxyMessage; |
| 25 | import org.onosproject.netconf.NetconfSession; |
| 26 | import org.onosproject.netconf.NetconfSessionFactory; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Arrays; |
| 31 | import java.util.Set; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.ExecutionException; |
| 34 | import java.util.concurrent.TimeUnit; |
| 35 | import java.util.concurrent.TimeoutException; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | public class NetconfSessionProxyImpl extends AbstractNetconfSession { |
| 40 | protected final NetconfDeviceInfo deviceInfo; |
| 41 | protected final NetconfController netconfController; |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 42 | protected final NodeId sessionNodeId; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 43 | 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.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 48 | NetconfController controller, |
| 49 | NodeId nodeId) { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 50 | this.deviceInfo = deviceInfo; |
| 51 | this.netconfController = controller; |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 52 | this.sessionNodeId = nodeId; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 53 | } |
| 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.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 84 | new ArrayList<>(Arrays.asList(request)), |
| 85 | sessionNodeId); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 86 | } |
| 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.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 172 | deviceInfo.getDeviceId(), capabilitiesList, |
| 173 | sessionNodeId); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 174 | 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.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame^] | 187 | return new NetconfSessionProxyImpl(netconfDeviceInfo, |
| 188 | netconfController, |
| 189 | netconfController.getLocalNodeId()); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |