23e1a6efd7a28ea3b6faf317a756015238ca850e
[hc2vpp.git] /
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.v3po.translate.v3po.interfacesstate;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.isInterfaceOfType;
20
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.util.TranslateUtils;
27 import java.math.BigInteger;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
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.VppBaseCallException;
44 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetails;
45 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDetailsReplyDump;
46 import org.openvpp.jvpp.dto.SwInterfaceVhostUserDump;
47 import org.openvpp.jvpp.future.FutureJVpp;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52 public class VhostUserCustomizer extends FutureJVppCustomizer
53         implements ChildReaderCustomizer<VhostUser, VhostUserBuilder> {
54
55     private static final Logger LOG = LoggerFactory.getLogger(VhostUserCustomizer.class);
56     public static final String DUMPED_VHOST_USERS_CONTEXT_KEY = VhostUserCustomizer.class.getName() + "dumpedVhostUsersDuringGetAllIds";
57     private NamingContext interfaceContext;
58
59     public VhostUserCustomizer(@Nonnull final FutureJVpp jvpp, @Nonnull final NamingContext interfaceContext) {
60         super(jvpp);
61         this.interfaceContext = interfaceContext;
62     }
63
64     @Override
65     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull VhostUser readValue) {
66         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setVhostUser(readValue);
67     }
68
69     @Nonnull
70     @Override
71     public VhostUserBuilder getBuilder(@Nonnull InstanceIdentifier<VhostUser> id) {
72         return new VhostUserBuilder();
73     }
74
75     @Override
76     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VhostUser> id,
77                                       @Nonnull final VhostUserBuilder builder,
78                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
79         try {
80             final InterfaceKey key = id.firstKeyOf(Interface.class);
81             final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
82             if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
83                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class)) {
84                 return;
85             }
86
87             LOG.debug("Reading attributes for vhpost user interface: {}", key.getName());
88
89             @SuppressWarnings("unchecked")
90             Map<Integer, SwInterfaceVhostUserDetails> mappedVhostUsers =
91                 (Map<Integer, SwInterfaceVhostUserDetails>) ctx.getModificationCache().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> swInterfaceVhostUserDetailsReplyDumpCompletionStage =
97                     getFutureJVpp().swInterfaceVhostUserDump(request);
98                 final SwInterfaceVhostUserDetailsReplyDump reply =
99                     TranslateUtils.getReplyForRead(swInterfaceVhostUserDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
100
101                 if(null == reply || null == reply.swInterfaceVhostUserDetails) {
102                     mappedVhostUsers = Collections.emptyMap();
103                 } else {
104                     final List<SwInterfaceVhostUserDetails> swInterfaceVhostUserDetails = reply.swInterfaceVhostUserDetails;
105                     // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
106                     mappedVhostUsers = swInterfaceVhostUserDetails.stream()
107                         .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
108                 }
109
110                 ctx.getModificationCache().put(DUMPED_VHOST_USERS_CONTEXT_KEY, mappedVhostUsers);
111             }
112
113             // Relying here that parent InterfaceCustomizer was invoked first to fill in the context with initial ifc mapping
114             final SwInterfaceVhostUserDetails swInterfaceVhostUserDetails = mappedVhostUsers.get(index);
115             LOG.trace("Vhost user interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceVhostUserDetails);
116
117             builder.setRole(swInterfaceVhostUserDetails.isServer == 1 ? VhostUserRole.Server : VhostUserRole.Client);
118             builder.setFeatures(BigInteger.valueOf(swInterfaceVhostUserDetails.features));
119             builder.setNumMemoryRegions((long) swInterfaceVhostUserDetails.numRegions);
120             builder.setSocket(TranslateUtils.toString(swInterfaceVhostUserDetails.sockFilename));
121             builder.setVirtioNetHdrSize((long) swInterfaceVhostUserDetails.virtioNetHdrSz);
122             builder.setConnectError(Integer.toString(swInterfaceVhostUserDetails.sockErrno));
123
124             LOG.debug("Vhost user interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
125         } catch (VppBaseCallException e) {
126             LOG.warn("Failed to readCurrentAttributes for: {}", id, e);
127             throw new ReadFailedException(id, e);
128         }
129     }
130 }