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.v3po.rev161214.Loopback;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.LoopbackBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
34 import java.util.Collections;
36 public class LoopbackCommand extends AbstractInterfaceCommand<LoopbackCommand> {
38 private PhysAddress physAddress;
39 private String bridgeDomain;
41 private IpAddress ipAddress;
42 private IpPrefix ipPrefix;
44 private LoopbackCommand(LoopbackCommandBuilder builder) {
45 this.name = builder.getInterfaceName();
46 this.operation = builder.getOperation();
47 this.physAddress = builder.getPhysAddress();
48 this.enabled = builder.isEnabled();
49 this.description = builder.getDescription();
50 this.bridgeDomain = builder.getBridgeDomain();
51 this.bvi = builder.isBvi();
52 this.ipAddress = builder.getIpAddress();
53 this.ipPrefix = builder.getIpPrefix();
56 public static LoopbackCommandBuilder builder() {
57 return new LoopbackCommandBuilder();
60 public PhysAddress getPhysAddress() {
64 public String getBridgeDomain() {
68 public boolean getBvi() {
72 public IpAddress getIpAddress() {
76 public IpPrefix getIpPrefix() {
80 public short getPrefixLength() {
81 return (short) NetUtils.getMaskFromPrefix(this.ipPrefix.getIpv4Prefix().getValue());
84 @Override public InterfaceBuilder getInterfaceBuilder() {
87 new InterfaceBuilder().setKey(new InterfaceKey(name))
89 .setDescription(description)
90 .setType(Loopback.class)
92 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled);
94 // Create the Loopback augmentation
95 VppInterfaceAugmentationBuilder
96 vppAugmentationBuilder =
97 new VppInterfaceAugmentationBuilder().setLoopback(new LoopbackBuilder().setMac(this.physAddress).build());
99 if (!Strings.isNullOrEmpty(bridgeDomain)) {
100 vppAugmentationBuilder.setL2(new L2Builder().setInterconnection(
101 new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).setBridgedVirtualInterface(bvi).build())
106 new Interface1Builder().setIpv4(new Ipv4Builder().setAddress(Collections.singletonList(
107 new AddressBuilder().setIp(new Ipv4AddressNoZone(this.ipAddress.getIpv4Address()))
108 .setSubnet(new PrefixLengthBuilder().setPrefixLength(this.getPrefixLength()).build())
110 interfaceBuilder.addAugmentation(Interface1.class, interface1Builder.build());
111 interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
112 return interfaceBuilder;
115 @Override public String toString() {
116 return "LoopPortUserCommand [physAddress=" + physAddress + ", IpAddress=" + ipAddress + ", IpPrefix=" + ipPrefix
117 + ", bridgeDomain=" + bridgeDomain + ", operations=" + operation + ", name=" + name + ", description="
118 + description + ", enabled=" + enabled + ", bvi=" + bvi + "]";
121 public static class LoopbackCommandBuilder {
123 private String interfaceName;
124 private Operations operation;
125 private PhysAddress physAddress;
126 private String bridgeDomain;
127 private String description;
128 private boolean bvi = false;
129 private boolean enabled = true;
130 private IpAddress ipAddress;
131 private IpPrefix ipPrefix;
133 public String getInterfaceName() {
134 return interfaceName;
137 public LoopbackCommandBuilder setInterfaceName(String interfaceName) {
138 this.interfaceName = interfaceName;
142 public Operations getOperation() {
146 public LoopbackCommandBuilder setOperation(Operations operation) {
147 this.operation = operation;
151 public PhysAddress getPhysAddress() {
155 public LoopbackCommandBuilder setPhysAddress(PhysAddress physAddress) {
156 this.physAddress = physAddress;
160 public String getBridgeDomain() {
164 public LoopbackCommandBuilder setBridgeDomain(String bridgeDomain) {
165 this.bridgeDomain = bridgeDomain;
169 public String getDescription() {
173 public LoopbackCommandBuilder setDescription(String description) {
174 this.description = description;
178 public boolean isEnabled() {
182 public LoopbackCommandBuilder setEnabled(boolean enabled) {
183 this.enabled = enabled;
187 public IpAddress getIpAddress() {
191 public LoopbackCommandBuilder setIpAddress(IpAddress ipAddress) {
192 this.ipAddress = ipAddress;
196 public IpPrefix getIpPrefix() {
200 public LoopbackCommandBuilder setIpPrefix(IpPrefix ipPrefix) {
201 this.ipPrefix = ipPrefix;
205 public boolean isBvi() {
209 public LoopbackCommandBuilder setBvi(boolean bvi) {
215 * LoopPortCommand build method.
217 * @return LoopPortCommand
218 * @throws IllegalArgumentException if interfaceName or operation is null.
220 public LoopbackCommand build() {
221 Preconditions.checkArgument(this.interfaceName != null);
222 Preconditions.checkArgument(this.operation != null);
224 return new LoopbackCommand(this);