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 com.google.common.base.Preconditions;
\r
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
\r
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
\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.interfaces._interface.l2.interconnection.BridgeBasedBuilder;
\r
25 import org.slf4j.Logger;
\r
26 import org.slf4j.LoggerFactory;
\r
28 public class VhostUserCommand extends AbstractInterfaceCommand {
\r
30 private static final Logger LOG = LoggerFactory.getLogger(VhostUserCommand.class);
\r
31 private String socket;
\r
32 private VhostUserRole role;
\r
33 private String bridgeDomain;
\r
35 private VhostUserCommand(VhostUserCommandBuilder builder) {
\r
36 this.name = builder.getName();
\r
37 this.operation = builder.getOperation();
\r
38 this.socket = builder.getSocket();
\r
39 this.role = builder.getRole();
\r
40 this.enabled = builder.isEnabled();
\r
41 this.description = builder.getDescription();
\r
42 this.bridgeDomain = builder.getBridgeDomain();
\r
46 public static VhostUserCommandBuilder builder() {
\r
47 return new VhostUserCommandBuilder();
\r
50 public String getSocket() {
\r
54 public VhostUserRole getRole() {
\r
58 public String getBridgeDomain() {
\r
59 return bridgeDomain;
\r
63 public void execute(ReadWriteTransaction readWriteTransaction) {
\r
64 switch (getOperation()) {
\r
67 LOG.info("Executing Add operation for command: {}", this);
\r
68 put(readWriteTransaction);
\r
71 LOG.info("Executing Delete operation for command: {}", this);
\r
72 delete(readWriteTransaction);
\r
75 LOG.info("Executing Update operation for command: {}", this);
\r
76 merge(readWriteTransaction);
\r
79 LOG.error("Execution failed for command: {}", this);
\r
84 private void put(ReadWriteTransaction readWriteTransaction) {
\r
85 Preconditions.checkNotNull(name, "Interface name should not be null");
\r
87 InterfaceBuilder interfaceBuilder = getVhostUserInterfaceBuilder();
\r
89 readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION,
\r
90 VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()), interfaceBuilder.build(), true);
\r
93 private void merge(ReadWriteTransaction readWriteTransaction) {
\r
94 Preconditions.checkNotNull(name, "Interface name should not be null");
\r
96 InterfaceBuilder interfaceBuilder = getVhostUserInterfaceBuilder();
\r
98 readWriteTransaction.merge(LogicalDatastoreType.CONFIGURATION,
\r
99 VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()), interfaceBuilder.build());
\r
102 private InterfaceBuilder getVhostUserInterfaceBuilder() {
\r
103 InterfaceBuilder interfaceBuilder =
\r
104 new InterfaceBuilder().setKey(new InterfaceKey(name))
\r
105 .setEnabled(enabled)
\r
106 .setDescription(description)
\r
108 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class)
\r
110 .setLinkUpDownTrapEnable(Interface.LinkUpDownTrapEnable.Enabled);
\r
112 // Create the vhost augmentation
\r
113 VppInterfaceAugmentation vppAugmentation =
\r
114 new VppInterfaceAugmentationBuilder()
\r
115 .setVhostUser(new VhostUserBuilder().setRole(role).setSocket(socket).build())
\r
116 .setL2(new L2Builder()
\r
117 .setInterconnection(new BridgeBasedBuilder().setBridgeDomain(bridgeDomain).build()).build())
\r
120 interfaceBuilder.addAugmentation(VppInterfaceAugmentation.class, vppAugmentation);
\r
121 return interfaceBuilder;
\r
124 private void delete(ReadWriteTransaction readWriteTransaction) {
\r
126 readWriteTransaction.delete(LogicalDatastoreType.CONFIGURATION,
\r
127 VppIidFactory.getInterfaceIID(new InterfaceKey(name)));
\r
128 } catch (IllegalStateException ex) {
\r
129 LOG.debug("Vhost Interface is not present in DS", ex);
\r
134 public static class VhostUserCommandBuilder {
\r
136 private String name;
\r
137 private General.Operations operation;
\r
138 private String socket;
\r
139 private VhostUserRole role = VhostUserRole.Server;
\r
140 private boolean enabled = true;
\r
141 private String description;
\r
142 private String bridgeDomain;
\r
144 public String getName() {
\r
148 public VhostUserCommandBuilder setName(String name) {
\r
153 public General.Operations getOperation() {
\r
157 public VhostUserCommandBuilder setOperation(General.Operations operation) {
\r
158 this.operation = operation;
\r
162 public String getSocket() {
\r
166 public VhostUserCommandBuilder setSocket(String socket) {
\r
167 this.socket = socket;
\r
171 public VhostUserRole getRole() {
\r
175 public VhostUserCommandBuilder setRole(VhostUserRole role) {
\r
180 public boolean isEnabled() {
\r
184 public VhostUserCommandBuilder setEnabled(boolean enabled) {
\r
185 this.enabled = enabled;
\r
189 public String getDescription() {
\r
190 return description;
\r
193 public VhostUserCommandBuilder setDescription(String description) {
\r
194 this.description = description;
\r
198 public String getBridgeDomain() {
\r
199 return bridgeDomain;
\r
202 public VhostUserCommandBuilder setBridgeDomain(String bridgeDomain) {
\r
203 this.bridgeDomain = bridgeDomain;
\r
208 * VhostUserCommand build method.
\r
210 * @return VhostUserCommand
\r
211 * @throws IllegalArgumentException if name, operation or socket is null.
\r
213 public VhostUserCommand build() {
\r
214 Preconditions.checkArgument(this.name != null);
\r
215 Preconditions.checkArgument(this.operation != null);
\r
216 Preconditions.checkArgument(this.socket != null);
\r
218 return new VhostUserCommand(this);
\r