HONEYCOMB-154: update revison of models that changed since 16.09
[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.vpp.util.AbstractInterfaceTypeCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
22 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
23 import io.fd.honeycomb.translate.vpp.util.NamingContext;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.vpp.jvpp.core.dto.CreateVhostUserIf;
27 import io.fd.vpp.jvpp.core.dto.CreateVhostUserIfReply;
28 import io.fd.vpp.jvpp.core.dto.DeleteVhostUserIf;
29 import io.fd.vpp.jvpp.core.dto.DeleteVhostUserIfReply;
30 import io.fd.vpp.jvpp.core.dto.ModifyVhostUserIf;
31 import io.fd.vpp.jvpp.core.dto.ModifyVhostUserIfReply;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUser;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Writer Customizer responsible for passing vhost user interface CRD operations to VPP
45  */
46 public class VhostUserCustomizer extends AbstractInterfaceTypeCustomizer<VhostUser>
47         implements ByteDataTranslator, JvppReplyConsumer {
48
49     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
50     private final NamingContext interfaceContext;
51
52     public VhostUserCustomizer(@Nonnull final FutureJVppCore 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.rev161214.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         createVhostUserIf(id, swIfName, dataAfter, writeContext);
68     }
69
70     private void createVhostUserIf(final InstanceIdentifier<VhostUser> id, final String swIfName,
71                                    final VhostUser vhostUser, final WriteContext writeContext)
72             throws WriteFailedException {
73         LOG.debug("Creating vhost user interface: name={}, vhostUser={}", swIfName, vhostUser);
74
75         final CompletionStage<CreateVhostUserIfReply> createVhostUserIfReplyCompletionStage =
76                 getFutureJVpp().createVhostUserIf(getCreateVhostUserIfRequest(vhostUser));
77         final CreateVhostUserIfReply reply =
78                 getReplyForCreate(createVhostUserIfReplyCompletionStage.toCompletableFuture(), id, vhostUser);
79         LOG.debug("Vhost user interface created successfully for: {}, vhostUser: {}", swIfName, vhostUser);
80         // Add new interface to our interface context
81         interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
82     }
83
84     private CreateVhostUserIf getCreateVhostUserIfRequest(final VhostUser vhostUser) {
85         CreateVhostUserIf request = new CreateVhostUserIf();
86         request.isServer = booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole()));
87         request.sockFilename = vhostUser.getSocket().getBytes();
88         // TODO HONEYCOMB-177 expose device instance attribute just like for TAP
89         request.renumber = 0;
90         request.customDevInstance = 0;
91         request.useCustomMac = 0;
92         request.macAddress = new byte[]{};
93         return request;
94     }
95
96     @Override
97     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
98                                         @Nonnull final VhostUser dataBefore, @Nonnull final VhostUser dataAfter,
99                                         @Nonnull final WriteContext writeContext)
100             throws WriteFailedException {
101         final String swIfName = id.firstKeyOf(Interface.class).getName();
102         modifyVhostUserIf(id, swIfName, dataBefore, dataAfter, writeContext);
103     }
104
105     private void modifyVhostUserIf(final InstanceIdentifier<VhostUser> id, final String swIfName,
106                                    final VhostUser vhostUserBefore, final VhostUser vhostUserAfter,
107                                    final WriteContext writeContext) throws WriteFailedException {
108         LOG.debug("Updating vhost user interface: name={}, vhostUser={}", swIfName, vhostUserAfter);
109         final CompletionStage<ModifyVhostUserIfReply> modifyVhostUserIfReplyCompletionStage =
110                 getFutureJVpp()
111                         .modifyVhostUserIf(getModifyVhostUserIfRequest(vhostUserAfter,
112                                 interfaceContext.getIndex(swIfName, writeContext.getMappingContext())));
113
114         getReplyForUpdate(modifyVhostUserIfReplyCompletionStage.toCompletableFuture(), id, vhostUserBefore,
115                 vhostUserAfter);
116         LOG.debug("Vhost user interface updated successfully for: {}, vhostUser: {}", swIfName, vhostUserAfter);
117     }
118
119     private ModifyVhostUserIf getModifyVhostUserIfRequest(final VhostUser vhostUser, final int swIfIndex) {
120         ModifyVhostUserIf request = new ModifyVhostUserIf();
121         request.isServer = booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole()));
122         request.sockFilename = vhostUser.getSocket().getBytes();
123         // TODO HONEYCOMB-177
124         request.renumber = 0;
125         request.customDevInstance = 0;
126         request.swIfIndex = swIfIndex;
127         return request;
128     }
129
130     @Override
131     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
132                                         @Nonnull final VhostUser dataBefore, @Nonnull final WriteContext writeContext)
133             throws WriteFailedException {
134         final String swIfName = id.firstKeyOf(Interface.class).getName();
135         deleteVhostUserIf(id, swIfName, dataBefore, writeContext);
136     }
137
138     private void deleteVhostUserIf(final InstanceIdentifier<VhostUser> id, final String swIfName,
139                                    final VhostUser vhostUser, final WriteContext writeContext)
140             throws WriteFailedException {
141         LOG.debug("Deleting vhost user interface: name={}, vhostUser={}", swIfName, vhostUser);
142         final CompletionStage<DeleteVhostUserIfReply> deleteVhostUserIfReplyCompletionStage =
143                 getFutureJVpp().deleteVhostUserIf(getDeleteVhostUserIfRequest(
144                         interfaceContext.getIndex(swIfName, writeContext.getMappingContext())));
145
146         getReplyForDelete(deleteVhostUserIfReplyCompletionStage.toCompletableFuture(), id);
147         LOG.debug("Vhost user interface deleted successfully for: {}, vhostUser: {}", swIfName, vhostUser);
148         // Remove interface from our interface context
149         interfaceContext.removeName(swIfName, writeContext.getMappingContext());
150     }
151
152     private DeleteVhostUserIf getDeleteVhostUserIfRequest(final int swIfIndex) {
153         DeleteVhostUserIf request = new DeleteVhostUserIf();
154         request.swIfIndex = swIfIndex;
155         return request;
156     }
157 }