Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / cases / OfToSalEthDstCase.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.cases;
10
11 import static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
12
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.data.MatchResponseConvertorData;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthDstCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.dst._case.EthDst;
24
25 public class OfToSalEthDstCase extends ConvertorCase<EthDstCase, MatchBuilder, MatchResponseConvertorData> {
26     public OfToSalEthDstCase() {
27         super(EthDstCase.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
28     }
29
30     @Override
31     public Optional<MatchBuilder> process(@Nonnull EthDstCase source, MatchResponseConvertorData data) {
32         final MatchBuilder matchBuilder = data.getMatchBuilder();
33         final EthernetMatchBuilder ethMatchBuilder = data.getEthernetMatchBuilder();
34
35         final EthDst ethDstCase = source.getEthDst();
36
37         if (ethDstCase != null) {
38             EthernetDestinationBuilder ethDestinationBuilder = new EthernetDestinationBuilder();
39             ethDestinationBuilder.setAddress(ethDstCase.getMacAddress());
40             byte[] destinationMask = ethDstCase.getMask();
41
42             if (destinationMask != null) {
43                 ethDestinationBuilder.setMask(new MacAddress(macAddressToString(destinationMask)));
44             }
45
46             ethMatchBuilder.setEthernetDestination(ethDestinationBuilder.build());
47             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
48         }
49
50         return Optional.of(matchBuilder);
51     }
52 }