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;
53 public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
54 @Nonnull final NamingContext locatorSetContext) {
56 this.locatorSetContext = checkNotNull(locatorSetContext, "Locator set context cannot be null");
60 public void writeCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
61 @Nonnull LocatorSet dataAfter,
62 @Nonnull WriteContext writeContext) throws WriteFailedException {
63 checkState(isNonEmptyLocatorSet(writeContext.readAfter(id).get()),
64 "Creating empty locator-sets is not allowed");
65 final String locatorSetName = dataAfter.getName();
66 checkState(!locatorSetContext.containsIndex(locatorSetName, writeContext.getMappingContext()),
67 "Locator set with name %s already defined", locatorSetName);
69 final int locatorSetIndex = addDelLocatorSetAndReply(true, dataAfter.getName(), id);
70 locatorSetContext.addName(locatorSetIndex, locatorSetName, writeContext.getMappingContext());
73 private boolean isNonEmptyLocatorSet(final LocatorSet locatorSet) {
74 final List<Interface> locators = locatorSet.getInterface();
75 return locators != null && !locators.isEmpty();
79 public void updateCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
80 @Nonnull LocatorSet dataBefore,
81 @Nonnull LocatorSet dataAfter,
82 @Nonnull WriteContext writeContext) throws WriteFailedException {
83 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
84 new UnsupportedOperationException("Operation not supported"));
88 public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<LocatorSet> id,
89 @Nonnull LocatorSet dataBefore,
90 @Nonnull WriteContext writeContext) throws WriteFailedException {
91 final String locatorSetName = dataBefore.getName();
92 addDelLocatorSetAndReply(false, dataBefore.getName(), id);
93 //removes mapping after successful delete
94 locatorSetContext.removeName(locatorSetName, writeContext.getMappingContext());
97 private int addDelLocatorSetAndReply(final boolean add, final String name, final InstanceIdentifier<LocatorSet> id)
98 throws WriteFailedException {
100 LispAddDelLocatorSet addDelSet = new LispAddDelLocatorSet();
102 addDelSet.isAdd = booleanToByte(add);
103 addDelSet.locatorSetName = name.getBytes(UTF_8);
105 return getReplyForWrite(getFutureJVpp().lispAddDelLocatorSet(addDelSet).toCompletableFuture(), id).lsIndex;