2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate;
19 import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.isInterfaceOfType;
21 import io.fd.honeycomb.v3po.translate.read.ReadContext;
22 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
23 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
27 import java.math.BigInteger;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.concurrent.CompletionStage;
32 import java.util.stream.Collectors;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VhostUser;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.VhostUserBuilder;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetails;
44 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetailsReplyDump;
45 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDump;
46 import org.openvpp.jvpp.future.FutureJVpp;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 public class VhostUserCustomizer extends FutureJVppCustomizer
52 implements ChildReaderCustomizer<VhostUser, VhostUserBuilder> {
54 private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
55 public static final String DUMPED_VHOST_USERS_CONTEXT_KEY = VhostUserCustomizer.class.getName() + "dumpedVhostUsersDuringGetAllIds";
56 private NamingContext interfaceContext;
58 public VhostUserCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
60 this.interfaceContext = interfaceContext;
64 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull VhostUser readValue) {
65 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVhostUser(readValue);
70 public VhostUserBuilder getBuilder(@Nonnull InstanceIdentifier<VhostUser> id) {
71 return new VhostUserBuilder();
75 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
76 @Nonnull final VhostUserBuilder builder,
77 @Nonnull final ReadContext ctx) throws ReadFailedException {
78 final InterfaceKey key = id.firstKeyOf(Interface.class);
79 // Relying here that parent InterfaceCustomizer was invoked first (PREORDER)
80 // to fill in the context with initial ifc mapping
81 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
82 if (!isInterfaceOfType(ctx.getModificationCache(), index, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class)) {
86 LOG.debug("Reading attributes for vhpost user interface: {}", key.getName());
88 @SuppressWarnings("unchecked")
89 Map<Integer, SwInterfaceVhostUserDetails> mappedVhostUsers =
90 (Map<Integer, SwInterfaceVhostUserDetails>) ctx.getModificationCache().get(DUMPED_VHOST_USERS_CONTEXT_KEY);
92 if(mappedVhostUsers == null) {
93 // Full VhostUser dump has to be performed here, no filter or anything is here to help so at least we cache it
94 final SwInterfaceVhostUserDump request = new SwInterfaceVhostUserDump();
95 final CompletionStage<SwInterfaceVhostUserDetailsReplyDump> swInterfaceVhostUserDetailsReplyDumpCompletionStage =
96 getFutureJVpp().swInterfaceVhostUserDump(request);
97 final SwInterfaceVhostUserDetailsReplyDump reply =
98 V3poUtils.getReply(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture());
100 if(null == reply || null == reply.swInterfaceVhostUserDetails) {
101 mappedVhostUsers = Collections.emptyMap();
103 final List<SwInterfaceVhostUserDetails> swInterfaceVhostUserDetails = reply.swInterfaceVhostUserDetails;
104 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
105 mappedVhostUsers = swInterfaceVhostUserDetails.stream()
106 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
109 ctx.getModificationCache().put(DUMPED_VHOST_USERS_CONTEXT_KEY, mappedVhostUsers);
112 // Relying here that parent InterfaceCustomizer was invoked first to fill in the context with initial ifc mapping
113 final SwInterfaceVhostUserDetails swInterfaceVhostUserDetails = mappedVhostUsers.get(index);
114 LOG.trace("Vhost user interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceVhostUserDetails);
116 builder.setRole(swInterfaceVhostUserDetails.isServer == 1 ? VhostUserRole.Server : VhostUserRole.Client);
117 builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features));
118 builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions);
119 builder.setSocket(V3poUtils.toString(swInterfaceVhostUserDetails.sockFilename));
120 builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz);
121 builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno));
123 LOG.debug("Vhost user interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);