Add implementation for flat L3 overlay
[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._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;
38
39 import java.util.Collections;
40
41 public class LoopbackCommand extends AbstractInterfaceCommand {
42
43     private PhysAddress physAddress;
44     private boolean bvi;
45     private IpAddress ipAddress;
46     private IpPrefix ipPrefix;
47
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();
60     }
61
62     public static LoopbackCommandBuilder builder() {
63         return new LoopbackCommandBuilder();
64     }
65
66     PhysAddress getPhysAddress() {
67         return physAddress;
68     }
69
70     public boolean getBvi() {
71         return bvi;
72     }
73
74     IpAddress getIpAddress() {
75         return ipAddress;
76     }
77
78     IpPrefix getIpPrefix() {
79         return ipPrefix;
80     }
81
82     @Override
83     public InterfaceBuilder getInterfaceBuilder() {
84         InterfaceBuilder interfaceBuilder = new InterfaceBuilder().setKey(new InterfaceKey(name))
85                 .setEnabled(enabled)
86                 .setDescription(description)
87                 .setType(Loopback.class)
88                 .setName(name)
89                 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled)
90                 .addAugmentation(NatInterfaceAugmentation.class, buildInboundNatAugmentation());
91
92         // Create the Loopback augmentation
93         VppInterfaceAugmentationBuilder
94                 vppAugmentationBuilder =
95                 new VppInterfaceAugmentationBuilder().setLoopback(new LoopbackBuilder().setMac(this.physAddress).build());
96
97         if (getVrfId() != null) {
98             vppAugmentationBuilder.setRouting(new RoutingBuilder().setIpv4VrfId(getVrfId()).build());
99         }
100
101         if (!Strings.isNullOrEmpty(bridgeDomain)) {
102             vppAugmentationBuilder.setL2(new L2Builder().setInterconnection(
103                     new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).setBridgedVirtualInterface(bvi).build())
104                     .build());
105         }
106         Interface1Builder
107                 interface1Builder =
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())
111                                 .build())).build());
112         interfaceBuilder.addAugmentation(Interface1.class, interface1Builder.build());
113         interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
114         addEnableProxyArpAugmentation(interfaceBuilder);
115         return interfaceBuilder;
116     }
117
118     private NatInterfaceAugmentation buildInboundNatAugmentation() {
119         return new NatInterfaceAugmentationBuilder().setNat(
120                 new NatBuilder().setInbound(new InboundBuilder().build()).build()).build();
121     }
122
123     @Override
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 + "]";
128     }
129
130     private short getPrefixLength() {
131         return (short) NetUtils.getMaskFromPrefix(this.ipPrefix.getIpv4Prefix().getValue());
132     }
133
134     public static class LoopbackCommandBuilder {
135
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;
145         private Long vrfId;
146         private Boolean enableProxyArp;
147
148         public String getInterfaceName() {
149             return interfaceName;
150         }
151
152         public LoopbackCommandBuilder setInterfaceName(String interfaceName) {
153             this.interfaceName = interfaceName;
154             return this;
155         }
156
157         public Operations getOperation() {
158             return operation;
159         }
160
161         public LoopbackCommandBuilder setOperation(Operations operation) {
162             this.operation = operation;
163             return this;
164         }
165
166         PhysAddress getPhysAddress() {
167             return physAddress;
168         }
169
170         public LoopbackCommandBuilder setPhysAddress(PhysAddress physAddress) {
171             this.physAddress = physAddress;
172             return this;
173         }
174
175         public String getBridgeDomain() {
176             return bridgeDomain;
177         }
178
179         public LoopbackCommandBuilder setBridgeDomain(String bridgeDomain) {
180             this.bridgeDomain = bridgeDomain;
181             return this;
182         }
183
184         public String getDescription() {
185             return description;
186         }
187
188         public LoopbackCommandBuilder setDescription(String description) {
189             this.description = description;
190             return this;
191         }
192
193         boolean isEnabled() {
194             return enabled;
195         }
196
197         public LoopbackCommandBuilder setEnabled(boolean enabled) {
198             this.enabled = enabled;
199             return this;
200         }
201
202         IpAddress getIpAddress() {
203             return ipAddress;
204         }
205
206         public LoopbackCommandBuilder setIpAddress(IpAddress ipAddress) {
207             this.ipAddress = ipAddress;
208             return this;
209         }
210
211         IpPrefix getIpPrefix() {
212             return ipPrefix;
213         }
214
215         public LoopbackCommandBuilder setIpPrefix(IpPrefix ipPrefix) {
216             this.ipPrefix = ipPrefix;
217             return this;
218         }
219
220         public boolean isBvi() {
221             return bvi;
222         }
223
224         public LoopbackCommandBuilder setBvi(boolean bvi) {
225             this.bvi = bvi;
226             return this;
227         }
228
229         Long getVrfId() {
230             return vrfId;
231         }
232
233         public LoopbackCommandBuilder setVrfId(Long vrfId) {
234             this.vrfId = vrfId;
235             return this;
236         }
237
238         public Boolean getEnableProxyArp() {
239             return enableProxyArp;
240         }
241
242         public void setEnableProxyArp(Boolean enableProxyArp) {
243             this.enableProxyArp = enableProxyArp;
244         }
245
246         /**
247          * LoopPortCommand build method.
248          *
249          * @return LoopPortCommand
250          * @throws IllegalArgumentException if interfaceName or operation is null.
251          */
252         public LoopbackCommand build() {
253             Preconditions.checkArgument(this.interfaceName != null);
254             Preconditions.checkArgument(this.operation != null);
255
256             return new LoopbackCommand(this);
257         }
258     }
259 }