VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / InterfaceCustomizer.java
1 /*
2  * Copyright (c) 2016 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.translate.v3po.interfaces;
18
19 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
20 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
22 import io.fd.honeycomb.translate.vpp.util.NamingContext;
23 import io.fd.honeycomb.translate.vpp.util.WriteTimeoutException;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import java.util.concurrent.CompletionStage;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
33 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Ietf interface write customizer that only caches interface objects for child writers
40  */
41 public class InterfaceCustomizer extends FutureJVppCustomizer
42         implements ListWriterCustomizer<Interface, InterfaceKey>, JvppReplyConsumer {
43
44     private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class);
45     private final NamingContext interfaceContext;
46
47     public InterfaceCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
48         super(vppApi);
49         this.interfaceContext = interfaceContext;
50     }
51
52     @Override
53     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
54                                        @Nonnull final Interface dataAfter,
55                                        @Nonnull final WriteContext writeContext)
56             throws WriteFailedException {
57
58         try {
59             setInterface(id, dataAfter, writeContext);
60         } catch (VppBaseCallException e) {
61             LOG.warn("Update of VppInterfaceAugment failed", e);
62             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
63         }
64     }
65
66     @Override
67     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
68                                         @Nonnull final Interface dataBefore,
69                                         @Nonnull final Interface dataAfter,
70                                         @Nonnull final WriteContext writeContext)
71             throws WriteFailedException {
72
73         try {
74             updateInterface(id, dataBefore, dataAfter, writeContext);
75         } catch (VppBaseCallException e) {
76             LOG.warn("Update of VppInterfaceAugment failed", e);
77             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
78         }
79     }
80
81     @Override
82     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
83                                         @Nonnull final Interface dataBefore,
84                                         @Nonnull final WriteContext writeContext) {
85         // Nothing to be done here, customizers for specific interface types e.g. vxlan handle the delete
86     }
87
88     private void setInterface(final InstanceIdentifier<Interface> id, final Interface swIf,
89                               final WriteContext writeContext)
90             throws VppBaseCallException, WriteTimeoutException {
91         LOG.debug("Setting interface: {} to: {}", id, swIf);
92         setInterfaceAttributes(id, swIf, swIf.getName(), writeContext);
93     }
94
95     private void setInterfaceAttributes(final InstanceIdentifier<Interface> id, final Interface swIf,
96                                         final String swIfName, final WriteContext writeContext)
97             throws VppBaseCallException, WriteTimeoutException {
98
99         setInterfaceFlags(id, swIfName, interfaceContext.getIndex(swIfName, writeContext.getMappingContext()),
100                 swIf.isEnabled()
101                         ? (byte) 1
102                         : (byte) 0);
103     }
104
105     private void updateInterface(final InstanceIdentifier<Interface> id,
106                                  final Interface dataBefore,
107                                  final Interface dataAfter, final WriteContext writeContext)
108             throws VppBaseCallException, WriteTimeoutException {
109         LOG.debug("Updating interface:{} to: {}", id, dataAfter);
110         setInterfaceAttributes(id, dataAfter, dataAfter.getName(), writeContext);
111     }
112
113     private void setInterfaceFlags(final InstanceIdentifier<Interface> id, final String swIfName, final int swIfIndex,
114                                    final byte enabled)
115             throws VppBaseCallException, WriteTimeoutException {
116         final CompletionStage<SwInterfaceSetFlagsReply> swInterfaceSetFlagsReplyFuture =
117                 getFutureJVpp().swInterfaceSetFlags(
118                         getSwInterfaceSetFlagsInput(swIfIndex, enabled, (byte) 0 /* deleted */));
119
120         LOG.debug("Updating interface flags for: {}, index: {}, enabled: {}", swIfName, swIfIndex, enabled);
121
122         getReplyForWrite(swInterfaceSetFlagsReplyFuture.toCompletableFuture(), id);
123         LOG.debug("Interface flags updated successfully for: {}, index: {}, enabled: {}",
124                 swIfName, swIfIndex, enabled);
125     }
126
127     private SwInterfaceSetFlags getSwInterfaceSetFlagsInput(final int swIfIndex, final byte enabled,
128                                                             final byte deleted) {
129         final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags();
130         swInterfaceSetFlags.swIfIndex = swIfIndex;
131         swInterfaceSetFlags.adminUpDown = enabled;
132         swInterfaceSetFlags.linkUpDown = enabled;
133         swInterfaceSetFlags.deleted = deleted;
134         return swInterfaceSetFlags;
135     }
136 }