61c345b44dde16673a372e94b167fc215c2a3561
[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.vppstate;
18
19 import com.google.common.collect.Lists;
20 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
21 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer;
22 import io.fd.honeycomb.v3po.translate.Context;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomainsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.Interface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.InterfaceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.L2Fib;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.L2FibBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.vppjapi.vppBridgeDomainDetails;
41 import org.openvpp.vppjapi.vppBridgeDomainInterfaceDetails;
42 import org.openvpp.vppjapi.vppL2Fib;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public final class BridgeDomainCustomizer extends VppApiCustomizer
47     implements ListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder> {
48
49     private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
50
51     public BridgeDomainCustomizer(@Nonnull final org.openvpp.vppjapi.vppApi vppApi) {
52         super(vppApi);
53     }
54
55     @Override
56     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
57                                       @Nonnull final BridgeDomainBuilder builder, @Nonnull final Context context) {
58         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
59                 id, builder, context);
60
61         final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
62         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
63
64         final int bdId = getVppApi().bridgeDomainIdFromName(key.getName());
65         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
66
67         final vppBridgeDomainDetails bridgeDomainDetails = getVppApi().getBridgeDomainDetails(bdId);
68         if(bridgeDomainDetails == null) {
69             LOG.debug("Bridge domain name={} does not exist", key.getName());
70             return;
71         }
72         logBridgeDomainDetails(bridgeDomainDetails);
73
74         builder.setName(key.getName());
75         // builder.setName(bridgeDomainDetails.name);
76         builder.setArpTermination(bridgeDomainDetails.arpTerm);
77         builder.setFlood(bridgeDomainDetails.flood);
78         builder.setForward(bridgeDomainDetails.forward);
79         builder.setLearn(bridgeDomainDetails.learn);
80         builder.setUnknownUnicastFlood(bridgeDomainDetails.uuFlood);
81
82         builder.setInterface(getIfcs(bridgeDomainDetails));
83
84         final vppL2Fib[] vppL2Fibs = getVppApi().l2FibTableDump(bdId);
85
86         final List<L2Fib> l2Fibs = Lists.newArrayListWithCapacity(vppL2Fibs.length);
87         for (vppL2Fib vppL2Fib : vppL2Fibs) {
88             l2Fibs.add(new L2FibBuilder()
89                 .setAction((vppL2Fib.filter
90                     ? L2Fib.Action.Filter
91                     : L2Fib.Action.Forward))
92                 .setBridgedVirtualInterface(vppL2Fib.bridgedVirtualInterface)
93                 .setOutgoingInterface(vppL2Fib.outgoingInterface)
94                 .setPhysAddress(new PhysAddress(getMacAddress(vppL2Fib.physAddress)))
95                 .setStaticConfig(vppL2Fib.staticConfig)
96                 .build());
97         }
98         builder.setL2Fib(l2Fibs);
99     }
100
101     private void logBridgeDomainDetails(final vppBridgeDomainDetails bridgeDomainDetails) {
102         LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
103         if (bridgeDomainDetails != null) {
104             LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
105             LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
106             LOG.debug("bridgeDomainDetails.bviInterfaceName={}", bridgeDomainDetails.bviInterfaceName);
107             LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
108             LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
109             LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
110             LOG.debug("bridgeDomainDetails.name={}", bridgeDomainDetails.name);
111             LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
112         }
113     }
114
115
116     private static String getMacAddress(byte[] mac) {
117         StringBuilder sb = new StringBuilder(18);
118         for (byte b : mac) {
119             if (sb.length() > 0) {
120                 sb.append(':');
121             }
122             sb.append(String.format("%02x", b));
123         }
124         return sb.toString();
125     }
126
127     private List<Interface> getIfcs(final vppBridgeDomainDetails bridgeDomainDetails) {
128         final List<Interface> ifcs = new ArrayList<>(bridgeDomainDetails.interfaces.length);
129         for (vppBridgeDomainInterfaceDetails anInterface : bridgeDomainDetails.interfaces) {
130             ifcs.add(new InterfaceBuilder()
131                 .setBridgedVirtualInterface(bridgeDomainDetails.bviInterfaceName.equals(anInterface.interfaceName))
132                 .setName(anInterface.interfaceName)
133                 .setKey(new InterfaceKey(anInterface.interfaceName))
134                 .build());
135         }
136         return ifcs;
137     }
138
139     @Nonnull
140     @Override
141     public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
142         return new BridgeDomainBuilder();
143     }
144
145     @Nonnull
146     @Override
147     public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id, @Nonnull final Context context) {
148         final int[] bIds = getVppApi().bridgeDomainDump(-1);
149
150         if(bIds == null) {
151             // No bridge domains
152             return Collections.emptyList();
153         }
154
155         LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIds.length);
156         final List<BridgeDomainKey> allIds = new ArrayList<>(bIds.length);
157         for (int bId : bIds) {
158             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bId={}", bId);
159             // FIXME this is highly inefficient having to dump all of the bridge domain details
160             // Use context to store already read information
161             // TODO Or just remove the getAllIds method and replace with a simple readAll
162             final vppBridgeDomainDetails bridgeDomainDetails = getVppApi().getBridgeDomainDetails(bId);
163             logBridgeDomainDetails(bridgeDomainDetails);
164
165             final String bName = bridgeDomainDetails.name;
166             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
167             allIds.add(new BridgeDomainKey(bName));
168         }
169
170         return allIds;
171     }
172
173     @Override
174     public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<BridgeDomain> readData) {
175         ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
176     }
177 }