15ffc66049ca2e256ca029429fd18e20c64f5162
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / InterfaceCustomizer.java
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.lisp.translate.write;
18
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;
22
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.rev160520.locator.sets.grouping.locator.sets.LocatorSet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.locator.set.Interface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.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;
40
41
42 /**
43  * Customizer for updating {@link Interface}
44  *
45  * @see Interface
46  */
47 public class InterfaceCustomizer extends FutureJVppCustomizer
48         implements ListWriterCustomizer<Interface, InterfaceKey>, ByteDataTranslator, JvppReplyConsumer {
49
50     private final NamingContext interfaceContext;
51
52     public InterfaceCustomizer(@Nonnull FutureJVppCore futureJvpp, @Nonnull NamingContext interfaceContext) {
53         super(futureJvpp);
54         this.interfaceContext = checkNotNull(interfaceContext, "Naming context is null");
55     }
56
57     @Override
58     public void writeCurrentAttributes(InstanceIdentifier<Interface> id, Interface dataAfter, WriteContext writeContext)
59             throws WriteFailedException {
60
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");
66
67         String interfaceName = id.firstKeyOf(Interface.class).getInterfaceRef();
68         String locatorSetName = id.firstKeyOf(LocatorSet.class).getName();
69
70         checkState(interfaceContext.containsIndex(interfaceName, writeContext.getMappingContext()),
71                 "No mapping stored for interface %s", interfaceName);
72
73         try {
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);
78         }
79
80     }
81
82     @Override
83     public void updateCurrentAttributes(InstanceIdentifier<Interface> id, Interface dataBefore, Interface dataAfter,
84                                         WriteContext writeContext) throws WriteFailedException {
85         throw new UnsupportedOperationException("Operation not supported");
86     }
87
88     @Override
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");
96
97         String interfaceName = id.firstKeyOf(Interface.class).getInterfaceRef();
98         String locatorSetName = id.firstKeyOf(LocatorSet.class).getName();
99
100         checkState(interfaceContext.containsIndex(interfaceName, writeContext.getMappingContext()),
101                 "No mapping stored for interface %s", interfaceName);
102         try {
103             addDelInterfaceAndReply(false, dataBefore,
104                     interfaceContext.getIndex(interfaceName, writeContext.getMappingContext()), locatorSetName);
105         } catch (VppBaseCallException | TimeoutException | UnsupportedEncodingException e) {
106             throw new WriteFailedException.DeleteFailedException(id, e);
107         }
108     }
109
110     private void addDelInterfaceAndReply(boolean add, Interface data, int interfaceIndex, String locatorSetName)
111             throws VppBaseCallException, TimeoutException, UnsupportedEncodingException {
112         LispAddDelLocator request = new LispAddDelLocator();
113
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);
119
120         getReply(getFutureJVpp().lispAddDelLocator(request).toCompletableFuture());
121     }
122 }