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