2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.nat.write.ifc;
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;
34 abstract class AbstractInterfaceNatCustomizer<D extends DataObject> implements JvppReplyConsumer, WriterCustomizer<D> {
36 private final FutureJVppSnatFacade jvppSnat;
37 private final NamingContext ifcContext;
39 AbstractInterfaceNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat,
40 @Nonnull final NamingContext ifcContext) {
41 this.jvppSnat = jvppSnat;
42 this.ifcContext = ifcContext;
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);
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);
56 final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
57 getLog().debug("NAT " + getType() + " enabled successfully on: {}, reply: {}", ifcName, reply);
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"));
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);
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);
80 final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
81 getLog().debug("NAT " + getType() + " disabled successfully on: {}, reply: {}", ifcName, reply);
85 INBOUND((byte)1), OUTBOUND((byte)0);
87 private final byte isInside;
89 NatType(final byte isInside) {
90 this.isInside = isInside;
94 abstract NatType getType();
95 abstract Logger getLog();
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;