2 * Copyright (c) 2016 Cisco Systems. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.commands;
\r
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
\r
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
\r
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
\r
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
\r
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
\r
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
\r
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentationBuilder;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2Builder;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBasedBuilder;
\r
25 import org.slf4j.Logger;
\r
26 import org.slf4j.LoggerFactory;
\r
28 import com.google.common.base.Preconditions;
\r
29 import com.google.common.base.Strings;
\r
31 public class VhostUserCommand extends AbstractInterfaceCommand<VhostUserCommand> {
\r
33 private static final Logger LOG = LoggerFactory.getLogger(VhostUserCommand.class);
\r
34 private String socket;
\r
35 private VhostUserRole role;
\r
36 private String bridgeDomain;
\r
38 private VhostUserCommand(VhostUserCommandBuilder builder) {
\r
39 this.name = builder.getName();
\r
40 this.operation = builder.getOperation();
\r
41 this.socket = builder.getSocket();
\r
42 this.role = builder.getRole();
\r
43 this.enabled = builder.isEnabled();
\r
44 this.description = builder.getDescription();
\r
45 this.bridgeDomain = builder.getBridgeDomain();
\r
49 public static VhostUserCommandBuilder builder() {
\r
50 return new VhostUserCommandBuilder();
\r
53 public String getSocket() {
\r
57 public VhostUserRole getRole() {
\r
61 public String getBridgeDomain() {
\r
62 return bridgeDomain;
\r
66 public void execute(ReadWriteTransaction readWriteTransaction) {
\r
67 switch (getOperation()) {
\r
70 LOG.debug("Executing Add operation for command: {}", this);
\r
71 put(readWriteTransaction);
\r
74 LOG.debug("Executing Delete operation for command: {}", this);
\r
75 delete(readWriteTransaction);
\r
78 LOG.debug("Executing Update operation for command: {}", this);
\r
79 merge(readWriteTransaction);
\r
82 LOG.error("Execution failed for command: {}", this);
\r
87 private void put(ReadWriteTransaction readWriteTransaction) {
\r
88 InterfaceBuilder interfaceBuilder = getVhostUserInterfaceBuilder();
\r
90 readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION,
\r
91 VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()), interfaceBuilder.build(), true);
\r
94 private void merge(ReadWriteTransaction readWriteTransaction) {
\r
95 InterfaceBuilder interfaceBuilder = getVhostUserInterfaceBuilder();
\r
97 readWriteTransaction.merge(LogicalDatastoreType.CONFIGURATION,
\r
98 VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()), interfaceBuilder.build());
\r
101 private InterfaceBuilder getVhostUserInterfaceBuilder() {
\r
102 InterfaceBuilder interfaceBuilder =
\r
103 new InterfaceBuilder().setKey(new InterfaceKey(name))
\r
104 .setEnabled(enabled)
\r
105 .setDescription(description)
\r
107 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class)
\r
109 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled);
\r
111 // Create the vhost augmentation
\r
112 VppInterfaceAugmentationBuilder vppAugmentationBuilder = new VppInterfaceAugmentationBuilder()
\r
113 .setVhostUser(new VhostUserBuilder().setRole(role).setSocket(socket).build());
\r
114 if (!Strings.isNullOrEmpty(bridgeDomain)) {
\r
115 vppAugmentationBuilder.setL2(new L2Builder()
\r
116 .setInterconnection(new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).build()).build());
\r
119 interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentationBuilder.build());
\r
120 return interfaceBuilder;
\r
123 private void delete(ReadWriteTransaction readWriteTransaction) {
\r
125 readWriteTransaction.delete(LogicalDatastoreType.CONFIGURATION,
\r
126 VppIidFactory.getInterfaceIID(new InterfaceKey(name)));
\r
127 } catch (IllegalStateException ex) {
\r
128 LOG.debug("Vhost Interface is not present in DS", ex);
\r
134 public String toString() {
\r
135 return "VhostUserCommand [socket=" + socket + ", role=" + role + ", bridgeDomain=" + bridgeDomain
\r
136 + ", operation=" + operation + ", name=" + name + ", description=" + description + ", enabled="
\r
142 public static class VhostUserCommandBuilder {
\r
144 private String name;
\r
145 private General.Operations operation;
\r
146 private String socket;
\r
147 private VhostUserRole role = VhostUserRole.Server;
\r
148 private boolean enabled = true;
\r
149 private String description;
\r
150 private String bridgeDomain;
\r
152 public String getName() {
\r
156 public VhostUserCommandBuilder setName(String name) {
\r
161 public General.Operations getOperation() {
\r
165 public VhostUserCommandBuilder setOperation(General.Operations operation) {
\r
166 this.operation = operation;
\r
170 public String getSocket() {
\r
174 public VhostUserCommandBuilder setSocket(String socket) {
\r
175 this.socket = socket;
\r
179 public VhostUserRole getRole() {
\r
183 public VhostUserCommandBuilder setRole(VhostUserRole role) {
\r
188 public boolean isEnabled() {
\r
192 public VhostUserCommandBuilder setEnabled(boolean enabled) {
\r
193 this.enabled = enabled;
\r
197 public String getDescription() {
\r
198 return description;
\r
201 public VhostUserCommandBuilder setDescription(String description) {
\r
202 this.description = description;
\r
206 public String getBridgeDomain() {
\r
207 return bridgeDomain;
\r
210 public VhostUserCommandBuilder setBridgeDomain(String bridgeDomain) {
\r
211 this.bridgeDomain = bridgeDomain;
\r
216 * VhostUserCommand build method.
\r
218 * @return VhostUserCommand
\r
219 * @throws IllegalArgumentException if name, operation or socket is null.
\r
221 public VhostUserCommand build() {
\r
222 Preconditions.checkArgument(this.name != null);
\r
223 Preconditions.checkArgument(this.operation != null);
\r
224 if (operation == Operations.PUT) {
\r
225 Preconditions.checkArgument(this.socket != null);
\r
228 return new VhostUserCommand(this);
\r