14124dcc7fec96cf4cc75ec203a904bb1c86100b
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / 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.common;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 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;
27 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;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class AceEthWriter implements AceWriter<AceEth>, AclTranslator, L2AclTranslator {
33
34     @VisibleForTesting
35     static final int MATCH_N_VECTORS = 1;
36     private static final Logger LOG = LoggerFactory.getLogger(AceEthWriter.class);
37
38     @Override
39     public ClassifyAddDelTable createTable(@Nonnull final AceEth aceEth,
40                                            @Nullable final InterfaceMode mode,
41                                            final int nextTableIndex,
42                                            final int vlanTags) {
43         final ClassifyAddDelTable request = createTable(nextTableIndex);
44
45         request.mask = new byte[16];
46         boolean aceIsEmpty =
47             destinationMacAddressMask(aceEth.getDestinationMacAddressMask(), aceEth.getDestinationMacAddress(),
48                 request);
49         aceIsEmpty &=
50             sourceMacAddressMask(aceEth.getSourceMacAddressMask(), aceEth.getSourceMacAddress(), request);
51
52         if (aceIsEmpty) {
53             throw new IllegalArgumentException(
54                 String.format("Ace %s does not define packet field match values", aceEth.toString()));
55         }
56
57         request.skipNVectors = 0;
58         request.matchNVectors = MATCH_N_VECTORS;
59
60         LOG.debug("ACE rule={} translated to table={}.", aceEth, request);
61         return request;
62     }
63
64     @Override
65     public List<ClassifyAddDelSession> createSession(@Nonnull final PacketHandling action,
66                                                      @Nonnull final AceEth aceEth,
67                                                      @Nullable final InterfaceMode mode,
68                                                      final int tableIndex,
69                                                      final int vlanTags) {
70         final ClassifyAddDelSession request = createSession(action, tableIndex);
71
72         request.match = new byte[16];
73         boolean noMatch = destinationMacAddressMatch(aceEth.getDestinationMacAddress(), request);
74         noMatch &= sourceMacAddressMatch(aceEth.getSourceMacAddress(), request);
75
76         if (noMatch) {
77             throw new IllegalArgumentException(
78                 String.format("Ace %s does not define neither source nor destination MAC address",
79                     aceEth.toString()));
80         }
81
82         LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceEth, request);
83         return Collections.singletonList(request);
84     }
85 }