2 * Copyright (c) 2015 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.util.EidTranslator;
27 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
28 import io.fd.honeycomb.translate.v3po.util.ByteDataTranslator;
29 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
30 import io.fd.honeycomb.translate.v3po.util.JvppReplyConsumer;
31 import io.fd.honeycomb.translate.write.WriteContext;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import java.io.UnsupportedEncodingException;
34 import java.util.concurrent.TimeoutException;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MappingId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.LocalMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.LocalMappingKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.openvpp.jvpp.VppBaseCallException;
42 import org.openvpp.jvpp.core.dto.LispAddDelLocalEid;
43 import org.openvpp.jvpp.core.future.FutureJVppCore;
47 * Customizer that writes changes for {@link LocalMapping}
49 public class LocalMappingCustomizer extends FutureJVppCustomizer
50 implements ListWriterCustomizer<LocalMapping, LocalMappingKey>, ByteDataTranslator, EidTranslator,
53 private final EidMappingContext localMappingsContext;
55 public LocalMappingCustomizer(@Nonnull FutureJVppCore futureJvpp, @Nonnull EidMappingContext localMappingsContext) {
57 this.localMappingsContext = checkNotNull(localMappingsContext, "No local mappings context defined");
61 public void writeCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataAfter,
62 WriteContext writeContext) throws WriteFailedException {
63 checkNotNull(dataAfter, "Mapping is null");
64 checkNotNull(dataAfter.getEid(), "Eid is null");
65 checkNotNull(dataAfter.getLocatorSet(), "Locator set is null");
66 checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
68 //checks whether value with specified mapping-id does not exist in mapping allready
69 MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
70 checkState(!localMappingsContext
71 .containsEid(mappingId, writeContext.getMappingContext()),
72 "Local mapping with id %s already defined", id);
76 addDelMappingAndReply(true, dataAfter,
77 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
78 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
79 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
82 //adds mapping for id and eid
83 localMappingsContext.addEid(mappingId, dataAfter.getEid(), writeContext.getMappingContext());
87 public void updateCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
88 LocalMapping dataAfter, WriteContext writeContext)
89 throws WriteFailedException {
90 throw new UnsupportedOperationException("Operation not supported");
94 public void deleteCurrentAttributes(InstanceIdentifier<LocalMapping> id, LocalMapping dataBefore,
95 WriteContext writeContext) throws WriteFailedException {
96 checkNotNull(dataBefore, "Mapping is null");
97 checkNotNull(dataBefore.getEid(), "Eid is null");
98 checkNotNull(dataBefore.getLocatorSet(), "LocatorSet is null");
99 checkState(id.firstKeyOf(VniTable.class) != null, "Parent vni table not found");
101 //checks whether value with specified mapping-id does exist in mapping,so there is something to delete
102 MappingId mappingId = id.firstKeyOf(LocalMapping.class).getId();
103 checkState(localMappingsContext
104 .containsEid(mappingId, writeContext.getMappingContext()),
105 "Local mapping with id %s not present in mapping", id);
108 addDelMappingAndReply(false, dataBefore,
109 id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier().intValue());
110 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
111 throw new WriteFailedException.DeleteFailedException(id, e);
114 //removes value also from mapping
115 localMappingsContext.removeEid(mappingId, writeContext.getMappingContext());
118 private void addDelMappingAndReply(boolean add, LocalMapping data, int vni) throws VppBaseCallException,
119 TimeoutException, UnsupportedEncodingException {
121 LispAddDelLocalEid request = new LispAddDelLocalEid();
123 request.isAdd = booleanToByte(add);
124 request.eid = getEidAsByteArray(data.getEid());
125 request.eidType = (byte) getEidType(data.getEid()).getValue();
126 request.locatorSetName = data.getLocatorSet().getBytes(UTF_8);
130 if (request.eidType == IPV4.getValue()) {
131 request.prefixLen = 32;
132 } else if (request.eidType == IPV6.getValue()) {
133 request.prefixLen = (byte) 128;
136 getReply(getFutureJVpp().lispAddDelLocalEid(request).toCompletableFuture());