HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / LoopbackCustomizer.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.vpp.util.AbstractInterfaceTypeCustomizer;
20 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
21 import io.fd.honeycomb.translate.vpp.util.MacTranslator;
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.CreateLoopback;
26 import io.fd.vpp.jvpp.core.dto.CreateLoopbackReply;
27 import io.fd.vpp.jvpp.core.dto.DeleteLoopback;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Loopback;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class LoopbackCustomizer extends AbstractInterfaceTypeCustomizer<Loopback>
38         implements MacTranslator, JvppReplyConsumer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(LoopbackCustomizer.class);
41     private final NamingContext interfaceContext;
42
43     public LoopbackCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
44         super(vppApi);
45         this.interfaceContext = interfaceContext;
46     }
47
48     @Override
49     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
50         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Loopback.class;
51     }
52
53     @Override
54     protected final void writeInterface(@Nonnull final InstanceIdentifier<Loopback> id, @Nonnull final Loopback dataAfter,
55                                         @Nonnull final WriteContext writeContext)
56             throws WriteFailedException {
57         final String ifcName = id.firstKeyOf(Interface.class).getName();
58         createLoopback(id, ifcName, dataAfter, writeContext);
59     }
60
61     @Override
62     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Loopback> id, @Nonnull final Loopback dataBefore,
63                                         @Nonnull final Loopback dataAfter, @Nonnull final WriteContext writeContext)
64             throws WriteFailedException {
65         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
66                 new UnsupportedOperationException("Modification of loopback interface is not supported"));
67     }
68
69     @Override
70     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Loopback> id, @Nonnull final Loopback dataBefore,
71                                         @Nonnull final WriteContext writeContext)
72             throws WriteFailedException {
73         final String ifcName = id.firstKeyOf(Interface.class).getName();
74
75         final int index;
76         try {
77             index = interfaceContext.getIndex(ifcName, writeContext.getMappingContext());
78         } catch (IllegalArgumentException e) {
79             throw new WriteFailedException.DeleteFailedException(id, e);
80         }
81
82         deleteLoopback(id, ifcName, index, dataBefore, writeContext);
83     }
84
85     private void createLoopback(final InstanceIdentifier<Loopback> id, final String swIfName, final Loopback loopback,
86                                 final WriteContext writeContext) throws WriteFailedException {
87         LOG.debug("Setting loopback interface: {}. Loopback: {}", swIfName, loopback);
88
89         final CreateLoopback createLoopback = new CreateLoopback();
90         if (loopback.getMac() != null) {
91             createLoopback.macAddress = parseMac(loopback.getMac().getValue());
92         }
93         final CreateLoopbackReply reply =
94                 getReplyForCreate(getFutureJVpp().createLoopback(createLoopback).toCompletableFuture(), id, loopback);
95
96         LOG.debug("Loopback set successfully for: {}, loopback: {}", swIfName, loopback);
97         // Add new interface to our interface context
98         interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
99     }
100
101     private void deleteLoopback(final InstanceIdentifier<Loopback> id, final String swIfName, final int index,
102                            final Loopback dataBefore, final WriteContext writeContext)
103             throws WriteFailedException {
104         LOG.debug("Deleting loopback interface: {}. Loopback: {}", swIfName, dataBefore);
105         final DeleteLoopback deleteLoopback = new DeleteLoopback();
106         deleteLoopback.swIfIndex = index;
107         getReplyForDelete(getFutureJVpp().deleteLoopback(deleteLoopback).toCompletableFuture(), id);
108         LOG.debug("Loopback deleted successfully for: {}, loopback: {}", swIfName, dataBefore);
109         // Remove deleted interface from interface context
110         interfaceContext.removeName(swIfName, writeContext.getMappingContext());
111     }
112 }