introducing loopback port for VPP
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / commands / LoopbackCommand.java
1 /*
2  * Copyright (c) 2016 Cisco Systems. 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.groupbasedpolicy.renderer.vpp.commands;
10
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;
33
34 import java.util.Collections;
35
36 public class LoopbackCommand extends AbstractInterfaceCommand<LoopbackCommand> {
37
38     private PhysAddress physAddress;
39     private String bridgeDomain;
40     private boolean bvi;
41     private IpAddress ipAddress;
42     private IpPrefix ipPrefix;
43
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();
54     }
55
56     public static LoopbackCommandBuilder builder() {
57         return new LoopbackCommandBuilder();
58     }
59
60     public PhysAddress getPhysAddress() {
61         return physAddress;
62     }
63
64     public String getBridgeDomain() {
65         return bridgeDomain;
66     }
67
68     public boolean getBvi() {
69         return bvi;
70     }
71
72     public IpAddress getIpAddress() {
73         return ipAddress;
74     }
75
76     public IpPrefix getIpPrefix() {
77         return ipPrefix;
78     }
79
80     public short getPrefixLength() {
81         return (short) NetUtils.getMaskFromPrefix(this.ipPrefix.getIpv4Prefix().getValue());
82     }
83
84     @Override public InterfaceBuilder getInterfaceBuilder() {
85         InterfaceBuilder
86             interfaceBuilder =
87             new InterfaceBuilder().setKey(new InterfaceKey(name))
88                 .setEnabled(enabled)
89                 .setDescription(description)
90                 .setType(Loopback.class)
91                 .setName(name)
92                 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled);
93
94         // Create the Loopback augmentation
95         VppInterfaceAugmentationBuilder
96             vppAugmentationBuilder =
97             new VppInterfaceAugmentationBuilder().setLoopback(new LoopbackBuilder().setMac(this.physAddress).build());
98
99         if (!Strings.isNullOrEmpty(bridgeDomain)) {
100             vppAugmentationBuilder.setL2(new L2Builder().setInterconnection(
101                 new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).setBridgedVirtualInterface(bvi).build())
102                 .build());
103         }
104         Interface1Builder
105             interface1Builder =
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())
109                     .build())).build());
110         interfaceBuilder.addAugmentation(Interface1.class, interface1Builder.build());
111         interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
112         return interfaceBuilder;
113     }
114
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 + "]";
119     }
120
121     public static class LoopbackCommandBuilder {
122
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;
132
133         public String getInterfaceName() {
134             return interfaceName;
135         }
136
137         public LoopbackCommandBuilder setInterfaceName(String interfaceName) {
138             this.interfaceName = interfaceName;
139             return this;
140         }
141
142         public Operations getOperation() {
143             return operation;
144         }
145
146         public LoopbackCommandBuilder setOperation(Operations operation) {
147             this.operation = operation;
148             return this;
149         }
150
151         public PhysAddress getPhysAddress() {
152             return physAddress;
153         }
154
155         public LoopbackCommandBuilder setPhysAddress(PhysAddress physAddress) {
156             this.physAddress = physAddress;
157             return this;
158         }
159
160         public String getBridgeDomain() {
161             return bridgeDomain;
162         }
163
164         public LoopbackCommandBuilder setBridgeDomain(String bridgeDomain) {
165             this.bridgeDomain = bridgeDomain;
166             return this;
167         }
168
169         public String getDescription() {
170             return description;
171         }
172
173         public LoopbackCommandBuilder setDescription(String description) {
174             this.description = description;
175             return this;
176         }
177
178         public boolean isEnabled() {
179             return enabled;
180         }
181
182         public LoopbackCommandBuilder setEnabled(boolean enabled) {
183             this.enabled = enabled;
184             return this;
185         }
186
187         public IpAddress getIpAddress() {
188             return ipAddress;
189         }
190
191         public LoopbackCommandBuilder setIpAddress(IpAddress ipAddress) {
192             this.ipAddress = ipAddress;
193             return this;
194         }
195
196         public IpPrefix getIpPrefix() {
197             return ipPrefix;
198         }
199
200         public LoopbackCommandBuilder setIpPrefix(IpPrefix ipPrefix) {
201             this.ipPrefix = ipPrefix;
202             return this;
203         }
204
205         public boolean isBvi() {
206             return bvi;
207         }
208
209         public LoopbackCommandBuilder setBvi(boolean bvi) {
210             this.bvi = bvi;
211             return this;
212         }
213
214         /**
215          * LoopPortCommand build method.
216          *
217          * @return LoopPortCommand
218          * @throws IllegalArgumentException if interfaceName or operation is null.
219          */
220         public LoopbackCommand build() {
221             Preconditions.checkArgument(this.interfaceName != null);
222             Preconditions.checkArgument(this.operation != null);
223
224             return new LoopbackCommand(this);
225         }
226     }
227 }