a6d17cf2e6d9f5c1d52e9ed50f0a3dfcc46f6be8
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / 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.interfacesstate;
18
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.read.ReadFailedException;
21 import io.fd.honeycomb.translate.spi.read.Initialized;
22 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceVhostUserDetails;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceVhostUserDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceVhostUserDump;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.math.BigInteger;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.CompletionStage;
36 import java.util.stream.Collectors;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.VhostUser;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.VhostUserBuilder;
45 import org.opendaylight.yangtools.concepts.Builder;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52 public class VhostUserCustomizer extends FutureJVppCustomizer
53         implements InitializingReaderCustomizer<VhostUser, VhostUserBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
54
55     public static final String DUMPED_VHOST_USERS_CONTEXT_KEY =
56             VhostUserCustomizer.class.getName() + "dumpedVhostUsersDuringGetAllIds";
57     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
58     private NamingContext interfaceContext;
59
60     public VhostUserCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
61         super(jvpp);
62         this.interfaceContext = interfaceContext;
63     }
64
65     @Override
66     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull VhostUser readValue) {
67         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVhostUser(readValue);
68     }
69
70     @Nonnull
71     @Override
72     public VhostUserBuilder getBuilder(@Nonnull InstanceIdentifier<VhostUser> id) {
73         return new VhostUserBuilder();
74     }
75
76     @Override
77     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
78                                       @Nonnull final VhostUserBuilder builder,
79                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
80
81         final InterfaceKey key = id.firstKeyOf(Interface.class);
82         final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
83         if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
84                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUser.class,
85                 LOG)) {
86             return;
87         }
88
89         LOG.debug("Reading attributes for vhpost user interface: {}", key.getName());
90
91         @SuppressWarnings("unchecked")
92         Map<Integer, SwInterfaceVhostUserDetails> mappedVhostUsers =
93                 (Map<Integer, SwInterfaceVhostUserDetails>) ctx.getModificationCache()
94                         .get(DUMPED_VHOST_USERS_CONTEXT_KEY);
95
96         if (mappedVhostUsers == null) {
97             // Full VhostUser dump has to be performed here, no filter or anything is here to help so at least we cache it
98             final SwInterfaceVhostUserDump request = new SwInterfaceVhostUserDump();
99             final CompletionStage<SwInterfaceVhostUserDetailsReplyDump>
100                     swInterfaceVhostUserDetailsReplyDumpCompletionStage =
101                     getFutureJVpp().swInterfaceVhostUserDump(request);
102             final SwInterfaceVhostUserDetailsReplyDump reply =
103                     getReplyForRead(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
104
105             if (null == reply || null == reply.swInterfaceVhostUserDetails) {
106                 mappedVhostUsers = Collections.emptyMap();
107             } else {
108                 final List<SwInterfaceVhostUserDetails> swInterfaceVhostUserDetails =
109                         reply.swInterfaceVhostUserDetails;
110                 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
111                 mappedVhostUsers = swInterfaceVhostUserDetails.stream()
112                         .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
113             }
114
115             ctx.getModificationCache().put(DUMPED_VHOST_USERS_CONTEXT_KEY, mappedVhostUsers);
116         }
117
118         // Relying here that parent InterfaceCustomizer was invoked first to fill in the context with initial ifc mapping
119         final SwInterfaceVhostUserDetails swInterfaceVhostUserDetails = mappedVhostUsers.get(index);
120         LOG.trace("Vhost user interface: {} attributes returned from VPP: {}", key.getName(),
121                 swInterfaceVhostUserDetails);
122
123         builder.setRole(swInterfaceVhostUserDetails.isServer == 1
124                 ? VhostUserRole.Server
125                 : VhostUserRole.Client);
126         builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features));
127         builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions);
128         builder.setSocket(toString(swInterfaceVhostUserDetails.sockFilename));
129         builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz);
130         // TODO: map error code to meaningful message after VPP-436 is done
131         builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno));
132
133         LOG.debug("Vhost user interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
134     }
135
136     @Override
137     public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUser> init(
138             @Nonnull final InstanceIdentifier<VhostUser> id,
139             @Nonnull final VhostUser readValue,
140             @Nonnull final ReadContext ctx) {
141         return Initialized.create(getCfgId(id),
142                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUserBuilder()
143                         .setRole(readValue.getRole())
144                         .setSocket(readValue.getSocket())
145                         .build());
146     }
147
148     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUser> getCfgId(
149             final InstanceIdentifier<VhostUser> id) {
150         return InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
151                 .augmentation(VppInterfaceAugmentation.class)
152                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUser.class);
153     }
154 }