b866842c1076c554b8e8583973450d0997e9addc
[hc2vpp.git] /
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.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.translate.MappingContext;
30 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.openvpp.jvpp.VppBaseCallException;
38 import org.openvpp.jvpp.core.dto.IpNeighborAddDel;
39 import org.openvpp.jvpp.core.future.FutureJVppCore;
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> {
49
50
51     private static final Logger LOG = LoggerFactory.getLogger(Ipv4NeighbourCustomizer.class);
52     final NamingContext interfaceContext;
53
54     public Ipv4NeighbourCustomizer(final FutureJVppCore futureJVppCore, final NamingContext interfaceContext) {
55         super(futureJVppCore);
56         this.interfaceContext = interfaceContext;
57     }
58
59     @Override
60     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataAfter,
61                                        @Nonnull WriteContext writeContext)
62             throws WriteFailedException {
63
64         checkNotNull(dataAfter, "Cannot write null neighbour");
65         checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
66
67         LOG.debug("Processing request for Neigbour write");
68         String interfaceName = id.firstKeyOf(Interface.class).getName();
69         MappingContext mappingContext = writeContext.getMappingContext();
70
71         checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
72                 "Mapping does not contains mapping for provider interface name ".concat(interfaceName));
73
74         LOG.debug("Parent interface index found");
75         try {
76             addDelNeighbourAndReply(id, true,
77                     interfaceContext.getIndex(interfaceName, mappingContext), dataAfter);
78             LOG.info("Neighbour successfully written");
79         } catch (VppBaseCallException e) {
80             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
81         }
82     }
83
84     @Override
85     public void updateCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataBefore,
86                                         @Nonnull Neighbor dataAfter,
87                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
88         throw new UnsupportedOperationException("Operation not supported");
89     }
90
91     @Override
92     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<Neighbor> id, @Nonnull Neighbor dataBefore,
93                                         @Nonnull WriteContext writeContext)
94             throws WriteFailedException {
95
96         checkNotNull(dataBefore, "Cannot delete null neighbour");
97         checkArgument(id.firstKeyOf(Interface.class) != null, "No parent interface key found");
98
99         LOG.debug("Processing request for Neigbour delete");
100         String interfaceName = id.firstKeyOf(Interface.class).getName();
101         MappingContext mappingContext = writeContext.getMappingContext();
102
103         checkState(interfaceContext.containsIndex(interfaceName, mappingContext),
104                 "Mapping does not contains mapping for provider interface name %s", interfaceName);
105
106         LOG.debug("Parent interface[{}] index found", interfaceName);
107         try {
108             addDelNeighbourAndReply(id, false,
109                     interfaceContext.getIndex(interfaceName, mappingContext), dataBefore);
110             LOG.info("Neighbour {} successfully deleted", id);
111         } catch (VppBaseCallException e) {
112             throw new WriteFailedException.DeleteFailedException(id, e);
113         }
114     }
115
116     private void addDelNeighbourAndReply(InstanceIdentifier<Neighbor> id, boolean add, int parentInterfaceIndex,
117                                          Neighbor data)
118             throws VppBaseCallException, WriteTimeoutException {
119
120         IpNeighborAddDel request = new IpNeighborAddDel();
121
122         request.isAdd = TranslateUtils.booleanToByte(add);
123         request.isIpv6 = 0;
124         request.isStatic = 1;
125         request.dstAddress = TranslateUtils.ipv4AddressNoZoneToArray(data.getIp());
126         request.macAddress = TranslateUtils.parseMac(data.getLinkLayerAddress().getValue());
127         request.swIfIndex = parentInterfaceIndex;
128
129         //TODO if it is necessary for future use ,make adjustments to be able to set vrfid
130         //request.vrfId
131         TranslateUtils.getReplyForWrite(getFutureJVpp().ipNeighborAddDel(request).toCompletableFuture(), id);
132     }
133 }