2 * Copyright (c) 2016 Cisco Systems. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.commands;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
14 import org.opendaylight.groupbasedpolicy.util.NetUtils;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1Builder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4Builder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLengthBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.InboundBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.Loopback;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.VppInterfaceAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.VppInterfaceAugmentationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.interfaces._interface.L2Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.interfaces._interface.LoopbackBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.interfaces._interface.RoutingBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.l2.base.attributes.interconnection.BridgeBasedBuilder;
39 import java.util.Collections;
41 public class LoopbackCommand extends AbstractInterfaceCommand {
43 private PhysAddress physAddress;
45 private IpAddress ipAddress;
46 private IpPrefix ipPrefix;
48 private LoopbackCommand(LoopbackCommandBuilder builder) {
49 this.name = builder.getInterfaceName();
50 this.operation = builder.getOperation();
51 this.physAddress = builder.getPhysAddress();
52 this.enabled = builder.isEnabled();
53 this.description = builder.getDescription();
54 this.bridgeDomain = builder.getBridgeDomain();
55 this.bvi = builder.isBvi();
56 this.ipAddress = builder.getIpAddress();
57 this.ipPrefix = builder.getIpPrefix();
58 this.vrfId = builder.getVrfId();
59 this.enableProxyArp = builder.getEnableProxyArp();
62 public static LoopbackCommandBuilder builder() {
63 return new LoopbackCommandBuilder();
66 PhysAddress getPhysAddress() {
70 public boolean getBvi() {
74 IpAddress getIpAddress() {
78 IpPrefix getIpPrefix() {
83 public InterfaceBuilder getInterfaceBuilder() {
84 InterfaceBuilder interfaceBuilder = new InterfaceBuilder().setKey(new InterfaceKey(name))
86 .setDescription(description)
87 .setType(Loopback.class)
89 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled)
90 .addAugmentation(NatInterfaceAugmentation.class, buildInboundNatAugmentation());
92 // Create the Loopback augmentation
93 VppInterfaceAugmentationBuilder
94 vppAugmentationBuilder =
95 new VppInterfaceAugmentationBuilder().setLoopback(new LoopbackBuilder().setMac(this.physAddress).build());
97 if (getVrfId() != null) {
98 vppAugmentationBuilder.setRouting(new RoutingBuilder().setIpv4VrfId(getVrfId()).build());
101 if (!Strings.isNullOrEmpty(bridgeDomain)) {
102 vppAugmentationBuilder.setL2(new L2Builder().setInterconnection(
103 new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).setBridgedVirtualInterface(bvi).build())
108 new Interface1Builder().setIpv4(new Ipv4Builder().setAddress(Collections.singletonList(
109 new AddressBuilder().setIp(new Ipv4AddressNoZone(this.ipAddress.getIpv4Address()))
110 .setSubnet(new PrefixLengthBuilder().setPrefixLength(this.getPrefixLength()).build())
112 interfaceBuilder.addAugmentation(Interface1.class, interface1Builder.build());
113 interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
114 addEnableProxyArpAugmentation(interfaceBuilder);
115 return interfaceBuilder;
118 private NatInterfaceAugmentation buildInboundNatAugmentation() {
119 return new NatInterfaceAugmentationBuilder().setNat(
120 new NatBuilder().setInbound(new InboundBuilder().build()).build()).build();
124 public String toString() {
125 return "LoopPortUserCommand [physAddress=" + physAddress + ", IpAddress=" + ipAddress + ", IpPrefix=" + ipPrefix
126 + ", bridgeDomain=" + bridgeDomain + ", operations=" + operation + ", name=" + name + ", description="
127 + description + ", enabled=" + enabled + ", bvi=" + bvi + ", enableProxyArp=" + enableProxyArp + "]";
130 private short getPrefixLength() {
131 return (short) NetUtils.getMaskFromPrefix(this.ipPrefix.getIpv4Prefix().getValue());
134 public static class LoopbackCommandBuilder {
136 private String interfaceName;
137 private Operations operation;
138 private PhysAddress physAddress;
139 private String bridgeDomain;
140 private String description;
141 private boolean bvi = false;
142 private boolean enabled = true;
143 private IpAddress ipAddress;
144 private IpPrefix ipPrefix;
146 private Boolean enableProxyArp;
148 public String getInterfaceName() {
149 return interfaceName;
152 public LoopbackCommandBuilder setInterfaceName(String interfaceName) {
153 this.interfaceName = interfaceName;
157 public Operations getOperation() {
161 public LoopbackCommandBuilder setOperation(Operations operation) {
162 this.operation = operation;
166 PhysAddress getPhysAddress() {
170 public LoopbackCommandBuilder setPhysAddress(PhysAddress physAddress) {
171 this.physAddress = physAddress;
175 public String getBridgeDomain() {
179 public LoopbackCommandBuilder setBridgeDomain(String bridgeDomain) {
180 this.bridgeDomain = bridgeDomain;
184 public String getDescription() {
188 public LoopbackCommandBuilder setDescription(String description) {
189 this.description = description;
193 boolean isEnabled() {
197 public LoopbackCommandBuilder setEnabled(boolean enabled) {
198 this.enabled = enabled;
202 IpAddress getIpAddress() {
206 public LoopbackCommandBuilder setIpAddress(IpAddress ipAddress) {
207 this.ipAddress = ipAddress;
211 IpPrefix getIpPrefix() {
215 public LoopbackCommandBuilder setIpPrefix(IpPrefix ipPrefix) {
216 this.ipPrefix = ipPrefix;
220 public boolean isBvi() {
224 public LoopbackCommandBuilder setBvi(boolean bvi) {
233 public LoopbackCommandBuilder setVrfId(Long vrfId) {
238 public Boolean getEnableProxyArp() {
239 return enableProxyArp;
242 public void setEnableProxyArp(Boolean enableProxyArp) {
243 this.enableProxyArp = enableProxyArp;
247 * LoopPortCommand build method.
249 * @return LoopPortCommand
250 * @throws IllegalArgumentException if interfaceName or operation is null.
252 public LoopbackCommand build() {
253 Preconditions.checkArgument(this.interfaceName != null);
254 Preconditions.checkArgument(this.operation != null);
256 return new LoopbackCommand(this);