HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vpp / ArpTerminationTableEntryCustomizer.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.translate.v3po.vpp;
18
19 import com.google.common.base.Preconditions;
20 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
22 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.vpp.jvpp.core.dto.BdIpMacAddDel;
29 import io.fd.vpp.jvpp.core.dto.BdIpMacAddDelReply;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Writer Customizer responsible for ARP termination table management.<br> Sends {@code bd_ip_mac_add_del} message to
44  * VPP.<br> Equivalent of invoking {@code vppctl set bridge-domain arp term} command.
45  */
46 public class ArpTerminationTableEntryCustomizer extends FutureJVppCustomizer
47         implements ListWriterCustomizer<ArpTerminationTableEntry, ArpTerminationTableEntryKey>, ByteDataTranslator,
48         AddressTranslator, JvppReplyConsumer {
49
50     private static final Logger LOG = LoggerFactory.getLogger(ArpTerminationTableEntryCustomizer.class);
51
52     private final NamingContext bdContext;
53
54     public ArpTerminationTableEntryCustomizer(@Nonnull final FutureJVppCore futureJvpp,
55                                               @Nonnull final NamingContext bdContext) {
56         super(futureJvpp);
57         this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
58     }
59
60     @Override
61     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
62                                        @Nonnull final ArpTerminationTableEntry dataAfter,
63                                        @Nonnull final WriteContext writeContext)
64             throws WriteFailedException {
65         LOG.debug("Creating ARP termination table entry: {} {}", id, dataAfter);
66         bdIpMacAddDel(id, dataAfter, writeContext, true);
67         LOG.debug("L2 ARP termination table entry created successfully: {} {}", id, dataAfter);
68     }
69
70     @Override
71     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
72                                         @Nonnull final ArpTerminationTableEntry dataBefore,
73                                         @Nonnull final ArpTerminationTableEntry dataAfter,
74                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
75         throw new UnsupportedOperationException(
76                 "ARP termination table entry update is not supported. It has to be deleted and then created.");
77     }
78
79     @Override
80     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
81                                         @Nonnull final ArpTerminationTableEntry dataBefore,
82                                         @Nonnull final WriteContext writeContext)
83             throws WriteFailedException {
84         LOG.debug("Deleting ARP termination table entry entry: {} {}", id, dataBefore);
85         bdIpMacAddDel(id, dataBefore, writeContext, false);
86         LOG.debug("ARP termination table entry deleted successfully: {} {}", id, dataBefore);
87     }
88
89     private void bdIpMacAddDel(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
90                                @Nonnull final ArpTerminationTableEntry entry,
91                                final WriteContext writeContext, boolean isAdd) throws WriteFailedException {
92         final String bdName = id.firstKeyOf(BridgeDomain.class).getName();
93         final int bdId = bdContext.getIndex(bdName, writeContext.getMappingContext());
94
95         final BdIpMacAddDel request = createRequest(entry, bdId, isAdd);
96         LOG.debug("Sending l2FibAddDel request: {}", request);
97         final CompletionStage<BdIpMacAddDelReply> replyCompletionStage =
98                 getFutureJVpp().bdIpMacAddDel(request);
99
100         getReplyForWrite(replyCompletionStage.toCompletableFuture(), id);
101     }
102
103     private BdIpMacAddDel createRequest(final ArpTerminationTableEntry entry, final int bdId, boolean isAdd) {
104         final BdIpMacAddDel request = new BdIpMacAddDel();
105         request.bdId = bdId;
106         request.isAdd = booleanToByte(isAdd);
107         request.macAddress = parseMac(entry.getPhysAddress().getValue());
108
109         final IpAddress ipAddress = entry.getIpAddress();
110         if (ipAddress.getIpv6Address() != null) {
111             // FIXME: HONEYCOMB-187 vpp does not support ipv6 in arp-termination table (based on analysis of l2_bd.c)
112             throw new UnsupportedOperationException("IPv6 address for ARP termination table is not supported yet");
113         }
114
115         request.ipAddress = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(ipAddress.getIpv4Address()));
116         return request;
117     }
118 }