HazelcastController.java

/*
 * Copyright 2022 Global Crop Diversity Trust
 *
 * 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.genesys.server.api.admin.v1;

import java.util.List;
import java.util.stream.Collectors;

import org.genesys.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hazelcast.cluster.Cluster;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;

/**
 * Manage caches
 * 
 * @author mobreza
 */
@RestController("hazelcastApi1")
@RequestMapping(HazelcastController.CONTROLLER_URL)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class HazelcastController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/admin/hazelcast";

	public static final Logger LOG = LoggerFactory.getLogger(HazelcastController.class);

	@Autowired(required = false)
	private HazelcastInstance hazelcastInstance;

	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public HzSetup viewHzSetup() {
		Config config = hazelcastInstance.getConfig();
		Cluster cluster = hazelcastInstance.getCluster();
		HzSetup hzSetup = new HzSetup();
		hzSetup.hzConfig = config.toString();
		hzSetup.hzCluster = cluster.toString();
		hzSetup.clusterState = cluster.getClusterState().toString();
		hzSetup.clusterMembers = cluster.getMembers().stream().map(Object::toString).collect(Collectors.toList());
		hzSetup.configProperties = config.getProperties().toString();
		hzSetup.networkConfig = config.getNetworkConfig().toString();
		hzSetup.mapConfigs = config.getMapConfigs().toString();
		hzSetup.queueConfigs = config.getQueueConfigs().toString();
		return hzSetup;
	}

	public static class HzSetup {
		public String hzConfig;
		public String hzCluster;
		public String clusterState;
		public List<String> clusterMembers;
		public String configProperties;
		public String networkConfig;
		public String mapConfigs;
		public String queueConfigs;
	}
}