79256ea58746863d87c6347f7aa12b36fb8c974f
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / pbb / PbbRewriteCustomizer.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.interfaces.pbb;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
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.rev160410.interfaces._interface.PbbRewrite;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 public class PbbRewriteCustomizer extends FutureJVppCustomizer
38         implements WriterCustomizer<PbbRewrite>, MacTranslator, JvppReplyConsumer {
39
40     private static final int OPERATION_DISABLE = 0;
41
42     private final NamingContext interfaceNamingContext;
43
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");
48     }
49
50     @Override
51     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<PbbRewrite> id,
52                                        @Nonnull final PbbRewrite dataAfter, @Nonnull final WriteContext writeContext)
53             throws WriteFailedException {
54         try {
55             setPbbRewrite(id, dataAfter, writeContext, false);
56         } catch (TimeoutException | VppBaseCallException e) {
57             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
58         }
59     }
60
61     @Override
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 {
65         try {
66             setPbbRewrite(id, dataAfter, writeContext, false);
67         } catch (TimeoutException | VppBaseCallException e) {
68             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
69         }
70     }
71
72     @Override
73     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<PbbRewrite> id,
74                                         @Nonnull final PbbRewrite dataBefore, @Nonnull final WriteContext writeContext)
75             throws WriteFailedException {
76
77         try {
78             setPbbRewrite(id, dataBefore, writeContext, true);
79         } catch (TimeoutException | VppBaseCallException e) {
80             throw new WriteFailedException.DeleteFailedException(id, e);
81         }
82     }
83
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();
88
89         final L2InterfacePbbTagRewrite request = new L2InterfacePbbTagRewrite();
90
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);
98
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();
102         }
103
104         getReply(getFutureJVpp().l2InterfacePbbTagRewrite(request).toCompletableFuture());
105     }
106
107     private String verifiedDestinationAddress(final PbbRewrite data) {
108         return checkNotNull(data.getDestinationAddress(), "Destination address cannot be null").getValue();
109     }
110
111     private String verifiedSourceAddress(final PbbRewrite data) {
112         return checkNotNull(data.getSourceAddress(), "Destination address cannot be null").getValue();
113     }
114
115     private byte verifiedBVlanId(final PbbRewrite data) {
116         return (byte) checkNotNull(data.getBVlanTagVlanId(), "BVlan id cannot be null").shortValue();
117     }
118
119     private int verifiedISid(final PbbRewrite data) {
120         return checkNotNull(data.getITagIsid(), "ISid cannot be null").intValue();
121     }
122
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) {
125
126         return disable
127                 ? OPERATION_DISABLE
128                 : checkNotNull(data.getInterfaceOperation(), "Operation cannot be null").getIntValue();
129     }
130 }