HONEYCOMB-230 : Refactor of Adjacencies api use
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / AceEthWriter.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.acl.ingress;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import io.fd.honeycomb.translate.vpp.util.MacTranslator;
21 import java.util.List;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEth;
25 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
26 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
27 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class AceEthWriter extends AbstractAceWriter<AceEth> implements MacTranslator {
33
34     @VisibleForTesting
35     static final int MATCH_N_VECTORS = 1;
36     private static final Logger LOG = LoggerFactory.getLogger(AceEthWriter.class);
37
38     public AceEthWriter(@Nonnull final FutureJVppCore futureJVppCore) {
39         super(futureJVppCore);
40     }
41
42     @Override
43     public ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action,
44                                                    @Nonnull final AceEth aceEth,
45                                                    @Nonnull final int nextTableIndex,
46                                                    final int vlanTags) {
47         final ClassifyAddDelTable request = createClassifyTable(action, nextTableIndex);
48
49         request.mask = new byte[16];
50         boolean aceIsEmpty = true;
51
52         // destination-mac-address or destination-mac-address-mask is present =>
53         // ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00
54         if (aceEth.getDestinationMacAddressMask() != null) {
55             aceIsEmpty = false;
56             final String macAddress = aceEth.getDestinationMacAddressMask().getValue();
57             final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
58             int i = 0;
59             for (String part : parts) {
60                 request.mask[i++] = parseHexByte(part);
61             }
62         } else if (aceEth.getDestinationMacAddress() != null) {
63             aceIsEmpty = false;
64             for (int i = 0; i < 6; ++i) {
65                 request.mask[i] = (byte) 0xff;
66             }
67         }
68
69         // source-mac-address or source-mac-address-mask =>
70         // 00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:00:00:00:00
71         if (aceEth.getSourceMacAddressMask() != null) {
72             aceIsEmpty = false;
73             final String macAddress = aceEth.getSourceMacAddressMask().getValue();
74             final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
75             int i = 6;
76             for (String part : parts) {
77                 request.mask[i++] = parseHexByte(part);
78             }
79         } else if (aceEth.getSourceMacAddress() != null) {
80             aceIsEmpty = false;
81             for (int i = 6; i < 12; ++i) {
82                 request.mask[i] = (byte) 0xff;
83             }
84         }
85
86         if (aceIsEmpty) {
87             throw new IllegalArgumentException(
88                     String.format("Ace %s does not define packet field match values", aceEth.toString()));
89         }
90
91         request.skipNVectors = 0;
92         request.matchNVectors = MATCH_N_VECTORS;
93
94         LOG.debug("ACE action={}, rule={} translated to table={}.", action, aceEth, request);
95         return request;
96     }
97
98     @Override
99     public ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action,
100                                                        @Nonnull final AceEth aceEth,
101                                                        @Nonnull final int tableIndex,
102                                                        final int vlanTags) {
103         final ClassifyAddDelSession request = createClassifySession(action, tableIndex);
104
105         request.match = new byte[16];
106         boolean noMatch = true;
107
108         if (aceEth.getDestinationMacAddress() != null) {
109             noMatch = false;
110             final String macAddress = aceEth.getDestinationMacAddress().getValue();
111             final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
112             int i = 0;
113             for (String part : parts) {
114                 request.match[i++] = parseHexByte(part);
115             }
116         }
117
118         if (aceEth.getSourceMacAddress() != null) {
119             noMatch = false;
120             final String macAddress = aceEth.getSourceMacAddress().getValue();
121             final List<String> parts = COLON_SPLITTER.splitToList(macAddress);
122             int i = 6;
123             for (String part : parts) {
124                 request.match[i++] = parseHexByte(part);
125             }
126         }
127
128         if (noMatch) {
129             throw new IllegalArgumentException(
130                     String.format("Ace %s does not define neither source nor destination MAC address",
131                             aceEth.toString()));
132         }
133
134         LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceEth, request);
135         return request;
136     }
137
138     @Override
139     protected void setClassifyTable(@Nonnull final InputAclSetInterface request, final int tableIndex) {
140         request.l2TableIndex = tableIndex;
141     }
142 }