d9526645717bde789e3bb4c47185d31ef09f3e6b
[honeycomb.git] / ioam / impl / src / main / java / io / fd / honeycomb / vppioam / impl / util / JVppIoamProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.vppioam.impl.util;
18
19 import com.google.inject.Inject;
20 import com.google.inject.Provider;
21 import io.fd.honeycomb.infra.distro.ProviderTrait;
22 import java.io.IOException;
23 import io.fd.vpp.jvpp.JVppRegistry;
24 import io.fd.vpp.jvpp.ioamtrace.future.FutureJVppIoamtrace;
25 import io.fd.vpp.jvpp.ioamtrace.future.FutureJVppIoamtraceFacade;
26 import io.fd.vpp.jvpp.ioamtrace.JVppIoamtraceImpl;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Provides future API for jvpp-ioam plugin. Must be a singleton due to shutdown hook usage.
32  * Registers shutdown hook to free plugin's resources on shutdown.
33  */
34 public final class JVppIoamProvider extends ProviderTrait<FutureJVppIoamtraceFacade> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(JVppIoamProvider.class);
37
38     @Inject
39     private JVppRegistry registry;
40
41     @Override
42     protected FutureJVppIoamtraceFacade create() {
43         try {
44             final JVppIoamtraceImpl jVppIoamTr = new JVppIoamtraceImpl();
45             // Free jvpp-ioam plugin's resources on shutdown
46             Runtime.getRuntime().addShutdownHook(new Thread() {
47                 @Override
48                 public void run() {
49                     LOG.info("Unloading jvpp-ioam plugin");
50                     jVppIoamTr.close();
51                     LOG.info("Successfully unloaded jvpp-ioam plugin");
52                 }
53             });
54
55             LOG.debug("Successfully loaded jvpp-ioam plugin");
56             return new FutureJVppIoamtraceFacade(registry, jVppIoamTr);
57         } catch (IOException e) {
58             throw new IllegalStateException("Unable to open VPP management connection", e);
59         }
60     }
61 }
62