8b7731890c9d89cd9ee6867f2ffc0e69ad8da15a
[bgpcep.git] / tcp-md5 / jni-cfg / src / main / java / org / opendaylight / controller / config / yang / tcpmd5 / jni / cfg / NativeKeyAccessFactoryModule.java
1 /*
2  * Copyright (c) 2014 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.config.yang.tcpmd5.jni.cfg;
9
10 import java.nio.channels.Channel;
11
12 import org.opendaylight.bgpcep.tcpmd5.KeyAccess;
13 import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
14 import org.opendaylight.bgpcep.tcpmd5.jni.NativeKeyAccessFactory;
15 import org.opendaylight.bgpcep.tcpmd5.jni.NativeSupportUnavailableException;
16
17 /**
18  * Service representing a way for accessing key informtion.
19  */
20 public class NativeKeyAccessFactoryModule extends
21         org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.AbstractNativeKeyAccessFactoryModule {
22     private KeyAccessFactory kaf;
23
24     public NativeKeyAccessFactoryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
25             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
26         super(identifier, dependencyResolver);
27     }
28
29     public NativeKeyAccessFactoryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
30             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
31             final org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.NativeKeyAccessFactoryModule oldModule,
32             final java.lang.AutoCloseable oldInstance) {
33         super(identifier, dependencyResolver, oldModule, oldInstance);
34     }
35
36     @Override
37     public void customValidation() {
38         try {
39             kaf = NativeKeyAccessFactory.getInstance();
40         } catch (NativeSupportUnavailableException e) {
41             throw new UnsupportedOperationException("Native support is not available", e);
42         }
43     }
44
45     @Override
46     public java.lang.AutoCloseable createInstance() {
47         final KeyAccessFactory kaf = this.kaf;
48
49         final class CloseableNativeKeyAccessFactory implements AutoCloseable, KeyAccessFactory {
50             @Override
51             public KeyAccess getKeyAccess(final Channel channel) {
52                 return kaf.getKeyAccess(channel);
53             }
54
55             @Override
56             public boolean canHandleChannelClass(final Class<? extends Channel> clazz) {
57                 return kaf.canHandleChannelClass(clazz);
58             }
59
60             @Override
61             public void close() {
62                 // Nothing to do
63             }
64         }
65
66         return new CloseableNativeKeyAccessFactory();
67     }
68 }