c5a53c170744068f90aee2c605f9d194afb06db8
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / commands / VhostUserCommand.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;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VhostUserRole;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VppInterfaceAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VppInterfaceAugmentationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.L2Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.RoutingBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.interfaces._interface.VhostUserBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.l2.config.attributes.interconnection.BridgeBasedBuilder;
25
26 public class VhostUserCommand extends AbstractInterfaceCommand {
27
28     private String socket;
29     private VhostUserRole role;
30
31     private VhostUserCommand(VhostUserCommandBuilder builder) {
32         this.name = builder.getName();
33         this.operation = builder.getOperation();
34         this.socket = builder.getSocket();
35         this.role = builder.getRole();
36         this.enabled = builder.isEnabled();
37         this.description = builder.getDescription();
38         this.bridgeDomain = builder.getBridgeDomain();
39         this.enableProxyArp = builder.getEnableProxyArp();
40
41     }
42
43     public static VhostUserCommandBuilder builder() {
44         return new VhostUserCommandBuilder();
45     }
46
47     public String getSocket() {
48         return socket;
49     }
50
51     public VhostUserRole getRole() {
52         return role;
53     }
54
55     @Override
56     public InterfaceBuilder getInterfaceBuilder() {
57         InterfaceBuilder interfaceBuilder =
58                 new InterfaceBuilder().setKey(new InterfaceKey(name))
59                         .setEnabled(enabled)
60                         .setDescription(description)
61                         .setType(
62                             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VhostUser.class)
63                         .setName(name)
64                         .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled);
65
66         // Create the vhost augmentation
67         VppInterfaceAugmentationBuilder vppAugmentationBuilder = new VppInterfaceAugmentationBuilder()
68                 .setVhostUser(new VhostUserBuilder().setRole(role).setSocket(socket).build());
69
70         if (getVrfId() != null) {
71             vppAugmentationBuilder.setRouting(new RoutingBuilder().setIpv4VrfId(getVrfId()).build());
72         }
73
74         if (!Strings.isNullOrEmpty(bridgeDomain)) {
75             vppAugmentationBuilder.setL2(new L2Builder()
76                     .setInterconnection(new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).build()).build());
77         }
78
79         interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
80         addEnableProxyArpAugmentation(interfaceBuilder);
81         return interfaceBuilder;
82     }
83
84     @Override
85     public String toString() {
86         return "VhostUserCommand [socket=" + socket + ", role=" + role + ", bridgeDomain=" + bridgeDomain
87                 + ", operation=" + operation + ", name=" + name + ", description=" + description + ", enabled="
88                 + enabled + "]";
89     }
90
91     public static class VhostUserCommandBuilder {
92
93         private String name;
94         private General.Operations operation;
95         private String socket;
96         private VhostUserRole role = VhostUserRole.Server;
97         private boolean enabled = true;
98         private String description;
99         private String bridgeDomain;
100         private Boolean enableProxyArp;
101         private Long vrfId;
102
103         public String getName() {
104             return name;
105         }
106
107         public VhostUserCommandBuilder setName(String name) {
108             this.name = name;
109             return this;
110         }
111
112         public General.Operations getOperation() {
113             return operation;
114         }
115
116         public VhostUserCommandBuilder setOperation(General.Operations operation) {
117             this.operation = operation;
118             return this;
119         }
120
121         public String getSocket() {
122             return socket;
123         }
124
125         public VhostUserCommandBuilder setSocket(String socket) {
126             this.socket = socket;
127             return this;
128         }
129
130         VhostUserRole getRole() {
131             return role;
132         }
133
134         public VhostUserCommandBuilder setRole(VhostUserRole role) {
135             this.role = role;
136             return this;
137         }
138
139         boolean isEnabled() {
140             return enabled;
141         }
142
143         VhostUserCommandBuilder setEnabled(boolean enabled) {
144             this.enabled = enabled;
145             return this;
146         }
147
148         public String getDescription() {
149             return description;
150         }
151
152         public VhostUserCommandBuilder setDescription(String description) {
153             this.description = description;
154             return this;
155         }
156
157         String getBridgeDomain() {
158             return bridgeDomain;
159         }
160
161         VhostUserCommandBuilder setBridgeDomain(String bridgeDomain) {
162             this.bridgeDomain = bridgeDomain;
163             return this;
164         }
165
166         public Boolean getEnableProxyArp() {
167             return enableProxyArp;
168         }
169
170         public void setEnableProxyArp(Boolean enableProxyArp) {
171             this.enableProxyArp = enableProxyArp;
172         }
173
174         public Long getVrfId() {
175             return vrfId;
176         }
177
178         public void setVrfId(Long vrfId) {
179             this.vrfId = vrfId;
180         }
181
182         /**
183          * VhostUserCommand build method.
184          *
185          * @return VhostUserCommand
186          * @throws IllegalArgumentException if name, operation or socket is null.
187          */
188         public VhostUserCommand build() {
189             Preconditions.checkArgument(this.name != null);
190             Preconditions.checkArgument(this.operation != null);
191             if (operation == Operations.PUT) {
192                 Preconditions.checkArgument(this.socket != null);
193             }
194
195             return new VhostUserCommand(this);
196         }
197     }
198 }