Specialize RemoteDeviceHandler to NetconfSessionPreferences
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / SchemalessNetconfDevice.java
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 salFacade;
29     private final SchemalessMessageTransformer messageTransformer;
30     private final BaseRpcSchemalessTransformer rpcTransformer;
31
32     public SchemalessNetconfDevice(final BaseNetconfSchemas baseSchemas, final RemoteDeviceId id,
33             final RemoteDeviceHandler 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 salFacade, final SchemalessMessageTransformer messageTransformer) {
45         this.baseSchemas = requireNonNull(baseSchemas);
46         this.id = id;
47         this.salFacade = salFacade;
48         final MessageCounter counter = new MessageCounter();
49         rpcTransformer = new BaseRpcSchemalessTransformer(baseSchemas, counter);
50         this.messageTransformer = messageTransformer;
51     }
52
53     @Override
54     public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
55             final NetconfDeviceCommunicator netconfDeviceCommunicator) {
56         final SchemalessNetconfDeviceRpc schemalessNetconfDeviceRpc = new SchemalessNetconfDeviceRpc(id,
57                 netconfDeviceCommunicator, rpcTransformer, messageTransformer);
58
59         salFacade.onDeviceConnected(baseSchemas.getBaseSchema().getMountPointContext(),
60                 remoteSessionCapabilities, schemalessNetconfDeviceRpc);
61     }
62
63     @Override
64     public void onRemoteSessionDown() {
65         salFacade.onDeviceDisconnected();
66     }
67
68     @Override
69     public void onRemoteSessionFailed(final Throwable throwable) {
70         salFacade.onDeviceFailed(throwable);
71     }
72
73     @Override
74     public void onNotification(final NetconfMessage notification) {
75         salFacade.onNotification(messageTransformer.toNotification(notification));
76     }
77 }