HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / ip / Ipv4NeighbourCustomizer.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.interfaces.ip;
18
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;
22
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;
42
43
44 /**
45  * Customizer for writing {@link Neighbor} for {@link Ipv4}.
46  */
47 public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer
48         implements ListWriterCustomizer<Neighbor, NeighborKey>, ByteDataTranslator, AddressTranslator,
49         JvppReplyConsumer {
50
51
52     private static final Logger LOG = LoggerFactory.getLogger(Ipv4NeighbourCustomizer.class);
53     final NamingContext interfaceContext;
54
55     public Ipv4NeighbourCustomizer(final FutureJVppCore futureJVppCore, final NamingContext interfaceContext) {
56         super(futureJVppCore);
57         this.interfaceContext = interfaceContext;
58     }
59
60     @Override
61     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataAfter,
62                                        @Nonnull WriteContext writeContext)
63             throws WriteFailedException {
64
65         checkNotNull(dataAfter, "Cannot write null neighbour");
66         checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
67
68         LOG.debug("Processing request for Neigbour write");
69         String interfaceName = id.firstKeyOf(Interface.class).getName();
70         MappingContext mappingContext = writeContext.getMappingContext();
71
72         checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
73                 "Mapping does not contains mapping for provider interface name ".concat(interfaceName));
74
75         LOG.debug("Parent interface index found");
76         addDelNeighbourAndReply(id, true,
77                 interfaceContext.getIndex(interfaceName, mappingContext), dataAfter);
78         LOG.info("Neighbour successfully written");
79     }
80
81     @Override
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");
86     }
87
88     @Override
89     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataBefore,
90                                         @Nonnull WriteContext writeContext)
91             throws WriteFailedException {
92
93         checkNotNull(dataBefore, "Cannot delete null neighbour");
94         checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
95
96         LOG.debug("Processing request for Neigbour delete");
97         String interfaceName = id.firstKeyOf(Interface.class).getName();
98         MappingContext mappingContext = writeContext.getMappingContext();
99
100         checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
101                 "Mapping does not contains mapping for provider interface name %s", interfaceName);
102
103         LOG.debug("Parent interface[{}] index found", interfaceName);
104
105         addDelNeighbourAndReply(id, false,
106                 interfaceContext.getIndex(interfaceName, mappingContext), dataBefore);
107         LOG.info("Neighbour {} successfully deleted", id);
108     }
109
110     private void addDelNeighbourAndReply(InstanceIdentifier<Neighbor> id, boolean add, int parentInterfaceIndex,
111                                          Neighbor data) throws WriteFailedException {
112
113         IpNeighborAddDel request = new IpNeighborAddDel();
114
115         request.isAdd = booleanToByte(add);
116         request.isIpv6 = 0;
117         request.isStatic = 1;
118         request.dstAddress = ipv4AddressNoZoneToArray(data.getIp());
119         request.macAddress = parseMac(data.getLinkLayerAddress().getValue());
120         request.swIfIndex = parentInterfaceIndex;
121
122         //TODO HONEYCOMB-182 if it is necessary for future use ,make adjustments to be able to set vrfid
123         //request.vrfId
124         getReplyForWrite(getFutureJVpp().ipNeighborAddDel(request).toCompletableFuture(), id);
125     }
126 }