/*-
 * ~~~~~~licensing~~~~~~
 * cellar-deployer
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.deployer.cellar.deployer;

import org.apache.karaf.cellar.bundle.management.CellarBundleMBean;
import ru.entaxy.esb.system.common.exception.BundleNotFound;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

public class BundleControllerImpl implements BundleController {

    private final CellarBundleMBean cellarBundleMBean;
    private String cellarGroup = "default";

    public BundleControllerImpl(CellarBundleMBean cellarBundleMBean) {
        this.cellarBundleMBean = cellarBundleMBean;
    }

    public String installAndStartBundle(String bundleUrl, String bundleName) throws Exception {
    	cellarBundleMBean.install(cellarGroup, bundleUrl, true);
        return getStatus(bundleName);
    }
    
    public void installBundle(String bundleUrl) throws Exception {
        cellarBundleMBean.install(cellarGroup, bundleUrl);
    }

    public String stopBundle(String bundleName) throws Exception {
        cellarBundleMBean.stop(cellarGroup, bundleName);
        return getStatus(bundleName);
    }

    public String updateBundle(String bundleName) throws Exception {
        cellarBundleMBean.update(cellarGroup, bundleName);
        return getStatus(bundleName);
    }

    public String startBundle(String bundleName) throws Exception {
        cellarBundleMBean.start(cellarGroup, bundleName);
        return getStatus(bundleName);
    }

    public void uninstallBundle(String bundleName) throws Exception {
        cellarBundleMBean.uninstall(cellarGroup, bundleName);
    }

    private String getStatus(String bundleName) throws Exception {
        TabularData tabularData = cellarBundleMBean.getBundles(cellarGroup);
        String status = null;
        for (Object value : tabularData.values()) {
            CompositeData compositeData = (CompositeData) value;
            if (bundleName.equals(compositeData.get("name"))) {
                status = compositeData.get("status").toString();
            }
        }
        if (status == null) {
            throw new BundleNotFound();
        }
        return status.toUpperCase();
    }

    public String getCellarGroup() {
        return cellarGroup;
    }

    public void setCellarGroup(String cellarGroup) {
        this.cellarGroup = cellarGroup;
    }
}
