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.translate.v3po.interfaces.pbb;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
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.MacTranslator;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.vpp.jvpp.VppBaseCallException;
29 import io.fd.vpp.jvpp.core.dto.L2InterfacePbbTagRewrite;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.concurrent.TimeoutException;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.pbb.rev161214.interfaces._interface.PbbRewrite;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 public class PbbRewriteCustomizer extends FutureJVppCustomizer
38 implements WriterCustomizer<PbbRewrite>, MacTranslator, JvppReplyConsumer {
40 private static final int OPERATION_DISABLE = 0;
42 private final NamingContext interfaceNamingContext;
44 public PbbRewriteCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
45 @Nonnull final NamingContext interfaceNamingContext) {
46 super(futureJVppCore);
47 this.interfaceNamingContext = checkNotNull(interfaceNamingContext, "Interface naming context cannot be null");
51 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<PbbRewrite> id,
52 @Nonnull final PbbRewrite dataAfter, @Nonnull final WriteContext writeContext)
53 throws WriteFailedException {
55 setPbbRewrite(id, dataAfter, writeContext, false);
56 } catch (TimeoutException | VppBaseCallException e) {
57 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
62 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<PbbRewrite> id,
63 @Nonnull final PbbRewrite dataBefore, @Nonnull final PbbRewrite dataAfter,
64 @Nonnull final WriteContext writeContext) throws WriteFailedException {
66 setPbbRewrite(id, dataAfter, writeContext, false);
67 } catch (TimeoutException | VppBaseCallException e) {
68 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
73 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<PbbRewrite> id,
74 @Nonnull final PbbRewrite dataBefore, @Nonnull final WriteContext writeContext)
75 throws WriteFailedException {
78 setPbbRewrite(id, dataBefore, writeContext, true);
79 } catch (TimeoutException | VppBaseCallException e) {
80 throw new WriteFailedException.DeleteFailedException(id, e);
84 private void setPbbRewrite(final InstanceIdentifier<PbbRewrite> id, final PbbRewrite data,
85 final WriteContext writeContext, final boolean disable)
86 throws TimeoutException, VppBaseCallException {
87 final String interfaceName = checkNotNull(id.firstKeyOf(Interface.class), "Interface key not found").getName();
89 final L2InterfacePbbTagRewrite request = new L2InterfacePbbTagRewrite();
91 //checking all attributes in preconditions(pbb-rewrite is subcontainer, so there can't be mandatory statements)
92 request.swIfIndex = interfaceNamingContext.getIndex(interfaceName, writeContext.getMappingContext());
93 request.bDmac = parseMac(verifiedDestinationAddress(data));
94 request.bSmac = parseMac(verifiedSourceAddress(data));
95 request.bVlanid = verifiedBVlanId(data);
96 request.iSid = verifiedISid(data);
97 request.vtrOp = verifiedOperation(data, disable);
99 //not sure whats gonna happen to this attribute, so its left optional for now
100 if (data.getOuterTag() != null) {
101 request.outerTag = data.getOuterTag().shortValue();
104 getReply(getFutureJVpp().l2InterfacePbbTagRewrite(request).toCompletableFuture());
107 private String verifiedDestinationAddress(final PbbRewrite data) {
108 return checkNotNull(data.getDestinationAddress(), "Destination address cannot be null").getValue();
111 private String verifiedSourceAddress(final PbbRewrite data) {
112 return checkNotNull(data.getSourceAddress(), "Destination address cannot be null").getValue();
115 private byte verifiedBVlanId(final PbbRewrite data) {
116 return (byte) checkNotNull(data.getBVlanTagVlanId(), "BVlan id cannot be null").shortValue();
119 private int verifiedISid(final PbbRewrite data) {
120 return checkNotNull(data.getITagIsid(), "ISid cannot be null").intValue();
123 // if disabled ,then uses non-public allowed value 0, which is equal to operation disable
124 private int verifiedOperation(final PbbRewrite data, final boolean disable) {
128 : checkNotNull(data.getInterfaceOperation(), "Operation cannot be null").getIntValue();