aef45c8215f1c104e13b660c8ef66c88a01f3705
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / v3po / translate / v3po / interfaces / VhostUserCustomizer.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.v3po.translate.v3po.interfaces;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import io.fd.honeycomb.v3po.translate.v3po.util.AbstractInterfaceTypeCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
24 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
25 import io.fd.honeycomb.v3po.translate.write.WriteContext;
26 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
27 import java.util.concurrent.CompletionStage;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
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.rev150105.VhostUserRole;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.openvpp.jvpp.dto.CreateVhostUserIf;
37 import org.openvpp.jvpp.dto.CreateVhostUserIfReply;
38 import org.openvpp.jvpp.dto.DeleteVhostUserIf;
39 import org.openvpp.jvpp.dto.DeleteVhostUserIfReply;
40 import org.openvpp.jvpp.dto.ModifyVhostUserIf;
41 import org.openvpp.jvpp.dto.ModifyVhostUserIfReply;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Writer Customizer responsible for passing vhost user interface CRD operations to VPP
48  */
49 public class VhostUserCustomizer extends AbstractInterfaceTypeCustomizer<VhostUser> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
52     private final NamingContext interfaceContext;
53
54     public VhostUserCustomizer(@Nonnull final FutureJVpp vppApi, @Nonnull final NamingContext interfaceContext) {
55         super(vppApi);
56         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
57     }
58
59     @Nonnull
60     @Override
61     public Optional<VhostUser> extract(@Nonnull final InstanceIdentifier<VhostUser> currentId,
62                                        @Nonnull final DataObject parentData) {
63         return Optional.fromNullable(((VppInterfaceAugmentation) parentData).getVhostUser());
64     }
65
66     @Override
67     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
68         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class;
69     }
70
71     @Override
72     protected final void writeInterface(@Nonnull final InstanceIdentifier<VhostUser> id,
73                                        @Nonnull final VhostUser dataAfter, @Nonnull final WriteContext writeContext)
74             throws WriteFailedException.CreateFailedException {
75         try {
76             createVhostUserIf(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
77         } catch (VppApiInvocationException | IllegalInterfaceTypeException e) {
78             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
79         }
80     }
81
82     private void createVhostUserIf(final String swIfName, final VhostUser vhostUser, final WriteContext writeContext) throws VppApiInvocationException {
83         LOG.debug("Creating vhost user interface: name={}, vhostUser={}", swIfName, vhostUser);
84         final CompletionStage<CreateVhostUserIfReply> createVhostUserIfReplyCompletionStage =
85                 getFutureJVpp().createVhostUserIf(getCreateVhostUserIfRequest(vhostUser));
86
87         final CreateVhostUserIfReply reply =
88                 V3poUtils.getReply(createVhostUserIfReplyCompletionStage.toCompletableFuture());
89         if (reply.retval < 0) {
90             LOG.debug("Failed to create vhost user interface: {}, vhostUser: {}", swIfName, vhostUser);
91             throw new VppApiInvocationException("createVhostUserIf", reply.context, reply.retval);
92         } else {
93             LOG.debug("Vhost user interface created successfully for: {}, vhostUser: {}", swIfName, vhostUser);
94             // Add new interface to our interface context
95             interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
96         }
97     }
98
99     private CreateVhostUserIf getCreateVhostUserIfRequest(final VhostUser vhostUser) {
100         CreateVhostUserIf request = new CreateVhostUserIf();
101         request.isServer = V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole()));
102         request.sockFilename = vhostUser.getSocket().getBytes();
103         request.renumber = 0; // TODO
104         request.customDevInstance = 0; // TODO
105         request.useCustomMac = 0;
106         request.macAddress = new byte[]{};
107         return request;
108     }
109
110     @Override
111     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
112                                         @Nonnull final VhostUser dataBefore, @Nonnull final VhostUser dataAfter,
113                                         @Nonnull final WriteContext writeContext)
114             throws WriteFailedException.UpdateFailedException {
115         try {
116             modifyVhostUserIf(id.firstKeyOf(Interface.class).getName(), dataAfter, writeContext);
117         } catch (VppApiInvocationException e) {
118             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
119         }
120     }
121
122     private void modifyVhostUserIf(final String swIfName, final VhostUser vhostUser, final WriteContext writeContext) throws VppApiInvocationException {
123         LOG.debug("Updating vhost user interface: name={}, vhostUser={}", swIfName, vhostUser);
124         final CompletionStage<ModifyVhostUserIfReply> modifyVhostUserIfReplyCompletionStage =
125                 getFutureJVpp()
126                         .modifyVhostUserIf(getModifyVhostUserIfRequest(vhostUser, interfaceContext.getIndex(swIfName, writeContext.getMappingContext())));
127
128         final ModifyVhostUserIfReply reply =
129                 V3poUtils.getReply(modifyVhostUserIfReplyCompletionStage.toCompletableFuture());
130         if (reply.retval < 0) {
131             LOG.debug("Failed to update vhost user interface: {}, vhostUser: {}", swIfName, vhostUser);
132             throw new VppApiInvocationException("modifyVhostUserIf", reply.context, reply.retval);
133         } else {
134             LOG.debug("Vhost user interface updated successfully for: {}, vhostUser: {}", swIfName, vhostUser);
135         }
136     }
137
138     private ModifyVhostUserIf getModifyVhostUserIfRequest(final VhostUser vhostUser, final int swIfIndex) {
139         ModifyVhostUserIf request = new ModifyVhostUserIf();
140         request.isServer = V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole()));
141         request.sockFilename = vhostUser.getSocket().getBytes();
142         request.renumber = 0; // TODO
143         request.customDevInstance = 0; // TODO
144         request.swIfIndex = swIfIndex;
145         return request;
146     }
147
148     @Override
149     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
150                                         @Nonnull final VhostUser dataBefore, @Nonnull final WriteContext writeContext)
151             throws WriteFailedException.DeleteFailedException {
152         try {
153             deleteVhostUserIf(id.firstKeyOf(Interface.class).getName(), dataBefore, writeContext);
154         } catch (VppApiInvocationException e) {
155             throw new WriteFailedException.DeleteFailedException(id, e);
156         }
157     }
158
159     private void deleteVhostUserIf(final String swIfName, final VhostUser vhostUser, final WriteContext writeContext) throws VppApiInvocationException {
160         LOG.debug("Deleting vhost user interface: name={}, vhostUser={}", swIfName, vhostUser);
161         final CompletionStage<DeleteVhostUserIfReply> deleteVhostUserIfReplyCompletionStage =
162                 getFutureJVpp().deleteVhostUserIf(getDeleteVhostUserIfRequest(interfaceContext.getIndex(swIfName, writeContext.getMappingContext())));
163
164         final DeleteVhostUserIfReply reply =
165                 V3poUtils.getReply(deleteVhostUserIfReplyCompletionStage.toCompletableFuture());
166         if (reply.retval < 0) {
167             LOG.debug("Failed to delete vhost user interface: {}, vhostUser: {}", swIfName, vhostUser);
168             throw new VppApiInvocationException("modifyVhostUserIf", reply.context, reply.retval);
169         } else {
170             LOG.debug("Vhost user interface deleted successfully for: {}, vhostUser: {}", swIfName, vhostUser);
171             // Remove interface from our interface context
172             interfaceContext.removeName(swIfName, writeContext.getMappingContext());
173         }
174     }
175
176     private DeleteVhostUserIf getDeleteVhostUserIfRequest(final int swIfIndex) {
177         DeleteVhostUserIf request = new DeleteVhostUserIf();
178         request.swIfIndex = swIfIndex;
179         return request;
180     }
181 }