d58c76f0c6e655a27146ce9297996af3a9faab83
[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
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
15 import org.opendaylight.groupbasedpolicy.util.NetUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1Builder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLengthBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.InboundBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Loopback;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.LoopbackBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
39
40 import java.util.Collections;
41
42 public class LoopbackCommand extends AbstractInterfaceCommand<LoopbackCommand> {
43
44     private PhysAddress physAddress;
45     private String bridgeDomain;
46     private boolean bvi;
47     private IpAddress ipAddress;
48     private IpPrefix ipPrefix;
49
50     private LoopbackCommand(LoopbackCommandBuilder builder) {
51         this.name = builder.getInterfaceName();
52         this.operation = builder.getOperation();
53         this.physAddress = builder.getPhysAddress();
54         this.enabled = builder.isEnabled();
55         this.description = builder.getDescription();
56         this.bridgeDomain = builder.getBridgeDomain();
57         this.bvi = builder.isBvi();
58         this.ipAddress = builder.getIpAddress();
59         this.ipPrefix = builder.getIpPrefix();
60     }
61
62     public static LoopbackCommandBuilder builder() {
63         return new LoopbackCommandBuilder();
64     }
65
66     public PhysAddress getPhysAddress() {
67         return physAddress;
68     }
69
70     public String getBridgeDomain() {
71         return bridgeDomain;
72     }
73
74     public boolean getBvi() {
75         return bvi;
76     }
77
78     public IpAddress getIpAddress() {
79         return ipAddress;
80     }
81
82     public IpPrefix getIpPrefix() {
83         return ipPrefix;
84     }
85
86     public short getPrefixLength() {
87         return (short) NetUtils.getMaskFromPrefix(this.ipPrefix.getIpv4Prefix().getValue());
88     }
89
90     @Override
91     public InterfaceBuilder getInterfaceBuilder() {
92         InterfaceBuilder interfaceBuilder = new InterfaceBuilder().setKey(new InterfaceKey(name))
93             .setEnabled(enabled)
94             .setDescription(description)
95             .setType(Loopback.class)
96             .setName(name)
97             .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled)
98             .addAugmentation(NatInterfaceAugmentation.class, buildInboundNatAugmentation());
99
100         // Create the Loopback augmentation
101         VppInterfaceAugmentationBuilder
102             vppAugmentationBuilder =
103             new VppInterfaceAugmentationBuilder().setLoopback(new LoopbackBuilder().setMac(this.physAddress).build());
104
105         if (!Strings.isNullOrEmpty(bridgeDomain)) {
106             vppAugmentationBuilder.setL2(new L2Builder().setInterconnection(
107                 new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).setBridgedVirtualInterface(bvi).build())
108                 .build());
109         }
110         Interface1Builder
111             interface1Builder =
112             new Interface1Builder().setIpv4(new Ipv4Builder().setAddress(Collections.singletonList(
113                 new AddressBuilder().setIp(new Ipv4AddressNoZone(this.ipAddress.getIpv4Address()))
114                     .setSubnet(new PrefixLengthBuilder().setPrefixLength(this.getPrefixLength()).build())
115                     .build())).build());
116         interfaceBuilder.addAugmentation(Interface1.class, interface1Builder.build());
117         interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
118         return interfaceBuilder;
119     }
120
121     private NatInterfaceAugmentation buildInboundNatAugmentation() {
122         return new NatInterfaceAugmentationBuilder().setNat(
123                 new NatBuilder().setInbound(new InboundBuilder().build()).build()).build();
124     }
125
126     @Override public String toString() {
127         return "LoopPortUserCommand [physAddress=" + physAddress + ", IpAddress=" + ipAddress + ", IpPrefix=" + ipPrefix
128             + ", bridgeDomain=" + bridgeDomain + ", operations=" + operation + ", name=" + name + ", description="
129             + description + ", enabled=" + enabled + ", bvi=" + bvi + "]";
130     }
131
132     public static class LoopbackCommandBuilder {
133
134         private String interfaceName;
135         private Operations operation;
136         private PhysAddress physAddress;
137         private String bridgeDomain;
138         private String description;
139         private boolean bvi = false;
140         private boolean enabled = true;
141         private IpAddress ipAddress;
142         private IpPrefix ipPrefix;
143
144         public String getInterfaceName() {
145             return interfaceName;
146         }
147
148         public LoopbackCommandBuilder setInterfaceName(String interfaceName) {
149             this.interfaceName = interfaceName;
150             return this;
151         }
152
153         public Operations getOperation() {
154             return operation;
155         }
156
157         public LoopbackCommandBuilder setOperation(Operations operation) {
158             this.operation = operation;
159             return this;
160         }
161
162         public PhysAddress getPhysAddress() {
163             return physAddress;
164         }
165
166         public LoopbackCommandBuilder setPhysAddress(PhysAddress physAddress) {
167             this.physAddress = physAddress;
168             return this;
169         }
170
171         public String getBridgeDomain() {
172             return bridgeDomain;
173         }
174
175         public LoopbackCommandBuilder setBridgeDomain(String bridgeDomain) {
176             this.bridgeDomain = bridgeDomain;
177             return this;
178         }
179
180         public String getDescription() {
181             return description;
182         }
183
184         public LoopbackCommandBuilder setDescription(String description) {
185             this.description = description;
186             return this;
187         }
188
189         public boolean isEnabled() {
190             return enabled;
191         }
192
193         public LoopbackCommandBuilder setEnabled(boolean enabled) {
194             this.enabled = enabled;
195             return this;
196         }
197
198         public IpAddress getIpAddress() {
199             return ipAddress;
200         }
201
202         public LoopbackCommandBuilder setIpAddress(IpAddress ipAddress) {
203             this.ipAddress = ipAddress;
204             return this;
205         }
206
207         public IpPrefix getIpPrefix() {
208             return ipPrefix;
209         }
210
211         public LoopbackCommandBuilder setIpPrefix(IpPrefix ipPrefix) {
212             this.ipPrefix = ipPrefix;
213             return this;
214         }
215
216         public boolean isBvi() {
217             return bvi;
218         }
219
220         public LoopbackCommandBuilder setBvi(boolean bvi) {
221             this.bvi = bvi;
222             return this;
223         }
224
225         /**
226          * LoopPortCommand build method.
227          *
228          * @return LoopPortCommand
229          * @throws IllegalArgumentException if interfaceName or operation is null.
230          */
231         public LoopbackCommand build() {
232             Preconditions.checkArgument(this.interfaceName != null);
233             Preconditions.checkArgument(this.operation != null);
234
235             return new LoopbackCommand(this);
236         }
237     }
238 }