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 java.nio.charset.StandardCharsets.UTF_8;
23 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
25 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.translate.write.WriteContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import java.io.UnsupportedEncodingException;
31 import java.util.concurrent.TimeoutException;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import io.fd.vpp.jvpp.VppBaseCallException;
38 import io.fd.vpp.jvpp.core.dto.LispAddDelLocator;
39 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
43 * Customizer for updating {@link Interface}
47 public class InterfaceCustomizer extends FutureJVppCustomizer
48 implements ListWriterCustomizer<Interface, InterfaceKey>, ByteDataTranslator, JvppReplyConsumer {
50 private final NamingContext interfaceContext;
52 public InterfaceCustomizer(@Nonnull FutureJVppCore futureJvpp, @Nonnull NamingContext interfaceContext) {
54 this.interfaceContext = checkNotNull(interfaceContext, "Naming context is null");
58 public void writeCurrentAttributes(InstanceIdentifier<Interface> id, Interface dataAfter, WriteContext writeContext)
59 throws WriteFailedException {
61 checkNotNull(dataAfter, "Interface is null");
62 checkNotNull(dataAfter.getPriority(), "Priority is null");
63 checkNotNull(dataAfter.getWeight(), "Weight is null");
64 checkState(id.firstKeyOf(Interface.class) != null, "Parent interface not found");
65 checkState(id.firstKeyOf(LocatorSet.class) != null, "Parent locator set not found");
67 String interfaceName = id.firstKeyOf(Interface.class).getInterfaceRef();
68 String locatorSetName = id.firstKeyOf(LocatorSet.class).getName();
70 checkState(interfaceContext.containsIndex(interfaceName, writeContext.getMappingContext()),
71 "No mapping stored for interface %s", interfaceName);
74 addDelInterfaceAndReply(true, dataAfter,
75 interfaceContext.getIndex(interfaceName, writeContext.getMappingContext()), locatorSetName);
76 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
77 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
83 public void updateCurrentAttributes(InstanceIdentifier<Interface> id, Interface dataBefore, Interface dataAfter,
84 WriteContext writeContext) throws WriteFailedException {
85 throw new UnsupportedOperationException("Operation not supported");
89 public void deleteCurrentAttributes(InstanceIdentifier<Interface> id, Interface dataBefore,
90 WriteContext writeContext) throws WriteFailedException {
91 checkNotNull(dataBefore, "Interface is null");
92 checkNotNull(dataBefore.getPriority(), "Priority is null");
93 checkNotNull(dataBefore.getWeight(), "Weight is null");
94 checkState(id.firstKeyOf(Interface.class) != null, "Parent interface not found");
95 checkState(id.firstKeyOf(LocatorSet.class) != null, "Parent locator set not found");
97 String interfaceName = id.firstKeyOf(Interface.class).getInterfaceRef();
98 String locatorSetName = id.firstKeyOf(LocatorSet.class).getName();
100 checkState(interfaceContext.containsIndex(interfaceName, writeContext.getMappingContext()),
101 "No mapping stored for interface %s", interfaceName);
103 addDelInterfaceAndReply(false, dataBefore,
104 interfaceContext.getIndex(interfaceName, writeContext.getMappingContext()), locatorSetName);
105 } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
106 throw new WriteFailedException.DeleteFailedException(id, e);
110 private void addDelInterfaceAndReply(boolean add, Interface data, int interfaceIndex, String locatorSetName)
111 throws VppBaseCallException, TimeoutException, UnsupportedEncodingException {
112 LispAddDelLocator request = new LispAddDelLocator();
114 request.isAdd = booleanToByte(add);
115 request.priority = data.getPriority().byteValue();
116 request.weight = data.getWeight().byteValue();
117 request.swIfIndex = interfaceIndex;
118 request.locatorSetName = locatorSetName.getBytes(UTF_8);
120 getReply(getFutureJVpp().lispAddDelLocator(request).toCompletableFuture());