/*
 * Copyright 2015 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.migration.wfly10.subsystem;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.dmr.ModelNode;
import org.wildfly.migration.core.ServerMigrationContext;
import org.wildfly.migration.core.logger.ServerMigrationLogger;
import org.wildfly.migration.wfly10.standalone.WildFly10StandaloneServer;

import java.io.IOException;

import static org.jboss.as.controller.PathAddress.pathAddress;
import static org.jboss.as.controller.PathElement.pathElement;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.*;

/**
 * @author emmartins
 */
public class EJb3WildFly10Extension extends WildFly10Extension {

    public static final EJb3WildFly10Extension INSTANCE = new EJb3WildFly10Extension();

    private EJb3WildFly10Extension() {
        super("org.jboss.as.ejb3");
        subsystems.add(new Ejb3WildFly10Subsystem(this));
    }

    private static class Ejb3WildFly10Subsystem extends BasicWildFly10Subsystem {
        private Ejb3WildFly10Subsystem(EJb3WildFly10Extension extension) {
            super("ejb3", extension);
        }

        @Override
        public void migrate(WildFly10StandaloneServer server, ServerMigrationContext context) throws IOException {
            super.migrate(server, context);
            migrateConfig(server.getSubsystem(getName()), server, context);
        }

        protected void migrateConfig(ModelNode config, WildFly10StandaloneServer server, ServerMigrationContext context) throws IOException {
            if (config == null) {
                return;
            }
            fixWFLY5520(config, server);
            migrateRemoteConfig(config, server);
        }

        private void fixWFLY5520(ModelNode config, WildFly10StandaloneServer server) throws IOException {
            if (!config.hasDefined("default-clustered-sfsb-cache")) {
                return;
            }
            ServerMigrationLogger.ROOT_LOGGER.debugf("Subsystem %s config after migration defines unsupported attr default-clustered-sfsb-cache (WFLY-5520): %s", getName(), config);
            // /subsystem=ejb3:undefine-attribute(name=default-clustered-sfsb-cache)
            final PathAddress address = pathAddress(pathElement(SUBSYSTEM, getName()));
            ModelNode op = Util.createEmptyOperation(UNDEFINE_ATTRIBUTE_OPERATION, address);
            op.get(NAME).set("default-clustered-sfsb-cache");
            server.executeManagementOperation(op);

            // /subsystem=ejb3:write-attribute(name=default-sfsb-cache,value=clustered)
            op = Util.createEmptyOperation(WRITE_ATTRIBUTE_OPERATION, address);
            op.get(NAME).set("default-sfsb-cache");
            op.get(VALUE).set("clustered");
            server.executeManagementOperation(op);

            // /subsystem=ejb3:write-attribute(name=default-sfsb-passivation-disabled-cache,value=simple)
            op = Util.createEmptyOperation(WRITE_ATTRIBUTE_OPERATION, address);
            op.get(NAME).set("default-sfsb-passivation-disabled-cache");
            op.get(VALUE).set("simple");
            server.executeManagementOperation(op);
        }

        private void migrateRemoteConfig(ModelNode config, WildFly10StandaloneServer server) throws IOException {
            if (!config.hasDefined(SERVICE,"remote")) {
                return;
            }
            // /subsystem=ejb3/service=remote:write-attribute(name=connector-ref,value=http-remoting-connector)
            final ModelNode op = Util.createEmptyOperation(WRITE_ATTRIBUTE_OPERATION, pathAddress(pathElement(SUBSYSTEM, getName()), pathElement(SERVICE,"remote")));
            op.get(NAME).set("connector-ref");
            op.get(VALUE).set("http-remoting-connector");
            server.executeManagementOperation(op);
            ServerMigrationLogger.ROOT_LOGGER.infof("EJB3 subsystem's remote service configured to use HTTP Remoting connector.");
        }
    }
}
@@ -641,52 +641,8 @@
 m;%0A%0A
-import org.jboss.as.controller.PathAddress;%0A
 impo
@@ -692,16 +692,16 @@
 n.Util;%0A
+
 import o
@@ -2169,49 +2169,8 @@
   %7D%0A
-            fixWFLY5520(config, server);%0A
     
@@ -2229,1462 +2229,8 @@
  %7D%0A%0A
-        private void fixWFLY5520(ModelNode config, WildFly10StandaloneServer server) throws IOException %7B%0A            if (!config.hasDefined(%22default-clustered-sfsb-cache%22)) %7B%0A                return;%0A            %7D%0A            ServerMigrationLogger.ROOT_LOGGER.debugf(%22Subsystem %25s config after migration defines unsupported attr default-clustered-sfsb-cache (WFLY-5520): %25s%22, getName(), config);%0A            // /subsystem=ejb3:undefine-attribute(name=default-clustered-sfsb-cache)%0A            final PathAddress address = pathAddress(pathElement(SUBSYSTEM, getName()));%0A            ModelNode op = Util.createEmptyOperation(UNDEFINE_ATTRIBUTE_OPERATION, address);%0A            op.get(NAME).set(%22default-clustered-sfsb-cache%22);%0A            server.executeManagementOperation(op);%0A%0A            // /subsystem=ejb3:write-attribute(name=default-sfsb-cache,value=clustered)%0A            op = Util.createEmptyOperation(WRITE_ATTRIBUTE_OPERATION, address);%0A            op.get(NAME).set(%22default-sfsb-cache%22);%0A            op.get(VALUE).set(%22clustered%22);%0A            server.executeManagementOperation(op);%0A%0A            // /subsystem=ejb3:write-attribute(name=default-sfsb-passivation-disabled-cache,value=simple)%0A            op = Util.createEmptyOperation(WRITE_ATTRIBUTE_OPERATION, address);%0A            op.get(NAME).set(%22default-sfsb-passivation-disabled-cache%22);%0A            op.get(VALUE).set(%22simple%22);%0A            server.executeManagementOperation(op);%0A        %7D%0A%0A
     

Remove workaround for WFLY-5520 since WildFly 10.0.0.Final