Reduce the use of AttrBuilders
[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 com.google.common.annotations.VisibleForTesting;
11 import org.opendaylight.netconf.api.NetconfMessage;
12 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
13 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
14 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
15 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
16 import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
17 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
18 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
19 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
20 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22
23 public class SchemalessNetconfDevice implements
24         RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
25
26     private RemoteDeviceId id;
27     private RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
28     private final SchemalessMessageTransformer messageTransformer;
29     private final BaseRpcSchemalessTransformer rpcTransformer;
30
31     public SchemalessNetconfDevice(final RemoteDeviceId id,
32                                    final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
33         this.id = id;
34         this.salFacade = salFacade;
35         final MessageCounter counter = new MessageCounter();
36         rpcTransformer = new BaseRpcSchemalessTransformer(counter);
37         messageTransformer = new SchemalessMessageTransformer(counter);
38     }
39
40     @VisibleForTesting
41     SchemalessNetconfDevice(final RemoteDeviceId id, final RemoteDeviceHandler<NetconfSessionPreferences> salFacade,
42                             final SchemalessMessageTransformer messageTransformer) {
43         this.id = id;
44         this.salFacade = salFacade;
45         final MessageCounter counter = new MessageCounter();
46         rpcTransformer = new BaseRpcSchemalessTransformer(counter);
47         this.messageTransformer = messageTransformer;
48     }
49
50     @Override public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
51                                             final NetconfDeviceCommunicator netconfDeviceCommunicator) {
52         final SchemalessNetconfDeviceRpc schemalessNetconfDeviceRpc = new SchemalessNetconfDeviceRpc(id,
53                 netconfDeviceCommunicator, rpcTransformer, messageTransformer);
54
55         salFacade.onDeviceConnected(BaseSchema.BASE_NETCONF_CTX.getSchemaContext(),
56                 remoteSessionCapabilities, schemalessNetconfDeviceRpc);
57
58     }
59
60     @Override public void onRemoteSessionDown() {
61         salFacade.onDeviceDisconnected();
62     }
63
64     @Override public void onRemoteSessionFailed(final Throwable throwable) {
65         salFacade.onDeviceFailed(throwable);
66     }
67
68     @Override public void onNotification(final NetconfMessage notification) {
69         salFacade.onNotification(messageTransformer.toNotification(notification));
70     }
71 }