001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.anarres.qemu.exec;
006
007import com.google.common.base.Preconditions;
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011import javax.annotation.Nonnull;
012import org.anarres.qemu.exec.recipe.QEmuVirtioNetRecipe;
013
014/**
015 * A network device backend, usually paired with a {@link QEmuDeviceOption} frontend.
016 *
017 * If you are looking for the equivalent of "-net nic,...", it is a frontend
018 * specification now performed using -device.
019 *
020 * @see QEmuVirtioNetRecipe
021 * @see QEmuDeviceOption
022 * @author shevek
023 */
024public class QEmuNetdevOption extends AbstractQEmuOption {
025
026    // tap,fd=29,id=hostnet1,vhost=on,vhostfd=30
027    private final String name;
028    public String id;
029    private final Map<String, String> properties = new HashMap<String, String>();
030
031    public QEmuNetdevOption(@Nonnull String name) {
032        this.name = name;
033    }
034
035    @Nonnull
036    public QEmuNetdevOption withId(@Nonnull String id) {
037        this.id = id;
038        return this;
039    }
040
041    @Nonnull
042    public QEmuNetdevOption withProperties(@Nonnull Map<String, String> properties) {
043        this.properties.putAll(properties);
044        return this;
045    }
046
047    @Nonnull
048    public QEmuNetdevOption withProperty(@Nonnull String key, @Nonnull String value) {
049        properties.put(key, value);
050        return this;
051    }
052
053    @Nonnull
054    public QEmuNetdevOption withProperty(@Nonnull String key) {
055        properties.put(key, null);
056        return this;
057    }
058
059    @Override
060    public void appendTo(List<? super String> line) {
061        StringBuilder buf = new StringBuilder(name);
062        appendTo(buf, "id", Preconditions.checkNotNull(id, "No id specified in -netdev"));
063        appendTo(buf, properties);
064        add(line, "-netdev", buf);
065    }
066
067    public static class User extends QEmuNetdevOption {
068
069        public User() {
070            super("user");
071        }
072    }
073
074    public static class Tap extends QEmuNetdevOption {
075
076        public static final String PROP_IFNAME = "ifname";
077
078        public Tap() {
079            super("tap");
080        }
081
082        @Nonnull
083        public Tap withTapInterface(@Nonnull String ifname) {
084            withProperty(PROP_IFNAME, ifname);
085            return this;
086        }
087    }
088
089    public static class Bridge extends QEmuNetdevOption {
090
091        public Bridge() {
092            super("bridge");
093        }
094    }
095
096    public static class Socket extends QEmuNetdevOption {
097
098        public Socket() {
099            super("socket");
100        }
101    }
102
103    public static class Hubport extends QEmuNetdevOption {
104
105        public Hubport() {
106            super("hubport");
107        }
108    }
109
110    public static class Dump extends QEmuNetdevOption {
111
112        public Dump() {
113            super("dump");
114        }
115    }
116
117    public static class None extends QEmuNetdevOption {
118
119        public None() {
120            super("none");
121        }
122    }
123
124}