ae271048ba8bbe4feb4ad17119912f73f4682d79
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / ActionSetNwDstConvertorImpl.java
1 /**
2  * Copyright (c) 2013 Ericsson. 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.action;
10
11 import java.math.BigInteger;
12
13 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
20
21 /**
22  * Utility class for converting a MD-SAL action subelement into the OF subelement
23  */
24 public class ActionSetNwDstConvertorImpl implements Convertor<SetNwDstActionCase, Object> {
25     
26     private static final String PREFIX_SEPARATOR = "/";
27     
28     @Override
29     public Object convert(SetNwDstActionCase source, BigInteger datapathid) {
30         Address address = source.getSetNwDstAction().getAddress();
31         if (address instanceof Ipv4) {
32             String[] addressParts = ((Ipv4) address).getIpv4Address().getValue().split(PREFIX_SEPARATOR);
33             return new Ipv4Address(addressParts[0]);
34         } else if (address instanceof Ipv6) {
35             String[] addressParts = ((Ipv6) address).getIpv6Address().getValue().split(PREFIX_SEPARATOR);
36             return new Ipv6Address(addressParts[0]);
37         } else {
38             throw new IllegalArgumentException("Address is not supported: "+address.getClass().getName());
39         }
40     }
41 }