7998a62d45d21158982760bbf36c477859769e42
[netconf.git] /
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import org.opendaylight.netconf.api.NetconfMessage;
14 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
15 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
16 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
17 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
18 import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
19 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseNetconfSchemas;
20 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
21 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
22 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24
25 public class SchemalessNetconfDevice implements RemoteDevice<NetconfDeviceCommunicator> {
26     private final BaseNetconfSchemas baseSchemas;
27     private final RemoteDeviceId id;
28     private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
29     private final SchemalessMessageTransformer messageTransformer;
30     private final BaseRpcSchemalessTransformer rpcTransformer;
31
32     public SchemalessNetconfDevice(final BaseNetconfSchemas baseSchemas, final RemoteDeviceId id,
33                                    final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
34         this.baseSchemas = requireNonNull(baseSchemas);
35         this.id = id;
36         this.salFacade = salFacade;
37         final MessageCounter counter = new MessageCounter();
38         rpcTransformer = new BaseRpcSchemalessTransformer(baseSchemas, counter);
39         messageTransformer = new SchemalessMessageTransformer(counter);
40     }
41
42     @VisibleForTesting
43     SchemalessNetconfDevice(final BaseNetconfSchemas baseSchemas, final RemoteDeviceId id,
44                             final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
45                             final SchemalessMessageTransformer messageTransformer) {
46         this.baseSchemas = requireNonNull(baseSchemas);
47         this.id = id;
48         this.salFacade = salFacade;
49         final MessageCounter counter = new MessageCounter();
50         rpcTransformer = new BaseRpcSchemalessTransformer(baseSchemas, counter);
51         this.messageTransformer = messageTransformer;
52     }
53
54     @Override
55     public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
56                                             final NetconfDeviceCommunicator netconfDeviceCommunicator) {
57         final SchemalessNetconfDeviceRpc schemalessNetconfDeviceRpc = new SchemalessNetconfDeviceRpc(id,
58                 netconfDeviceCommunicator, rpcTransformer, messageTransformer);
59
60         salFacade.onDeviceConnected(baseSchemas.getBaseSchema().getMountPointContext(),
61                 remoteSessionCapabilities, schemalessNetconfDeviceRpc);
62
63     }
64
65     @Override
66     public void onRemoteSessionDown() {
67         salFacade.onDeviceDisconnected();
68     }
69
70     @Override
71     public void onRemoteSessionFailed(final Throwable throwable) {
72         salFacade.onDeviceFailed(throwable);
73     }
74
75     @Override
76     public void onNotification(final NetconfMessage notification) {
77         salFacade.onNotification(messageTransformer.toNotification(notification));
78     }
79 }