Merge "Migrate most of openflowplugin to use Uint types"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / cases / OfToSalEthSrcCase.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 org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.data.MatchResponseConvertorData;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthSrcCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.src._case.EthSrc;
25
26 public class OfToSalEthSrcCase extends ConvertorCase<EthSrcCase, MatchBuilder, MatchResponseConvertorData> {
27     public OfToSalEthSrcCase() {
28         super(EthSrcCase.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
29     }
30
31     @Override
32     public Optional<MatchBuilder> process(@NonNull EthSrcCase source, MatchResponseConvertorData data,
33             ConvertorExecutor convertorExecutor) {
34         final MatchBuilder matchBuilder = data.getMatchBuilder();
35         final EthernetMatchBuilder ethMatchBuilder = data.getEthernetMatchBuilder();
36
37         final EthSrc ethSrcCase = source.getEthSrc();
38
39         if (ethSrcCase != null) {
40             EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
41             ethSourceBuilder.setAddress(ethSrcCase.getMacAddress());
42             byte[] mask = ethSrcCase.getMask();
43
44             if (mask != null) {
45                 ethSourceBuilder.setMask(new MacAddress(macAddressToString(mask)));
46             }
47
48             ethMatchBuilder.setEthernetSource(ethSourceBuilder.build());
49             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
50         }
51
52         return Optional.of(matchBuilder);
53     }
54 }