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.translate.v3po.interfaces.ip;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
26 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
27 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.translate.write.WriteContext;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import io.fd.vpp.jvpp.core.dto.IpNeighborAddDel;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 * Customizer for writing {@link Neighbor} for {@link Ipv4}.
47 public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer
48 implements ListWriterCustomizer<Neighbor, NeighborKey>, ByteDataTranslator, AddressTranslator,
52 private static final Logger LOG = LoggerFactory.getLogger(Ipv4NeighbourCustomizer.class);
53 final NamingContext interfaceContext;
55 public Ipv4NeighbourCustomizer(final FutureJVppCore futureJVppCore, final NamingContext interfaceContext) {
56 super(futureJVppCore);
57 this.interfaceContext = interfaceContext;
61 public void writeCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataAfter,
62 @Nonnull WriteContext writeContext)
63 throws WriteFailedException {
65 checkNotNull(dataAfter, "Cannot write null neighbour");
66 checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
68 LOG.debug("Processing request for Neigbour write");
69 String interfaceName = id.firstKeyOf(Interface.class).getName();
70 MappingContext mappingContext = writeContext.getMappingContext();
72 checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
73 "Mapping does not contains mapping for provider interface name ".concat(interfaceName));
75 LOG.debug("Parent interface index found");
76 addDelNeighbourAndReply(id, true,
77 interfaceContext.getIndex(interfaceName, mappingContext), dataAfter);
78 LOG.info("Neighbour successfully written");
82 public void updateCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataBefore,
83 @Nonnull Neighbor dataAfter,
84 @Nonnull WriteContext writeContext) throws WriteFailedException {
85 throw new UnsupportedOperationException("Operation not supported");
89 public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataBefore,
90 @Nonnull WriteContext writeContext)
91 throws WriteFailedException {
93 checkNotNull(dataBefore, "Cannot delete null neighbour");
94 checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
96 LOG.debug("Processing request for Neigbour delete");
97 String interfaceName = id.firstKeyOf(Interface.class).getName();
98 MappingContext mappingContext = writeContext.getMappingContext();
100 checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
101 "Mapping does not contains mapping for provider interface name %s", interfaceName);
103 LOG.debug("Parent interface[{}] index found", interfaceName);
105 addDelNeighbourAndReply(id, false,
106 interfaceContext.getIndex(interfaceName, mappingContext), dataBefore);
107 LOG.info("Neighbour {} successfully deleted", id);
110 private void addDelNeighbourAndReply(InstanceIdentifier<Neighbor> id, boolean add, int parentInterfaceIndex,
111 Neighbor data) throws WriteFailedException {
113 IpNeighborAddDel request = new IpNeighborAddDel();
115 request.isAdd = booleanToByte(add);
117 request.isStatic = 1;
118 request.dstAddress = ipv4AddressNoZoneToArray(data.getIp());
119 request.macAddress = parseMac(data.getLinkLayerAddress().getValue());
120 request.swIfIndex = parentInterfaceIndex;
122 //TODO HONEYCOMB-182 if it is necessary for future use ,make adjustments to be able to set vrfid
124 getReplyForWrite(getFutureJVpp().ipNeighborAddDel(request).toCompletableFuture(), id);