HONEYCOMB-58 - Routing Api
[honeycomb.git] / vpp-common / vpp-common-integration / src / main / java / io / fd / honeycomb / vpp / common / integration / JVppCoreProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and/or 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.vpp.common.integration;
18
19 import com.google.inject.Inject;
20 import io.fd.honeycomb.infra.distro.ProviderTrait;
21 import java.io.IOException;
22 import io.fd.vpp.jvpp.JVppRegistry;
23 import io.fd.vpp.jvpp.core.JVppCoreImpl;
24 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
25 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Provides future API for jvpp-core plugin. Must be a singleton due to shutdown hook usage. Registers shutdown hook to
31  * free plugin's resources on shutdown.
32  */
33 public final class JVppCoreProvider extends ProviderTrait<FutureJVppCore> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(JVppCoreProvider.class);
36
37     @Inject
38     private JVppRegistry registry;
39
40     @Override
41     protected FutureJVppCoreFacade create() {
42         try {
43             final JVppCoreImpl jVpp = new JVppCoreImpl();
44             // Free jvpp-core plugin's resources on shutdown
45             Runtime.getRuntime().addShutdownHook(new Thread() {
46
47                 @Override
48                 public void run() {
49                     LOG.info("Unloading jvpp-core plugin");
50                     jVpp.close();
51                     LOG.info("Successfully unloaded jvpp-core plugin");
52                 }
53             });
54
55             LOG.info("Successfully loaded jvpp-core plugin");
56             return new FutureJVppCoreFacade(registry, jVpp);
57         } catch (IOException e) {
58             throw new IllegalStateException("Unable to open VPP management connection", e);
59         }
60     }
61 }