Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / actions / SetNwSrcActionSerializer.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.impl.protocol.serialization.actions;
10
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
21
22 public class SetNwSrcActionSerializer extends AbstractSetFieldActionSerializer {
23
24     @Override
25     protected SetFieldCase buildAction(Action input) {
26         final Address address = ((SetNwSrcActionCase) input).getSetNwSrcAction().getAddress();
27         final SetFieldBuilder builder = new SetFieldBuilder();
28
29         if (address instanceof Ipv4) {
30             builder.setLayer3Match(new Ipv4MatchBuilder()
31                     .setIpv4Source(((Ipv4) address).getIpv4Address())
32                     .build());
33         } else if (address instanceof Ipv6) {
34             builder.setLayer3Match(new Ipv6MatchBuilder()
35                     .setIpv6Source(((Ipv6) address).getIpv6Address())
36                     .build());
37         }
38
39         return new SetFieldCaseBuilder().setSetField(builder.build()).build();
40     }
41
42 }