9a27961241d40ffd38b55f2ea19a507ce4f4aaa0
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vppstate / BridgeDomainCustomizer.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.vppstate;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Iterables;
21 import io.fd.honeycomb.translate.read.ReadContext;
22 import io.fd.honeycomb.translate.read.ReadFailedException;
23 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
25 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomainsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetails;
39 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
40 import io.fd.vpp.jvpp.core.dto.BridgeDomainDump;
41 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public final class BridgeDomainCustomizer extends FutureJVppCustomizer
46         implements ListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder>, ByteDataTranslator {
47
48     private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
49     private final NamingContext bdContext;
50
51     public BridgeDomainCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
52                                   @Nonnull final NamingContext bdContext) {
53         super(futureJVppCore);
54         this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
55     }
56
57     @Override
58     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
59                                       @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
60             throws ReadFailedException {
61         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
62                 id, builder, context);
63
64         final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
65         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
66
67         final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
68         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
69
70         BridgeDomainDetailsReplyDump reply;
71         BridgeDomainDetails bridgeDomainDetails;
72         final BridgeDomainDump request = new BridgeDomainDump();
73         request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
74         try {
75             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
76             bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
77         } catch (Exception e) {
78             LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
79             return;
80         }
81
82         logBridgeDomainDetails(bridgeDomainDetails);
83
84         builder.setName(key.getName());
85         builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
86         builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
87         builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
88         builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
89         builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
90     }
91
92     private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
93         LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
94         if (bridgeDomainDetails != null) {
95             LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
96             LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
97             LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
98             LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
99             LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
100             LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
101             LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
102             LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
103         }
104     }
105
106     @Nonnull
107     @Override
108     public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
109         return new BridgeDomainBuilder();
110     }
111
112     @Nonnull
113     @Override
114     public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
115                                            @Nonnull final ReadContext context) throws ReadFailedException {
116         final BridgeDomainDump request = new BridgeDomainDump();
117         request.bdId = -1; // dump call
118
119         BridgeDomainDetailsReplyDump reply;
120         try {
121             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
122         } catch (Exception e) {
123             throw new ReadFailedException(id, e);
124         }
125
126         if (reply == null || reply.bridgeDomainDetails == null) {
127             return Collections.emptyList();
128         }
129
130         final int bIdsLength = reply.bridgeDomainDetails.size();
131         LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
132         if (bIdsLength == 0) {
133             // No bridge domains
134             return Collections.emptyList();
135         }
136
137         final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
138         for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
139             logBridgeDomainDetails(detail);
140
141             final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
142             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
143             allIds.add(new BridgeDomainKey(bName));
144         }
145
146         return allIds;
147     }
148
149     @Override
150     public void merge(@Nonnull final Builder<? extends DataObject> builder,
151                       @Nonnull final List<BridgeDomain> readData) {
152         ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
153     }
154 }