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