c74db7e2a1b2933daf01a23f5811605713078c10
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / L2AclTranslator.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.common;
18
19 import io.fd.honeycomb.translate.vpp.util.MacTranslator;
20 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
22 import java.util.List;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
24
25 interface L2AclTranslator extends MacTranslator {
26
27     default boolean destinationMacAddressMask(final MacAddress dstMask, final MacAddress dstAddress,
28                                               final ClassifyAddDelTable request) {
29         // destination-mac-address or destination-mac-address-mask is present =>
30         // ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00
31         if (dstMask != null) {
32             final List<String> parts = COLON_SPLITTER.splitToList(dstMask.getValue());
33             int i = 0;
34             for (String part : parts) {
35                 request.mask[i++] = parseHexByte(part);
36             }
37             return false;
38         } else if (dstAddress != null) {
39             for (int i = 0; i < 6; ++i) {
40                 request.mask[i] = (byte) 0xff;
41             }
42             return false;
43         }
44         return true;
45     }
46
47     default boolean sourceMacAddressMask(final MacAddress srcMask, final MacAddress srcAddress,
48                                          final ClassifyAddDelTable request) {
49         // source-mac-address or source-mac-address-mask =>
50         // 00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:00:00:00:00
51         if (srcMask != null) {
52             final List<String> parts = COLON_SPLITTER.splitToList(srcMask.getValue());
53             int i = 6;
54             for (String part : parts) {
55                 request.mask[i++] = parseHexByte(part);
56             }
57             return false;
58         } else if (srcAddress != null) {
59             for (int i = 6; i < 12; ++i) {
60                 request.mask[i] = (byte) 0xff;
61             }
62             return false;
63         }
64         return true;
65     }
66
67     default boolean destinationMacAddressMatch(final MacAddress dstAddress, final ClassifyAddDelSession request) {
68         if (dstAddress != null) {
69             final List<String> parts = COLON_SPLITTER.splitToList(dstAddress.getValue());
70             int i = 0;
71             for (String part : parts) {
72                 request.match[i++] = parseHexByte(part);
73             }
74             return false;
75         }
76         return true;
77     }
78
79     default boolean sourceMacAddressMatch(final MacAddress srcAddress, final ClassifyAddDelSession request) {
80         if (srcAddress != null) {
81             final List<String> parts = COLON_SPLITTER.splitToList(srcAddress.getValue());
82             int i = 6;
83             for (String part : parts) {
84                 request.match[i++] = parseHexByte(part);
85             }
86             return false;
87         }
88         return true;
89     }
90 }