gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.netconf.ctl.impl; |
| 18 | |
| 19 | import org.onosproject.netconf.AbstractNetconfSession; |
| 20 | import org.onosproject.netconf.NetconfController; |
| 21 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 22 | import org.onosproject.netconf.NetconfException; |
| 23 | import org.onosproject.netconf.NetconfProxyMessage; |
| 24 | import org.onosproject.netconf.NetconfSession; |
| 25 | import org.onosproject.netconf.NetconfSessionFactory; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.CompletableFuture; |
| 32 | import java.util.concurrent.ExecutionException; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | import java.util.concurrent.TimeoutException; |
| 35 | |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | public 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 | } |