HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / RoutingCustomizer.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.WriterCustomizer;
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.SwInterfaceSetTable;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetTableReply;
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.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Routing;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class RoutingCustomizer extends FutureJVppCustomizer implements WriterCustomizer<Routing>, JvppReplyConsumer {
37
38     private static final Logger LOG = LoggerFactory.getLogger(RoutingCustomizer.class);
39     private final NamingContext interfaceContext;
40
41     public RoutingCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
42         super(vppApi);
43         this.interfaceContext = interfaceContext;
44     }
45
46     @Override
47     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
48                                        @Nonnull final Routing dataAfter, @Nonnull final WriteContext writeContext)
49             throws WriteFailedException {
50
51         final String ifName = id.firstKeyOf(Interface.class).getName();
52         setRouting(id, ifName, dataAfter, writeContext);
53     }
54
55     @Override
56     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
57                                         @Nonnull final Routing dataBefore, @Nonnull final Routing dataAfter,
58                                         @Nonnull final WriteContext writeContext)
59             throws WriteFailedException {
60
61         final String ifName = id.firstKeyOf(Interface.class).getName();
62         setRouting(id, ifName, dataAfter, writeContext);
63     }
64
65     @Override
66     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
67                                         @Nonnull final Routing dataBefore, @Nonnull final WriteContext writeContext)
68             throws WriteFailedException {
69         final String ifName = id.firstKeyOf(Interface.class).getName();
70         disableRouting(id, ifName, writeContext);
71     }
72
73     private void setRouting(final InstanceIdentifier<Routing> id, final String name, final Routing rt,
74                             final WriteContext writeContext) throws WriteFailedException {
75         final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
76         LOG.debug("Setting routing for interface: {}, {}. Routing: {}", name, swIfc, rt);
77
78         int vrfId = (rt != null)
79                 ? rt.getVrfId().intValue()
80                 : 0;
81
82         if (vrfId != 0) {
83             final CompletionStage<SwInterfaceSetTableReply> swInterfaceSetTableReplyCompletionStage =
84                     getFutureJVpp()
85                             .swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, (byte) 0, /* isIpv6 */ vrfId));
86             getReplyForWrite(swInterfaceSetTableReplyCompletionStage.toCompletableFuture(), id);
87             LOG.debug("Routing set successfully for interface: {}, {}, routing: {}", name, swIfc, rt);
88         }
89     }
90
91     /**
92      * In this case, there is no such thing as delete routing,only thing that can be done is to disable it by setting
93      * default value 0
94      */
95     private void disableRouting(final InstanceIdentifier<Routing> id, final String name,
96                                 final WriteContext writeContext) throws WriteFailedException {
97         final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
98         LOG.debug("Disabling routing for interface: {}, {}.", name, swIfc);
99
100         getReplyForDelete(getFutureJVpp()
101                 .swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, (byte) 0, 0)).toCompletableFuture(), id);
102         LOG.debug("Routing for interface: {}, {} successfully disabled", name, swIfc);
103
104     }
105
106     private SwInterfaceSetTable getInterfaceSetTableRequest(final int swIfc, final byte isIpv6, final int vrfId) {
107         final SwInterfaceSetTable swInterfaceSetTable = new SwInterfaceSetTable();
108         swInterfaceSetTable.isIpv6 = isIpv6;
109         swInterfaceSetTable.swIfIndex = swIfc;
110         swInterfaceSetTable.vrfId = vrfId;
111         return swInterfaceSetTable;
112     }
113 }