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