2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connect.netconf;
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.INVENTORY_CONNECTED;
12 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.INVENTORY_ID;
13 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.INVENTORY_NODE;
14 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.INVENTORY_PATH;
15 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.NETCONF_INVENTORY_INITIAL_CAPABILITY;
16 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.CONFIG_SOURCE_RUNNING;
17 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_DATA_QNAME;
18 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_GET_CONFIG_QNAME;
19 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.NETCONF_GET_QNAME;
20 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.toFilterStructure;
21 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.toRpcMessage;
22 import static org.opendaylight.controller.sal.connect.netconf.NetconfMapping.wrap;
24 import com.google.common.base.Preconditions;
25 import java.io.InputStream;
26 import java.net.InetSocketAddress;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
34 import java.util.concurrent.ExecutionException;
35 import java.util.concurrent.ExecutorService;
37 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
38 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
39 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
40 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
41 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
42 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
43 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
44 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
45 import org.opendaylight.controller.sal.core.api.Provider;
46 import org.opendaylight.controller.sal.core.api.RpcImplementation;
47 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
48 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
49 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
50 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
51 import org.opendaylight.protocol.framework.ReconnectStrategy;
52 import org.opendaylight.yangtools.concepts.Registration;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
56 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.Node;
58 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
59 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
60 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
61 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
62 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
63 import org.opendaylight.yangtools.yang.model.api.Module;
64 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
67 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
68 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
69 import org.opendaylight.yangtools.yang.parser.impl.util.YangSourceContext;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 import com.google.common.base.Function;
74 import com.google.common.base.Optional;
75 import com.google.common.base.Predicate;
76 import com.google.common.collect.FluentIterable;
77 import com.google.common.collect.Iterables;
78 import com.google.common.util.concurrent.ListenableFuture;
79 import io.netty.util.concurrent.EventExecutor;
81 public class NetconfDevice implements Provider, //
82 DataReader<InstanceIdentifier, CompositeNode>, //
83 DataCommitHandler<InstanceIdentifier, CompositeNode>, //
87 InetSocketAddress socketAddress;
89 MountProvisionInstance mountInstance;
91 EventExecutor eventExecutor;
93 ExecutorService processingExecutor;
95 InstanceIdentifier path;
97 ReconnectStrategy reconnectStrategy;
99 AbstractCachingSchemaSourceProvider<String, InputStream> schemaSourceProvider;
101 private NetconfDeviceSchemaContextProvider deviceContextProvider;
103 protected Logger logger;
105 Registration<DataReader<InstanceIdentifier, CompositeNode>> operReaderReg;
106 Registration<DataReader<InstanceIdentifier, CompositeNode>> confReaderReg;
107 Registration<DataCommitHandler<InstanceIdentifier, CompositeNode>> commitHandlerReg;
108 List<RpcRegistration> rpcReg;
112 MountProvisionService mountService;
114 NetconfClientDispatcher dispatcher;
116 static InstanceIdentifier ROOT_PATH = InstanceIdentifier.builder().toInstance();
118 SchemaSourceProvider<InputStream> remoteSourceProvider;
120 DataBrokerService dataBroker;
122 NetconfDeviceListener listener;
124 private boolean rollbackSupported;
126 private NetconfClientConfiguration clientConfig;
128 public NetconfDevice(String name) {
130 this.logger = LoggerFactory.getLogger(NetconfDevice.class + "#" + name);
131 this.path = InstanceIdentifier.builder(INVENTORY_PATH)
132 .nodeWithKey(INVENTORY_NODE, Collections.<QName, Object>singletonMap(INVENTORY_ID, name)).toInstance();
135 public void start() {
136 checkState(dispatcher != null, "Dispatcher must be set.");
137 checkState(schemaSourceProvider != null, "Schema Source Provider must be set.");
138 checkState(eventExecutor != null, "Event executor must be set.");
140 Preconditions.checkArgument(clientConfig.getSessionListener() instanceof NetconfDeviceListener);
141 listener = (NetconfDeviceListener) clientConfig.getSessionListener();
143 logger.info("Starting NETCONF Client {} for address {}", name, socketAddress);
145 dispatcher.createClient(clientConfig);
148 Optional<SchemaContext> getSchemaContext() {
149 if (deviceContextProvider == null) {
150 return Optional.absent();
152 return deviceContextProvider.currentContext;
156 if (rpcReg != null) {
157 for (RpcRegistration reg : rpcReg) {
162 closeGracefully(confReaderReg);
163 confReaderReg = null;
164 closeGracefully(operReaderReg);
165 operReaderReg = null;
166 closeGracefully(commitHandlerReg);
167 commitHandlerReg = null;
169 updateDeviceState(false, Collections.<QName> emptySet());
172 private void closeGracefully(final AutoCloseable resource) {
173 if (resource != null) {
176 } catch (Exception e) {
177 logger.warn("Ignoring exception while closing {}", resource, e);
182 void bringUp(final SchemaSourceProvider<String> delegate, final Set<QName> capabilities, final boolean rollbackSupported) {
183 // This has to be called from separate thread, not from netty thread calling onSessionUp in DeviceListener.
184 // Reason: delegate.getSchema blocks thread when waiting for response
185 // however, if the netty thread is blocked, no incoming message can be processed
186 // ... netty should pick another thread from pool to process incoming message, but it does not http://netty.io/wiki/thread-model.html
187 // TODO redesign +refactor
188 processingExecutor.submit(new Runnable() {
191 NetconfDevice.this.rollbackSupported = rollbackSupported;
192 remoteSourceProvider = schemaSourceProvider.createInstanceFor(delegate);
193 deviceContextProvider = new NetconfDeviceSchemaContextProvider(NetconfDevice.this, remoteSourceProvider);
194 deviceContextProvider.createContextFromCapabilities(capabilities);
195 if (mountInstance != null && getSchemaContext().isPresent()) {
196 mountInstance.setSchemaContext(getSchemaContext().get());
199 updateDeviceState(true, capabilities);
201 if (mountInstance != null) {
202 confReaderReg = mountInstance.registerConfigurationReader(ROOT_PATH, NetconfDevice.this);
203 operReaderReg = mountInstance.registerOperationalReader(ROOT_PATH, NetconfDevice.this);
204 commitHandlerReg = mountInstance.registerCommitHandler(ROOT_PATH, NetconfDevice.this);
206 List<RpcRegistration> rpcs = new ArrayList<>();
207 // TODO same condition twice
208 if (mountInstance != null && getSchemaContext().isPresent()) {
209 for (RpcDefinition rpc : mountInstance.getSchemaContext().getOperations()) {
210 rpcs.add(mountInstance.addRpcImplementation(rpc.getQName(), NetconfDevice.this));
219 private void updateDeviceState(boolean up, Set<QName> capabilities) {
220 DataModificationTransaction transaction = dataBroker.beginTransaction();
222 CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder();
223 it.setQName(INVENTORY_NODE);
224 it.addLeaf(INVENTORY_ID, name);
225 it.addLeaf(INVENTORY_CONNECTED, up);
227 logger.debug("Client capabilities {}", capabilities);
228 for (QName capability : capabilities) {
229 it.addLeaf(NETCONF_INVENTORY_INITIAL_CAPABILITY, capability.toString());
232 logger.debug("Update device state transaction " + transaction.getIdentifier()
233 + " putting operational data started.");
234 transaction.removeOperationalData(path);
235 transaction.putOperationalData(path, it.toInstance());
236 logger.debug("Update device state transaction " + transaction.getIdentifier()
237 + " putting operational data ended.");
239 // FIXME: this has to be asynchronous
240 RpcResult<TransactionStatus> transactionStatus = null;
242 transactionStatus = transaction.commit().get();
243 } catch (InterruptedException e) {
244 throw new RuntimeException("Interrupted while waiting for response", e);
245 } catch (ExecutionException e) {
246 throw new RuntimeException("Read configuration data " + path + " failed", e);
248 // TODO better ex handling
250 if (transactionStatus.isSuccessful()) {
251 logger.debug("Update device state transaction " + transaction.getIdentifier() + " SUCCESSFUL.");
253 logger.debug("Update device state transaction " + transaction.getIdentifier() + " FAILED!");
254 logger.debug("Update device state transaction status " + transaction.getStatus());
259 public CompositeNode readConfigurationData(InstanceIdentifier path) {
260 RpcResult<CompositeNode> result = null;
262 result = this.invokeRpc(NETCONF_GET_CONFIG_QNAME,
263 wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path))).get();
264 } catch (InterruptedException e) {
265 throw new RuntimeException("Interrupted while waiting for response", e);
266 } catch (ExecutionException e) {
267 throw new RuntimeException("Read configuration data " + path + " failed", e);
270 CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
271 return data == null ? null : (CompositeNode) findNode(data, path);
275 public CompositeNode readOperationalData(InstanceIdentifier path) {
276 RpcResult<CompositeNode> result = null;
278 result = invokeRpc(NETCONF_GET_QNAME, wrap(NETCONF_GET_QNAME, toFilterStructure(path))).get();
279 } catch (InterruptedException e) {
280 throw new RuntimeException("Interrupted while waiting for response", e);
281 } catch (ExecutionException e) {
282 throw new RuntimeException("Read configuration data " + path + " failed", e);
285 CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
286 return (CompositeNode) findNode(data, path);
290 public Set<QName> getSupportedRpcs() {
291 return Collections.emptySet();
295 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
296 return listener.sendRequest(toRpcMessage(rpc, input, getSchemaContext()), rpc);
300 public Collection<ProviderFunctionality> getProviderFunctionality() {
301 return Collections.emptySet();
305 public void onSessionInitiated(ProviderSession session) {
306 dataBroker = session.getService(DataBrokerService.class);
308 DataModificationTransaction transaction = dataBroker.beginTransaction();
309 if (operationalNodeNotExisting(transaction)) {
310 transaction.putOperationalData(path, getNodeWithId());
312 if (configurationNodeNotExisting(transaction)) {
313 transaction.putConfigurationData(path, getNodeWithId());
317 transaction.commit().get();
318 } catch (InterruptedException e) {
319 throw new RuntimeException("Interrupted while waiting for response", e);
320 } catch (ExecutionException e) {
321 throw new RuntimeException("Read configuration data " + path + " failed", e);
324 mountService = session.getService(MountProvisionService.class);
325 if (mountService != null) {
326 mountInstance = mountService.createOrGetMountPoint(path);
330 CompositeNode getNodeWithId() {
331 SimpleNodeTOImpl id = new SimpleNodeTOImpl(INVENTORY_ID, null, name);
332 return new CompositeNodeTOImpl(INVENTORY_NODE, null, Collections.<Node<?>> singletonList(id));
335 boolean configurationNodeNotExisting(DataModificationTransaction transaction) {
336 return null == transaction.readConfigurationData(path);
339 boolean operationalNodeNotExisting(DataModificationTransaction transaction) {
340 return null == transaction.readOperationalData(path);
343 static Node<?> findNode(CompositeNode node, InstanceIdentifier identifier) {
345 Node<?> current = node;
346 for (InstanceIdentifier.PathArgument arg : identifier.getPath()) {
347 if (current instanceof SimpleNode<?>) {
349 } else if (current instanceof CompositeNode) {
350 CompositeNode currentComposite = (CompositeNode) current;
352 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
353 if (current == null) {
354 current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
356 if (current == null) {
357 current = currentComposite.getFirstSimpleByName(arg.getNodeType());
359 if (current == null) {
360 current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
362 if (current == null) {
371 public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
372 DataModification<InstanceIdentifier, CompositeNode> modification) {
373 NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit = new NetconfDeviceTwoPhaseCommitTransaction(this,
374 modification, true, rollbackSupported);
376 twoPhaseCommit.prepare();
377 } catch (InterruptedException e) {
378 throw new RuntimeException("Interrupted while waiting for response", e);
379 } catch (ExecutionException e) {
380 throw new RuntimeException("Read configuration data " + path + " failed", e);
382 return twoPhaseCommit;
385 Set<QName> getCapabilities(Collection<String> capabilities) {
386 return FluentIterable.from(capabilities).filter(new Predicate<String>() {
388 public boolean apply(final String capability) {
389 return capability.contains("?") && capability.contains("module=") && capability.contains("revision=");
391 }).transform(new Function<String, QName>() {
393 public QName apply(final String capability) {
394 String[] parts = capability.split("\\?");
395 String namespace = parts[0];
396 FluentIterable<String> queryParams = FluentIterable.from(Arrays.asList(parts[1].split("&")));
398 String revision = getStringAndTransform(queryParams, "revision=", "revision=");
400 String moduleName = getStringAndTransform(queryParams, "module=", "module=");
402 if (revision == null) {
403 logger.warn("Netconf device was not reporting revision correctly, trying to get amp;revision=");
404 revision = getStringAndTransform(queryParams, "amp;revision==", "revision=");
406 if (revision != null) {
407 logger.warn("Netconf device returned revision incorectly escaped for {}", capability);
410 if (revision == null) {
411 return QName.create(URI.create(namespace), null, moduleName);
413 return QName.create(namespace, revision, moduleName);
416 private String getStringAndTransform(final Iterable<String> queryParams, final String match,
417 final String substringToRemove) {
418 Optional<String> found = Iterables.tryFind(queryParams, new Predicate<String>() {
420 public boolean apply(final String input) {
421 return input.startsWith(match);
425 return found.isPresent() ? found.get().replaceAll(substringToRemove, "") : null;
432 public void close() {
436 public String getName() {
440 public InetSocketAddress getSocketAddress() {
441 return socketAddress;
444 public MountProvisionInstance getMountInstance() {
445 return mountInstance;
448 public void setReconnectStrategy(final ReconnectStrategy reconnectStrategy) {
449 this.reconnectStrategy = reconnectStrategy;
452 public void setProcessingExecutor(final ExecutorService processingExecutor) {
453 this.processingExecutor = processingExecutor;
456 public void setSocketAddress(final InetSocketAddress socketAddress) {
457 this.socketAddress = socketAddress;
460 public void setEventExecutor(final EventExecutor eventExecutor) {
461 this.eventExecutor = eventExecutor;
464 public void setSchemaSourceProvider(final AbstractCachingSchemaSourceProvider<String, InputStream> schemaSourceProvider) {
465 this.schemaSourceProvider = schemaSourceProvider;
468 public void setDispatcher(final NetconfClientDispatcher dispatcher) {
469 this.dispatcher = dispatcher;
472 public void setClientConfig(final NetconfClientConfiguration clientConfig) {
473 this.clientConfig = clientConfig;
478 class NetconfDeviceSchemaContextProvider {
480 NetconfDevice device;
482 SchemaSourceProvider<InputStream> sourceProvider;
484 Optional<SchemaContext> currentContext;
486 NetconfDeviceSchemaContextProvider(NetconfDevice device, SchemaSourceProvider<InputStream> sourceProvider) {
487 this.device = device;
488 this.sourceProvider = sourceProvider;
489 this.currentContext = Optional.absent();
492 void createContextFromCapabilities(Iterable<QName> capabilities) {
493 YangSourceContext sourceContext = YangSourceContext.createFrom(capabilities, sourceProvider);
494 if (!sourceContext.getMissingSources().isEmpty()) {
495 device.logger.warn("Sources for following models are missing {}", sourceContext.getMissingSources());
497 device.logger.debug("Trying to create schema context from {}", sourceContext.getValidSources());
498 List<InputStream> modelsToParse = YangSourceContext.getValidInputStreams(sourceContext);
499 if (!sourceContext.getValidSources().isEmpty()) {
500 SchemaContext schemaContext = tryToCreateContext(modelsToParse);
501 currentContext = Optional.fromNullable(schemaContext);
503 currentContext = Optional.absent();
505 if (currentContext.isPresent()) {
506 device.logger.debug("Schema context successfully created.");
510 SchemaContext tryToCreateContext(List<InputStream> modelsToParse) {
511 YangParserImpl parser = new YangParserImpl();
514 Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
515 return parser.resolveSchemaContext(models);
516 } catch (Exception e) {
517 device.logger.debug("Error occured during parsing YANG schemas", e);