001/*
002 * VM-Operator
003 * Copyright (C) 2023,2024 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.vmviewer;
020
021import com.fasterxml.jackson.annotation.JsonGetter;
022import com.fasterxml.jackson.annotation.JsonProperty;
023import com.fasterxml.jackson.core.JsonProcessingException;
024import com.fasterxml.jackson.databind.ObjectMapper;
025import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
026import com.google.gson.JsonObject;
027import com.google.gson.JsonPrimitive;
028import freemarker.core.ParseException;
029import freemarker.template.MalformedTemplateNameException;
030import freemarker.template.Template;
031import freemarker.template.TemplateNotFoundException;
032import io.kubernetes.client.util.Strings;
033import java.io.IOException;
034import java.net.Inet4Address;
035import java.net.Inet6Address;
036import java.net.InetAddress;
037import java.net.UnknownHostException;
038import java.time.Duration;
039import java.util.Base64;
040import java.util.Collections;
041import java.util.HashSet;
042import java.util.List;
043import java.util.Map;
044import java.util.Optional;
045import java.util.ResourceBundle;
046import java.util.Set;
047import java.util.logging.Level;
048import org.bouncycastle.util.Objects;
049import org.jdrupes.json.JsonBeanDecoder;
050import org.jdrupes.json.JsonDecodeException;
051import org.jdrupes.vmoperator.common.K8sDynamicModel;
052import org.jdrupes.vmoperator.common.K8sObserver;
053import org.jdrupes.vmoperator.common.VmDefinitionModel;
054import org.jdrupes.vmoperator.common.VmDefinitionModel.Permission;
055import org.jdrupes.vmoperator.manager.events.ChannelCache;
056import org.jdrupes.vmoperator.manager.events.GetDisplayPassword;
057import org.jdrupes.vmoperator.manager.events.ModifyVm;
058import org.jdrupes.vmoperator.manager.events.ResetVm;
059import org.jdrupes.vmoperator.manager.events.VmChannel;
060import org.jdrupes.vmoperator.manager.events.VmDefChanged;
061import org.jdrupes.vmoperator.util.GsonPtr;
062import org.jgrapes.core.Channel;
063import org.jgrapes.core.Components;
064import org.jgrapes.core.Event;
065import org.jgrapes.core.Manager;
066import org.jgrapes.core.annotation.Handler;
067import org.jgrapes.http.Session;
068import org.jgrapes.util.events.ConfigurationUpdate;
069import org.jgrapes.util.events.KeyValueStoreQuery;
070import org.jgrapes.util.events.KeyValueStoreUpdate;
071import org.jgrapes.webconsole.base.Conlet.RenderMode;
072import org.jgrapes.webconsole.base.ConletBaseModel;
073import org.jgrapes.webconsole.base.ConsoleConnection;
074import org.jgrapes.webconsole.base.ConsoleRole;
075import org.jgrapes.webconsole.base.ConsoleUser;
076import org.jgrapes.webconsole.base.WebConsoleUtils;
077import org.jgrapes.webconsole.base.events.AddConletRequest;
078import org.jgrapes.webconsole.base.events.AddConletType;
079import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
080import org.jgrapes.webconsole.base.events.ConletDeleted;
081import org.jgrapes.webconsole.base.events.ConsoleConfigured;
082import org.jgrapes.webconsole.base.events.ConsolePrepared;
083import org.jgrapes.webconsole.base.events.ConsoleReady;
084import org.jgrapes.webconsole.base.events.DeleteConlet;
085import org.jgrapes.webconsole.base.events.NotifyConletModel;
086import org.jgrapes.webconsole.base.events.NotifyConletView;
087import org.jgrapes.webconsole.base.events.OpenModalDialog;
088import org.jgrapes.webconsole.base.events.RenderConlet;
089import org.jgrapes.webconsole.base.events.RenderConletRequestBase;
090import org.jgrapes.webconsole.base.events.SetLocale;
091import org.jgrapes.webconsole.base.events.UpdateConletType;
092import org.jgrapes.webconsole.base.freemarker.FreeMarkerConlet;
093
094/**
095 * The Class VmViewer.
096 */
097@SuppressWarnings({ "PMD.DataflowAnomalyAnalysis", "PMD.ExcessiveImports",
098    "PMD.CouplingBetweenObjects", "PMD.GodClass", "PMD.TooManyMethods" })
099public class VmViewer extends FreeMarkerConlet<VmViewer.ViewerModel> {
100
101    private static final String VM_NAME_PROPERTY = "vmName";
102    private static final String RENDERED
103        = VmViewer.class.getName() + ".rendered";
104    private static final String PENDING
105        = VmViewer.class.getName() + ".pending";
106    private static final Set<RenderMode> MODES = RenderMode.asSet(
107        RenderMode.Preview, RenderMode.Edit);
108    private static final Set<RenderMode> MODES_FOR_GENERATED = RenderMode.asSet(
109        RenderMode.Preview, RenderMode.StickyPreview);
110    private final ChannelCache<String, VmChannel,
111            VmDefinitionModel> channelManager = new ChannelCache<>();
112    private static ObjectMapper objectMapper
113        = new ObjectMapper().registerModule(new JavaTimeModule());
114    private Class<?> preferredIpVersion = Inet4Address.class;
115    private final Set<String> syncUsers = new HashSet<>();
116    private final Set<String> syncRoles = new HashSet<>();
117
118    /**
119     * The periodically generated update event.
120     */
121    public static class Update extends Event<Void> {
122    }
123
124    /**
125     * Creates a new component with its channel set to the given channel.
126     * 
127     * @param componentChannel the channel that the component's handlers listen
128     * on by default and that {@link Manager#fire(Event, Channel...)}
129     * sends the event to
130     */
131    public VmViewer(Channel componentChannel) {
132        super(componentChannel);
133    }
134
135    /**
136     * Configure the component.
137     *
138     * @param event the event
139     */
140    @SuppressWarnings("unchecked")
141    @Handler
142    public void onConfigurationUpdate(ConfigurationUpdate event) {
143        event.structured(componentPath()).ifPresent(c -> {
144            try {
145                var dispRes = (Map<String, Object>) c
146                    .getOrDefault("displayResource", Collections.emptyMap());
147                switch ((String) dispRes.getOrDefault("preferredIpVersion",
148                    "")) {
149                case "ipv6":
150                    preferredIpVersion = Inet6Address.class;
151                    break;
152                case "ipv4":
153                default:
154                    preferredIpVersion = Inet4Address.class;
155                    break;
156                }
157
158                // Sync
159                for (var entry : (List<Map<String, String>>) c.getOrDefault(
160                    "syncPreviewsFor", Collections.emptyList())) {
161                    if (entry.containsKey("user")) {
162                        syncUsers.add(entry.get("user"));
163                    } else if (entry.containsKey("role")) {
164                        syncRoles.add(entry.get("role"));
165                    }
166                }
167            } catch (ClassCastException e) {
168                logger.config("Malformed configuration: " + e.getMessage());
169            }
170        });
171    }
172
173    private boolean syncPreviews(Session session) {
174        return WebConsoleUtils.userFromSession(session)
175            .filter(u -> syncUsers.contains(u.getName())).isPresent()
176            || WebConsoleUtils.rolesFromSession(session).stream()
177                .filter(cr -> syncRoles.contains(cr.getName())).findAny()
178                .isPresent();
179    }
180
181    /**
182     * On {@link ConsoleReady}, fire the {@link AddConletType}.
183     *
184     * @param event the event
185     * @param channel the channel
186     * @throws TemplateNotFoundException the template not found exception
187     * @throws MalformedTemplateNameException the malformed template name
188     *             exception
189     * @throws ParseException the parse exception
190     * @throws IOException Signals that an I/O exception has occurred.
191     */
192    @Handler
193    public void onConsoleReady(ConsoleReady event, ConsoleConnection channel)
194            throws TemplateNotFoundException, MalformedTemplateNameException,
195            ParseException, IOException {
196        // Add conlet resources to page
197        channel.respond(new AddConletType(type())
198            .setDisplayNames(
199                localizations(channel.supportedLocales(), "conletName"))
200            .addRenderMode(RenderMode.Preview)
201            .addScript(new ScriptResource().setScriptType("module")
202                .setScriptUri(event.renderSupport().conletResource(
203                    type(), "VmViewer-functions.js"))));
204        channel.session().put(RENDERED, new HashSet<>());
205    }
206
207    /**
208     * On console configured.
209     *
210     * @param event the event
211     * @param connection the console connection
212     * @throws InterruptedException the interrupted exception
213     */
214    @Handler
215    @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
216    public void onConsoleConfigured(ConsoleConfigured event,
217            ConsoleConnection connection) throws InterruptedException,
218            IOException {
219        @SuppressWarnings("unchecked")
220        final var rendered = (Set<String>) connection.session().get(RENDERED);
221        connection.session().remove(RENDERED);
222        if (!syncPreviews(connection.session())) {
223            return;
224        }
225
226        boolean foundMissing = false;
227        for (var vmName : accessibleVms(connection)) {
228            if (rendered.contains(vmName)) {
229                continue;
230            }
231            if (!foundMissing) {
232                // Suspending to allow rendering of conlets to be noticed
233                var failSafe = Components.schedule(t -> event.resumeHandling(),
234                    Duration.ofSeconds(1));
235                event.suspendHandling(failSafe::cancel);
236                connection.setAssociated(PENDING, event);
237                foundMissing = true;
238            }
239            fire(new AddConletRequest(event.event().event().renderSupport(),
240                VmViewer.class.getName(),
241                RenderMode.asSet(RenderMode.Preview))
242                    .addProperty(VM_NAME_PROPERTY, vmName),
243                connection);
244        }
245    }
246
247    /**
248     * On console prepared.
249     *
250     * @param event the event
251     * @param connection the connection
252     */
253    @Handler
254    public void onConsolePrepared(ConsolePrepared event,
255            ConsoleConnection connection) {
256        if (syncPreviews(connection.session())) {
257            connection.respond(new UpdateConletType(type()));
258        }
259    }
260
261    private String storagePath(Session session, String conletId) {
262        return "/" + WebConsoleUtils.userFromSession(session)
263            .map(ConsoleUser::getName).orElse("")
264            + "/" + VmViewer.class.getName() + "/" + conletId;
265    }
266
267    @Override
268    protected Optional<ViewerModel> createNewState(AddConletRequest event,
269            ConsoleConnection connection, String conletId) throws Exception {
270        var model = new ViewerModel(conletId);
271        model.vmName = (String) event.properties().get(VM_NAME_PROPERTY);
272        if (model.vmName != null) {
273            model.setGenerated(true);
274        }
275        String jsonState = objectMapper.writeValueAsString(model);
276        connection.respond(new KeyValueStoreUpdate().update(
277            storagePath(connection.session(), model.getConletId()), jsonState));
278        return Optional.of(model);
279    }
280
281    @Override
282    protected Optional<ViewerModel> createStateRepresentation(Event<?> event,
283            ConsoleConnection connection, String conletId) throws Exception {
284        var model = new ViewerModel(conletId);
285        String jsonState = objectMapper.writeValueAsString(model);
286        connection.respond(new KeyValueStoreUpdate().update(
287            storagePath(connection.session(), model.getConletId()), jsonState));
288        return Optional.of(model);
289    }
290
291    @Override
292    @SuppressWarnings("PMD.EmptyCatchBlock")
293    protected Optional<ViewerModel> recreateState(Event<?> event,
294            ConsoleConnection channel, String conletId) throws Exception {
295        KeyValueStoreQuery query = new KeyValueStoreQuery(
296            storagePath(channel.session(), conletId), channel);
297        newEventPipeline().fire(query, channel);
298        try {
299            if (!query.results().isEmpty()) {
300                var json = query.results().get(0).values().stream().findFirst()
301                    .get();
302                ViewerModel model
303                    = objectMapper.readValue(json, ViewerModel.class);
304                return Optional.of(model);
305            }
306        } catch (InterruptedException e) {
307            // Means we have no result.
308        }
309
310        // Fall back to creating default state.
311        return createStateRepresentation(event, channel, conletId);
312    }
313
314    @Override
315    @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "unchecked" })
316    protected Set<RenderMode> doRenderConlet(RenderConletRequestBase<?> event,
317            ConsoleConnection channel, String conletId, ViewerModel model)
318            throws Exception {
319        ResourceBundle resourceBundle = resourceBundle(channel.locale());
320        Set<RenderMode> renderedAs = new HashSet<>();
321        if (event.renderAs().contains(RenderMode.Preview)) {
322            channel.associated(PENDING, Event.class)
323                .ifPresent(e -> {
324                    e.resumeHandling();
325                    channel.setAssociated(PENDING, null);
326                });
327
328            // Remove conlet if definition has been removed
329            if (model.vmName() != null
330                && !channelManager.associated(model.vmName()).isPresent()) {
331                channel.respond(
332                    new DeleteConlet(conletId, Collections.emptySet()));
333                return Collections.emptySet();
334            }
335
336            // Don't render if user has not at least one permission
337            if (model.vmName() != null
338                && channelManager.associated(model.vmName())
339                    .map(d -> permissions(d, channel.session()).isEmpty())
340                    .orElse(true)) {
341                return Collections.emptySet();
342            }
343
344            // Render
345            Template tpl
346                = freemarkerConfig().getTemplate("VmViewer-preview.ftl.html");
347            channel.respond(new RenderConlet(type(), conletId,
348                processTemplate(event, tpl,
349                    fmModel(event, channel, conletId, model)))
350                        .setRenderAs(
351                            RenderMode.Preview.addModifiers(event.renderAs()))
352                        .setSupportedModes(
353                            model.isGenerated() ? MODES_FOR_GENERATED : MODES));
354            renderedAs.add(RenderMode.Preview);
355            if (!Strings.isNullOrEmpty(model.vmName())) {
356                Optional.ofNullable(channel.session().get(RENDERED))
357                    .ifPresent(s -> ((Set<String>) s).add(model.vmName()));
358                updateConfig(channel, model);
359            }
360        }
361        if (event.renderAs().contains(RenderMode.Edit)) {
362            Template tpl = freemarkerConfig()
363                .getTemplate("VmViewer-edit.ftl.html");
364            var fmModel = fmModel(event, channel, conletId, model);
365            fmModel.put("vmNames", accessibleVms(channel));
366            channel.respond(new OpenModalDialog(type(), conletId,
367                processTemplate(event, tpl, fmModel))
368                    .addOption("cancelable", true)
369                    .addOption("okayLabel",
370                        resourceBundle.getString("okayLabel")));
371        }
372        return renderedAs;
373    }
374
375    private List<String> accessibleVms(ConsoleConnection channel) {
376        return channelManager.associated().stream()
377            .filter(d -> !permissions(d, channel.session()).isEmpty())
378            .map(d -> d.getMetadata().getName()).sorted().toList();
379    }
380
381    private Set<Permission> permissions(VmDefinitionModel vmDef,
382            Session session) {
383        var user = WebConsoleUtils.userFromSession(session)
384            .map(ConsoleUser::getName).orElse(null);
385        var roles = WebConsoleUtils.rolesFromSession(session)
386            .stream().map(ConsoleRole::getName).toList();
387        return vmDef.permissionsFor(user, roles);
388    }
389
390    private void updateConfig(ConsoleConnection channel, ViewerModel model) {
391        channel.respond(new NotifyConletView(type(),
392            model.getConletId(), "updateConfig", model.vmName()));
393        updateVmDef(channel, model);
394    }
395
396    private void updateVmDef(ConsoleConnection channel, ViewerModel model) {
397        if (Strings.isNullOrEmpty(model.vmName())) {
398            return;
399        }
400        channelManager.associated(model.vmName()).ifPresent(vmDef -> {
401            try {
402                var def = JsonBeanDecoder.create(vmDef.data().toString())
403                    .readObject();
404                def.setField("userPermissions",
405                    permissions(vmDef, channel.session()).stream()
406                        .map(Permission::toString).toList());
407                channel.respond(new NotifyConletView(type(),
408                    model.getConletId(), "updateVmDefinition", def));
409            } catch (JsonDecodeException e) {
410                logger.log(Level.SEVERE, e,
411                    () -> "Failed to serialize VM definition");
412            }
413        });
414    }
415
416    @Override
417    protected void doConletDeleted(ConletDeleted event,
418            ConsoleConnection channel, String conletId, ViewerModel conletState)
419            throws Exception {
420        if (event.renderModes().isEmpty()) {
421            channel.respond(new KeyValueStoreUpdate().delete(
422                storagePath(channel.session(), conletId)));
423        }
424    }
425
426    /**
427     * Track the VM definitions.
428     *
429     * @param event the event
430     * @param channel the channel
431     * @throws JsonDecodeException the json decode exception
432     * @throws IOException 
433     */
434    @Handler(namedChannels = "manager")
435    @SuppressWarnings({ "PMD.ConfusingTernary", "PMD.CognitiveComplexity",
436        "PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidDuplicateLiterals",
437        "PMD.ConfusingArgumentToVarargsMethod" })
438    public void onVmDefChanged(VmDefChanged event, VmChannel channel)
439            throws JsonDecodeException, IOException {
440        var vmDef = new VmDefinitionModel(channel.client().getJSON()
441            .getGson(), event.vmDefinition().data());
442        GsonPtr.to(vmDef.data()).to("metadata").get(JsonObject.class)
443            .remove("managedFields");
444        var vmName = vmDef.getMetadata().getName();
445        if (event.type() == K8sObserver.ResponseType.DELETED) {
446            channelManager.remove(vmName);
447        } else {
448            channelManager.put(vmName, channel, vmDef);
449        }
450        for (var entry : conletIdsByConsoleConnection().entrySet()) {
451            var connection = entry.getKey();
452            for (var conletId : entry.getValue()) {
453                var model = stateFromSession(connection.session(), conletId);
454                if (model.isEmpty()
455                    || !Objects.areEqual(model.get().vmName(), vmName)) {
456                    continue;
457                }
458                if (event.type() == K8sObserver.ResponseType.DELETED) {
459                    connection.respond(
460                        new DeleteConlet(conletId, Collections.emptySet()));
461                } else {
462                    updateVmDef(connection, model.get());
463                }
464            }
465        }
466    }
467
468    @Override
469    @SuppressWarnings({ "PMD.AvoidDecimalLiteralsInBigDecimalConstructor",
470        "PMD.ConfusingArgumentToVarargsMethod", "PMD.NcssCount",
471        "PMD.AvoidLiteralsInIfCondition" })
472    protected void doUpdateConletState(NotifyConletModel event,
473            ConsoleConnection channel, ViewerModel model)
474            throws Exception {
475        event.stop();
476        if ("selectedVm".equals(event.method())) {
477            selectVm(event, channel, model);
478            return;
479        }
480
481        // Handle command for selected VM
482        var both = Optional.ofNullable(model.vmName())
483            .flatMap(vm -> channelManager.both(vm));
484        if (both.isEmpty()) {
485            return;
486        }
487        var vmChannel = both.get().channel;
488        var vmDef = both.get().associated;
489        var vmName = vmDef.metadata().getName();
490        var perms = permissions(vmDef, channel.session());
491        var resourceBundle = resourceBundle(channel.locale());
492        switch (event.method()) {
493        case "start":
494            if (perms.contains(Permission.START)) {
495                fire(new ModifyVm(vmName, "state", "Running", vmChannel));
496            }
497            break;
498        case "stop":
499            if (perms.contains(Permission.STOP)) {
500                fire(new ModifyVm(vmName, "state", "Stopped", vmChannel));
501            }
502            break;
503        case "reset":
504            if (perms.contains(Permission.RESET)) {
505                confirmReset(event, channel, model, resourceBundle);
506            }
507            break;
508        case "resetConfirmed":
509            if (perms.contains(Permission.RESET)) {
510                fire(new ResetVm(vmName), vmChannel);
511            }
512            break;
513        case "openConsole":
514            if (perms.contains(Permission.ACCESS_CONSOLE)) {
515                var pwQuery = Event.onCompletion(new GetDisplayPassword(vmDef),
516                    e -> openConsole(vmName, channel, model,
517                        e.password().orElse(null)));
518                fire(pwQuery, vmChannel);
519            }
520            break;
521        default:// ignore
522            break;
523        }
524    }
525
526    private void selectVm(NotifyConletModel event, ConsoleConnection channel,
527            ViewerModel model) throws JsonProcessingException {
528        model.setVmName(event.params().asString(0));
529        String jsonState = objectMapper.writeValueAsString(model);
530        channel.respond(new KeyValueStoreUpdate().update(storagePath(
531            channel.session(), model.getConletId()), jsonState));
532        updateConfig(channel, model);
533    }
534
535    private void openConsole(String vmName, ConsoleConnection connection,
536            ViewerModel model, String password) {
537        var vmDef = channelManager.associated(vmName).orElse(null);
538        if (vmDef == null) {
539            return;
540        }
541        var addr = displayIp(vmDef);
542        if (addr.isEmpty()) {
543            logger.severe(() -> "Failed to find display IP for " + vmName);
544            return;
545        }
546        var port = GsonPtr.to(vmDef.data()).get(JsonPrimitive.class, "spec",
547            "vm", "display", "spice", "port");
548        if (port.isEmpty()) {
549            logger.severe(() -> "No port defined for display of " + vmName);
550            return;
551        }
552        var proxyUrl = GsonPtr.to(vmDef.data()).get(JsonPrimitive.class, "spec",
553            "vm", "display", "spice", "proxyUrl");
554        StringBuffer data = new StringBuffer(100)
555            .append("[virt-viewer]\ntype=spice\nhost=")
556            .append(addr.get().getHostAddress()).append("\nport=")
557            .append(Integer.toString(port.get().getAsInt()))
558            .append('\n');
559        if (password != null) {
560            data.append("password=").append(password).append('\n');
561        }
562        proxyUrl.map(JsonPrimitive::getAsString).ifPresent(u -> {
563            if (!Strings.isNullOrEmpty(u)) {
564                data.append("proxy=").append(u).append('\n');
565            }
566        });
567        connection.respond(new NotifyConletView(type(),
568            model.getConletId(), "openConsole", "application/x-virt-viewer",
569            Base64.getEncoder().encodeToString(data.toString().getBytes())));
570    }
571
572    private Optional<InetAddress> displayIp(K8sDynamicModel vmDef) {
573        var server = GsonPtr.to(vmDef.data()).get(JsonPrimitive.class, "spec",
574            "vm", "display", "spice", "server");
575        if (server.isPresent()) {
576            var srv = server.get().getAsString();
577            try {
578                var addr = InetAddress.getByName(srv);
579                logger.fine(() -> "Using IP address from CRD for "
580                    + vmDef.getMetadata().getName() + ": " + addr);
581                return Optional.of(addr);
582            } catch (UnknownHostException e) {
583                logger.log(Level.SEVERE, e, () -> "Invalid server address "
584                    + srv + ": " + e.getMessage());
585                return Optional.empty();
586            }
587        }
588        var addrs = GsonPtr.to(vmDef.data()).getAsListOf(JsonPrimitive.class,
589            "nodeAddresses").stream().map(JsonPrimitive::getAsString)
590            .map(a -> {
591                try {
592                    return InetAddress.getByName(a);
593                } catch (UnknownHostException e) {
594                    logger.warning(() -> "Invalid IP address: " + a);
595                    return null;
596                }
597            }).filter(a -> a != null).toList();
598        logger.fine(() -> "Known IP addresses for "
599            + vmDef.getMetadata().getName() + ": " + addrs);
600        return addrs.stream()
601            .filter(a -> preferredIpVersion.isAssignableFrom(a.getClass()))
602            .findFirst().or(() -> addrs.stream().findFirst());
603    }
604
605    private void confirmReset(NotifyConletModel event,
606            ConsoleConnection channel, ViewerModel model,
607            ResourceBundle resourceBundle) throws TemplateNotFoundException,
608            MalformedTemplateNameException, ParseException, IOException {
609        Template tpl = freemarkerConfig()
610            .getTemplate("VmViewer-confirmReset.ftl.html");
611        channel.respond(new OpenModalDialog(type(), model.getConletId(),
612            processTemplate(event, tpl,
613                fmModel(event, channel, model.getConletId(), model)))
614                    .addOption("cancelable", true).addOption("closeLabel", "")
615                    .addOption("title",
616                        resourceBundle.getString("confirmResetTitle")));
617    }
618
619    @Override
620    protected boolean doSetLocale(SetLocale event, ConsoleConnection channel,
621            String conletId) throws Exception {
622        return true;
623    }
624
625    /**
626     * The Class VmsModel.
627     */
628    @SuppressWarnings("PMD.DataClass")
629    public static class ViewerModel extends ConletBaseModel {
630
631        private String vmName;
632        private boolean generated;
633
634        /**
635         * Instantiates a new vms model.
636         *
637         * @param conletId the conlet id
638         */
639        public ViewerModel(@JsonProperty("conletId") String conletId) {
640            super(conletId);
641        }
642
643        /**
644         * Gets the vm name.
645         *
646         * @return the vmName
647         */
648        @JsonGetter("vmName")
649        public String vmName() {
650            return vmName;
651        }
652
653        /**
654         * Sets the vm name.
655         *
656         * @param vmName the vmName to set
657         */
658        public void setVmName(String vmName) {
659            this.vmName = vmName;
660        }
661
662        /**
663         * Checks if is generated.
664         *
665         * @return the generated
666         */
667        public boolean isGenerated() {
668            return generated;
669        }
670
671        /**
672         * Sets the generated.
673         *
674         * @param generated the generated to set
675         */
676        public void setGenerated(boolean generated) {
677            this.generated = generated;
678        }
679
680    }
681}