9e49879b78524552b9c2d5e92cf2f442c71cbe85
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.docs.core.mock.binding;
18
19 import static com.google.inject.name.Names.named;
20
21 import com.google.inject.AbstractModule;
22 import io.fd.honeycomb.data.init.ShutdownHandler;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.vpp.jvpp.JVppRegistry;
25 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
26 import java.lang.reflect.InvocationHandler;
27 import java.lang.reflect.Proxy;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29
30 /**
31  * Use to bypass jvpp registration, and infra modules
32  */
33 public class MockBindingModule extends AbstractModule {
34
35     private static final InvocationHandler NOOP_INVOCATION_HANDLER = (proxy, method, args) -> null;
36
37     @Override
38     protected void configure() {
39         bind(FutureJVppCore.class).toInstance(noOpProxy(FutureJVppCore.class));
40         bind(MappingContext.class).annotatedWith(named("honeycomb-context"))
41                 .toInstance(noOpProxy(MappingContext.class));
42         bind(DataBroker.class).annotatedWith(named("honeycomb-context")).toInstance(noOpProxy(DataBroker.class));
43         bind(JVppRegistry.class).toInstance(noOpProxy(JVppRegistry.class));
44         bind(ShutdownHandler.class).toInstance(noOpProxy(ShutdownHandler.class));
45     }
46
47     static <T> T noOpProxy(Class<T> clazz) {
48         return (T) Proxy.newProxyInstance(MockBindingModule.class.getClassLoader(),
49                 new Class[]{clazz}, NOOP_INVOCATION_HANDLER);
50     }
51 }