HONEYCOMB-234: update YANG model to support egress ACLs
[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.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.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Routing;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetTable;
33 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetTableReply;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class RoutingCustomizer extends FutureJVppCustomizer implements WriterCustomizer<Routing>, JvppReplyConsumer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(RoutingCustomizer.class);
41     private final NamingContext interfaceContext;
42
43     public RoutingCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
44         super(vppApi);
45         this.interfaceContext = interfaceContext;
46     }
47
48     @Override
49     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
50                                        @Nonnull final Routing dataAfter, @Nonnull final WriteContext writeContext)
51             throws WriteFailedException {
52
53         final String ifName = id.firstKeyOf(Interface.class).getName();
54         try {
55             setRouting(id, ifName, dataAfter, writeContext);
56         } catch (VppBaseCallException e) {
57             LOG.warn("Failed to set routing for interface: {}, {}, vxlan: {}", ifName, writeContext, dataAfter);
58             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
59         }
60     }
61
62     @Override
63     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
64                                         @Nonnull final Routing dataBefore, @Nonnull final Routing dataAfter,
65                                         @Nonnull final WriteContext writeContext)
66             throws WriteFailedException {
67
68         final String ifName = id.firstKeyOf(Interface.class).getName();
69         try {
70             setRouting(id, ifName, dataAfter, writeContext);
71         } catch (VppBaseCallException e) {
72             LOG.warn("Failed to update routing for interface: {}, {}, vxlan: {}", ifName, writeContext, dataAfter);
73             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
74         }
75     }
76
77     @Override
78     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Routing> id,
79                                         @Nonnull final Routing dataBefore, @Nonnull final WriteContext writeContext) {
80         // TODO HONEYCOMB-176 implement delete
81     }
82
83     private void setRouting(final InstanceIdentifier<Routing> id, final String name, final Routing rt,
84                             final WriteContext writeContext) throws VppBaseCallException, WriteTimeoutException {
85         final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
86         LOG.debug("Setting routing for interface: {}, {}. Routing: {}", name, swIfc, rt);
87
88         int vrfId = (rt != null)
89                 ? rt.getVrfId().intValue()
90                 : 0;
91
92         if (vrfId != 0) {
93             final CompletionStage<SwInterfaceSetTableReply> swInterfaceSetTableReplyCompletionStage =
94                     getFutureJVpp()
95                             .swInterfaceSetTable(getInterfaceSetTableRequest(swIfc, (byte) 0, /* isIpv6 */ vrfId));
96             getReplyForWrite(swInterfaceSetTableReplyCompletionStage.toCompletableFuture(), id);
97             LOG.debug("Routing set successfully for interface: {}, {}, routing: {}", name, swIfc, rt);
98         }
99     }
100
101     private SwInterfaceSetTable getInterfaceSetTableRequest(final int swIfc, final byte isIpv6, final int vrfId) {
102         final SwInterfaceSetTable swInterfaceSetTable = new SwInterfaceSetTable();
103         swInterfaceSetTable.isIpv6 = isIpv6;
104         swInterfaceSetTable.swIfIndex = swIfc;
105         swInterfaceSetTable.vrfId = vrfId;
106         return swInterfaceSetTable;
107     }
108 }