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