adaaca9c38a2c05427fdc020de883c1ff0651a47
[honeycomb.git] / nat / nat2vpp / src / main / java / io / fd / honeycomb / nat / NatModule.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.nat;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.inject.AbstractModule;
21 import com.google.inject.Provider;
22 import com.google.inject.Singleton;
23 import com.google.inject.multibindings.Multibinder;
24 import io.fd.honeycomb.nat.jvpp.JVppSnatProvider;
25 import io.fd.honeycomb.nat.read.NatReaderFactory;
26 import io.fd.honeycomb.nat.read.ifc.IfcNatReaderFactory;
27 import io.fd.honeycomb.nat.util.MappingEntryContext;
28 import io.fd.honeycomb.nat.write.NatWriterFactory;
29 import io.fd.honeycomb.nat.write.ifc.IfcNatWriterFactory;
30 import io.fd.honeycomb.translate.read.ReaderFactory;
31 import io.fd.honeycomb.translate.write.WriterFactory;
32 import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Module class instantiating nat plugin components.
38  */
39 public final class NatModule extends AbstractModule {
40
41     private static final Logger LOG = LoggerFactory.getLogger(NatModule.class);
42     private final Class<? extends Provider<FutureJVppSnatFacade>> jvppSnatProviderClass;
43
44     public NatModule() {
45         this(JVppSnatProvider.class);
46     }
47
48     @VisibleForTesting
49     NatModule(Class<? extends Provider<FutureJVppSnatFacade>> jvppSnatProvider) {
50         this.jvppSnatProviderClass = jvppSnatProvider;
51     }
52
53     @Override
54     protected void configure() {
55         // Mapping entry context util
56         bind(MappingEntryContext.class).toInstance(new MappingEntryContext());
57
58         LOG.debug("Installing NAT module");
59
60         // Bind to Plugin's JVPP
61         bind(FutureJVppSnatFacade.class).toProvider(jvppSnatProviderClass).in(Singleton.class);
62
63         final Multibinder<ReaderFactory> readBinder = Multibinder.newSetBinder(binder(), ReaderFactory.class);
64         readBinder.addBinding().to(IfcNatReaderFactory.class).in(Singleton.class);
65         readBinder.addBinding().to(NatReaderFactory.class).in(Singleton.class);
66
67         final Multibinder<WriterFactory> writeBinder = Multibinder.newSetBinder(binder(), WriterFactory.class);
68         writeBinder.addBinding().to(IfcNatWriterFactory.class).in(Singleton.class);
69         writeBinder.addBinding().to(NatWriterFactory.class).in(Singleton.class);
70         LOG.info("Module NAT successfully configured");
71     }
72 }