60961125295a09313c89d7d3c684c2959ceece26
[hc2vpp.git] /
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.hc2vpp.common.integration;
18
19 import com.google.inject.Inject;
20 import io.fd.honeycomb.binding.init.ProviderTrait;
21 import io.fd.honeycomb.data.init.ShutdownHandler;
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 java.io.IOException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Provides future API for jvpp-core plugin. Must be a singleton due to shutdown hook usage. Registers shutdown hook to
32  * free plugin's resources on shutdown.
33  */
34 public final class JVppCoreProvider extends ProviderTrait<FutureJVppCore> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(JVppCoreProvider.class);
37
38     @Inject
39     private JVppRegistry registry;
40
41     @Inject
42     private ShutdownHandler shutdownHandler;
43
44     @Override
45     protected FutureJVppCoreFacade create() {
46         try {
47             final JVppCoreImpl jVpp = new JVppCoreImpl();
48             // Free jvpp-core plugin's resources on shutdown
49             shutdownHandler.register("jvpp-core", jVpp);
50             LOG.info("Successfully loaded jvpp-core plugin");
51             return new FutureJVppCoreFacade(registry, jVpp);
52         } catch (IOException e) {
53             throw new IllegalStateException("Unable to open VPP management connection", e);
54         }
55     }
56 }