HONEYCOMB-58 - Routing Api
[honeycomb.git] / nat / nat2vpp / src / main / java / io / fd / honeycomb / nat / write / ifc / AbstractInterfaceNatCustomizer.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.write.ifc;
18
19 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
20 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
21 import io.fd.honeycomb.translate.vpp.util.NamingContext;
22 import io.fd.honeycomb.translate.write.WriteContext;
23 import io.fd.honeycomb.translate.write.WriteFailedException;
24 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceAddDelFeature;
25 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceAddDelFeatureReply;
26 import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
27 import java.util.concurrent.CompletionStage;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33
34 abstract class AbstractInterfaceNatCustomizer<D extends DataObject> implements JvppReplyConsumer, WriterCustomizer<D> {
35
36     private final FutureJVppSnatFacade jvppSnat;
37     private final NamingContext ifcContext;
38
39     AbstractInterfaceNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat,
40                                    @Nonnull final NamingContext ifcContext) {
41         this.jvppSnat = jvppSnat;
42         this.ifcContext = ifcContext;
43     }
44
45     @Override
46     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter,
47                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
48         final String ifcName = id.firstKeyOf(Interface.class).getName();
49         getLog().debug("Enabling " + getType() + " NAT on interface: {}", ifcName);
50         getLog().debug("Enabling " + getType() + " NAT: {}", id);
51
52         final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
53         final SnatInterfaceAddDelFeature request = getRequest(ifcIndex, (byte)1);
54         final CompletionStage<SnatInterfaceAddDelFeatureReply> future = jvppSnat.snatInterfaceAddDelFeature(request);
55
56         final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
57         getLog().debug("NAT " + getType() + " enabled successfully on: {}, reply: {}", ifcName, reply);
58     }
59
60     @Override
61     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
62                                         @Nonnull final D dataBefore, @Nonnull final D dataAfter,
63                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
64         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
65                 new UnsupportedOperationException("Unable to update NAT feature"));
66     }
67
68     @Override
69     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
70                                         @Nonnull final D dataBefore, @Nonnull final WriteContext writeContext)
71             throws WriteFailedException {
72         final String ifcName = id.firstKeyOf(Interface.class).getName();
73         getLog().debug("Disabling " + getType() + " NAT on interface: {}", ifcName);
74         getLog().debug("Disabling " + getType() + " NAT: {}", id);
75
76         final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
77         final SnatInterfaceAddDelFeature request = getRequest(ifcIndex, (byte)0);
78         final CompletionStage<SnatInterfaceAddDelFeatureReply> future = jvppSnat.snatInterfaceAddDelFeature(request);
79
80         final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
81         getLog().debug("NAT " + getType() + " disabled successfully on: {}, reply: {}", ifcName, reply);
82     }
83
84     enum NatType {
85         INBOUND((byte)1), OUTBOUND((byte)0);
86
87         private final byte isInside;
88
89         NatType(final byte isInside) {
90             this.isInside = isInside;
91         }
92     }
93
94     abstract NatType getType();
95     abstract Logger getLog();
96
97     private SnatInterfaceAddDelFeature getRequest(final int ifcIdx, final byte isAdd) {
98         final SnatInterfaceAddDelFeature request = new SnatInterfaceAddDelFeature();
99         request.isAdd = isAdd;
100         request.isInside = getType().isInside;
101         request.swIfIndex = ifcIdx;
102         return request;
103     }
104
105 }