Merge "Technical debt MeterUtil, GroupUtil, FlowUtil"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / cases / SalToOfSetNwSrcActionV10Case.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.action.cases;
10
11 import java.util.Optional;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.openflowplugin.api.OFConstants;
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwSrcCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.src._case.SetNwSrcActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
25
26 public class SalToOfSetNwSrcActionV10Case extends ConvertorCase<SetNwSrcActionCase, Action, ActionConvertorData> {
27     public SalToOfSetNwSrcActionV10Case() {
28         super(SetNwSrcActionCase.class, true, OFConstants.OFP_VERSION_1_0);
29     }
30
31     @Nonnull
32     @Override
33     public Optional<Action> process(@Nonnull final SetNwSrcActionCase source, final ActionConvertorData data, ConvertorExecutor convertorExecutor) {
34         final ActionBuilder builder = new ActionBuilder();
35         final Address address = source.getSetNwSrcAction().getAddress();
36
37         if (address instanceof Ipv4) {
38             //FIXME use of substring should be removed and OF models should distinguish where
39             //FIXME to use Ipv4Prefix (with mask) and where to use Ipv4Address (without mask)
40
41             String ipAddress = ((Ipv4) address).getIpv4Address().getValue();
42             ipAddress = ipAddress.substring(0, ipAddress.indexOf("/"));
43             Ipv4Address result = new Ipv4Address(ipAddress);
44             SetNwSrcCaseBuilder nwSrcCaseBuilder = new SetNwSrcCaseBuilder();
45             SetNwSrcActionBuilder nwSrcBuilder = new SetNwSrcActionBuilder();
46             nwSrcBuilder.setIpAddress(new Ipv4Address(result));
47             nwSrcCaseBuilder.setSetNwSrcAction(nwSrcBuilder.build());
48             builder.setActionChoice(nwSrcCaseBuilder.build());
49         } else {
50             throw new IllegalArgumentException("Address is not supported by OF-1.0: " + address.getClass().getName());
51         }
52
53         return Optional.of(builder.build());
54     }
55 }