Merge "BUG-1568 Ssh Handler for netconf client Mina implementation"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / client / Invoker.java
1 /*
2  * Copyright (c) 2013 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.controller.netconf.nettyutil.handler.ssh.client;
9
10 import java.io.IOException;
11
12 /**
13  * Abstract class providing mechanism of invoking various SSH level services.
14  * Class is not allowed to be extended, as it provides its own implementations via instance initiators.
15  */
16 abstract class Invoker {
17     private boolean invoked = false;
18
19     private Invoker() {
20     }
21
22     protected boolean isInvoked() {
23         return invoked;
24     }
25
26     public void setInvoked() {
27         this.invoked = true;
28     }
29
30     abstract void invoke(SshSession session) throws IOException;
31
32     public static Invoker netconfSubsystem(){
33         return subsystem("netconf");
34     }
35
36     public static Invoker subsystem(final String subsystem) {
37         return new Invoker() {
38             @Override
39             synchronized void invoke(SshSession session) throws IOException {
40                 if (isInvoked()) {
41                     throw new IllegalStateException("Already invoked.");
42                 }
43                 try {
44                     session.startSubSystem(subsystem);
45                 } finally {
46                     setInvoked();
47                 }
48             }
49         };
50     }
51 }