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.lisp.translate.write;
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.base.Preconditions.checkState;
21 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV4;
22 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV6;
23 import static java.nio.charset.StandardCharsets.UTF_8;
25 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
26 import io.fd.honeycomb.lisp.translate.read.trait.MappingProducer;
27 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
28 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
31 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
32 import io.fd.honeycomb.translate.write.WriteContext;
33 import io.fd.honeycomb.translate.write.WriteFailedException;
34 import java.io.UnsupportedEncodingException;
35 import java.util.concurrent.TimeoutException;
36 import javax.annotation.Nonnull;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.LocalMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.LocalMappingKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import io.fd.vpp.jvpp.VppBaseCallException;
43 import io.fd.vpp.jvpp.core.dto.LispAddDelLocalEid;
44 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
48 * Customizer that writes changes for {@link LocalMapping}
50 public class LocalMappingCustomizer extends FutureJVppCustomizer
51 implements ListWriterCustomizer<LocalMapping, LocalMappingKey>, ByteDataTranslator, EidTranslator,
52 JvppReplyConsumer, MappingProducer {
54 private final EidMappingContext localMappingsContext;
56 public LocalMappingCustomizer(@Nonnull FutureJVppCore futureJvpp, @Nonnull EidMappingContext localMappingsContext) {
58 this.localMappingsContext = checkNotNull(localMappingsContext, "No local mappings context defined");
62 public void writeCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataAfter,
63 WriteContext writeContext) throws WriteFailedException {
64 checkNotNull(dataAfter, "Mapping is null");
65 checkNotNull(dataAfter.getEid(), "Eid is null");
66 checkNotNull(dataAfter.getLocatorSet(), "Locator set is null");
67 checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
68 checkAllowedCombination(id, dataAfter);
70 //checks whether value with specified mapping-id does not exist in mapping allready
71 final MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
72 checkState(!localMappingsContext
73 .containsEid(mappingId, writeContext.getMappingContext()),
74 "Local mapping with id %s already defined", id);
78 addDelMappingAndReply(true, dataAfter,
79 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
80 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
81 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
84 //adds mapping for id and eid
85 localMappingsContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
89 public void updateCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
90 LocalMapping dataAfter, WriteContext writeContext)
91 throws WriteFailedException {
92 throw new UnsupportedOperationException("Operation not supported");
96 public void deleteCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
97 WriteContext writeContext) throws WriteFailedException {
98 checkNotNull(dataBefore, "Mapping is null");
99 checkNotNull(dataBefore.getEid(), "Eid is null");
100 checkNotNull(dataBefore.getLocatorSet(), "LocatorSet is null");
101 checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
103 //checks whether value with specified mapping-id does exist in mapping,so there is something to delete
104 MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
105 checkState(localMappingsContext
106 .containsEid(mappingId, writeContext.getMappingContext()),
107 "Local mapping with id %s not present in mapping", id);
110 addDelMappingAndReply(false, dataBefore,
111 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
112 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
113 throw new WriteFailedException.DeleteFailedException(id, e);
116 //removes value also from mapping
117 localMappingsContext.removeEid(mappingId, writeContext.getMappingContext());
120 private void addDelMappingAndReply(boolean add, LocalMapping data, int vni) throws VppBaseCallException,
121 TimeoutException, UnsupportedEncodingException {
123 LispAddDelLocalEid request = new LispAddDelLocalEid();
125 request.isAdd = booleanToByte(add);
126 request.eid = getEidAsByteArray(data.getEid());
127 request.eidType = (byte) getEidType(data.getEid()).getValue();
128 request.locatorSetName = data.getLocatorSet().getBytes(UTF_8);
132 if (request.eidType == IPV4.getValue()) {
133 request.prefixLen = 32;
134 } else if (request.eidType == IPV6.getValue()) {
135 request.prefixLen = (byte) 128;
138 getReply(getFutureJVpp().lispAddDelLocalEid(request).toCompletableFuture());