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 java.nio.charset.StandardCharsets.UTF_8;
23 import io.fd.honeycomb.lisp.translate.read.trait.LocatorSetReader;
24 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
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.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
32 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.List;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 * Customizer for {@link LocatorSet} entity.
47 public class LocatorSetCustomizer extends FutureJVppCustomizer
48 implements ListWriterCustomizer<LocatorSet, LocatorSetKey>, ByteDataTranslator,
51 private final NamingContext locatorSetContext;
52 private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
54 public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
55 @Nonnull final NamingContext locatorSetContext) {
57 this.locatorSetContext = checkNotNull(locatorSetContext, "Locator set context cannot be null");
58 this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorSetDetailsReplyDump, Void>()
59 .withExecutor(createExecutor(futureJvpp))
64 public void writeCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
65 @Nonnull LocatorSet dataAfter,
66 @Nonnull WriteContext writeContext) throws WriteFailedException {
67 checkState(isNonEmptyLocatorSet(writeContext.readAfter(id).get()),
68 "Creating empty locator-sets is not allowed");
69 final String locatorSetName = dataAfter.getName();
70 checkState(!locatorSetContext.containsIndex(locatorSetName, writeContext.getMappingContext()),
71 "Locator set with name %s already defined", locatorSetName);
73 final int locatorSetIndex = addDelLocatorSetAndReply(true, dataAfter.getName(), id);
74 locatorSetContext.addName(locatorSetIndex, locatorSetName, writeContext.getMappingContext());
77 private boolean isNonEmptyLocatorSet(final LocatorSet locatorSet) {
78 final List<Interface> locators = locatorSet.getInterface();
79 return locators != null && !locators.isEmpty();
83 public void updateCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
84 @Nonnull LocatorSet dataBefore,
85 @Nonnull LocatorSet dataAfter,
86 @Nonnull WriteContext writeContext) throws WriteFailedException {
87 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
88 new UnsupportedOperationException("Operation not supported"));
92 public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
93 @Nonnull LocatorSet dataBefore,
94 @Nonnull WriteContext writeContext) throws WriteFailedException {
95 final String locatorSetName = dataBefore.getName();
96 addDelLocatorSetAndReply(false, dataBefore.getName(), id);
97 //removes mapping after successful delete
98 locatorSetContext.removeName(locatorSetName, writeContext.getMappingContext());
101 private int addDelLocatorSetAndReply(final boolean add, final String name, final InstanceIdentifier<LocatorSet> id)
102 throws WriteFailedException {
104 LispAddDelLocatorSet addDelSet = new LispAddDelLocatorSet();
106 addDelSet.isAdd = booleanToByte(add);
107 addDelSet.locatorSetName = name.getBytes(UTF_8);
109 return getReplyForWrite(getFutureJVpp().lispAddDelLocatorSet(addDelSet).toCompletableFuture(), id).lsIndex;