HONEYCOMB-58 - Routing Api
[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 static com.google.common.base.Preconditions.checkArgument;
20
21 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetTable;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetTableReply;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Routing;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class RoutingCustomizer extends FutureJVppCustomizer implements WriterCustomizer<Routing>, JvppReplyConsumer,
40     ByteDataTranslator {
41
42     private static final Logger LOG = LoggerFactory.getLogger(RoutingCustomizer.class);
43     private final NamingContext interfaceContext;
44
45     public RoutingCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
46         super(vppApi);
47         this.interfaceContext = interfaceContext;
48     }
49
50     @Override
51     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
52                                        @Nonnull final Routing dataAfter, @Nonnull final WriteContext writeContext)
53             throws WriteFailedException {
54
55         final String ifName = id.firstKeyOf(Interface.class).getName();
56         setRouting(id, ifName, dataAfter, writeContext);
57     }
58
59     @Override
60     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
61                                         @Nonnull final Routing dataBefore, @Nonnull final Routing dataAfter,
62                                         @Nonnull final WriteContext writeContext)
63             throws WriteFailedException {
64
65         final String ifName = id.firstKeyOf(Interface.class).getName();
66         setRouting(id, ifName, dataAfter, writeContext);
67     }
68
69     @Override
70     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
71                                         @Nonnull final Routing dataBefore, @Nonnull final WriteContext writeContext)
72             throws WriteFailedException {
73         final String ifName = id.firstKeyOf(Interface.class).getName();
74         disableRouting(id, ifName, writeContext);
75     }
76
77     private void setRouting(@Nonnull final InstanceIdentifier<Routing> id, @Nonnull final String name,
78                             @Nonnull final Routing rt,
79                             @Nonnull final WriteContext writeContext) throws WriteFailedException {
80         final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
81         LOG.debug("Setting routing for interface: {}, {}. Routing: {}", name, swIfc, rt);
82         checkArgument(rt.getIpv4VrfId() != null || rt.getIpv6VrfId() != null, "No vrf-id given");
83
84         setVrfId(id, swIfc, rt.getIpv4VrfId(), false);
85         setVrfId(id, swIfc, rt.getIpv6VrfId(), true);
86
87         LOG.debug("Routing set successfully for interface: {}, {}, routing: {}", name, swIfc, rt);
88     }
89
90     private void setVrfId(final InstanceIdentifier<Routing> id, final int swIfc, final Long vrfId, boolean isIp6)
91         throws WriteFailedException {
92         if (vrfId == null) {
93             return;
94         }
95         final CompletionStage<SwInterfaceSetTableReply> cs = getFutureJVpp()
96             .swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, booleanToByte(isIp6), vrfId.intValue()));
97         getReplyForWrite(cs.toCompletableFuture(), id);
98     }
99
100     /**
101      * In this case, there is no such thing as delete routing,only thing that can be done is to disable it by setting
102      * default value 0
103      */
104     private void disableRouting(final InstanceIdentifier<Routing> id, final String name,
105                                 final WriteContext writeContext) throws WriteFailedException {
106         final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
107         LOG.debug("Disabling routing for interface: {}, {}.", name, swIfc);
108
109         getReplyForDelete(getFutureJVpp()
110             .swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, (byte) 0, 0)).toCompletableFuture(), id);
111         LOG.debug("Routing for interface: {}, {} successfully disabled", name, swIfc);
112
113     }
114
115     private SwInterfaceSetTable getInterfaceSetTableRequest(final int swIfc, final byte isIpv6, final int vrfId) {
116         final SwInterfaceSetTable swInterfaceSetTable = new SwInterfaceSetTable();
117         swInterfaceSetTable.isIpv6 = isIpv6;
118         swInterfaceSetTable.swIfIndex = swIfc;
119         swInterfaceSetTable.vrfId = vrfId;
120         return swInterfaceSetTable;
121     }
122 }