org.bidib.wizard.gateway.autoconfig.DistributedBidibAutoConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-gateway Show documentation
Show all versions of bidibwizard-gateway Show documentation
jBiDiB BiDiB Wizard Gateway POM
package org.bidib.wizard.gateway.autoconfig;
import java.io.File;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.core.schema.bidibbase.DefaultLabelsActionType;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.jbidibc.messages.BidibMessageEvaluator;
import org.bidib.jbidibc.messages.ConnectionListener;
import org.bidib.jbidibc.messages.HostAdapter;
import org.bidib.jbidibc.messages.SoftwareVersion;
import org.bidib.jbidibc.messages.enums.PortModelEnum;
import org.bidib.jbidibc.messages.helpers.Context;
import org.bidib.jbidibc.messages.helpers.DefaultContext;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.BidibResponseFactory;
import org.bidib.jbidibc.messages.message.netbidib.LocalProtocolSignatureMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.LogonStatus;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.PairingStatus;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.PartnerType;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.netbidib.pairingstore.LocalPairingStore;
import org.bidib.jbidibc.netbidib.pairingstore.PairingStore;
import org.bidib.jbidibc.netbidib.server.NetBidibServer;
import org.bidib.jbidibc.netbidib.server.NetBidibServerHandler;
import org.bidib.jbidibc.netbidib.server.RoleTypeEnum;
import org.bidib.wizard.common.labels.DefaultWizardLabelFactory.DefaultLabelsApplied;
import org.bidib.wizard.common.labels.DefaultWizardLabelFactory.VersionedDefaultNodeLabelsWrapper;
import org.bidib.wizard.common.labels.WizardLabelFactory;
import org.bidib.wizard.common.labels.WizardLabelWrapper;
import org.bidib.wizard.common.model.settings.GatewayServiceSettingsInterface;
import org.bidib.wizard.common.model.settings.NetBidibSettingsInterface;
import org.bidib.wizard.common.service.SettingsService;
import org.bidib.wizard.core.model.connection.ConnectionRegistry;
import org.bidib.wizard.gateway.config.GatewayConfig;
import org.bidib.wizard.gateway.config.GatewayServiceSettings;
import org.bidib.wizard.gateway.netbidib.MessageHostAdapter;
import org.bidib.wizard.gateway.netbidib.NetBidibServerMessage;
import org.bidib.wizard.gateway.service.BidibDistributedGatewayService;
import org.bidib.wizard.gateway.service.ProxyConnectionAdapter;
import org.bidib.wizard.gateway.service.ProxyNetBidibConnectionAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import io.netty.channel.group.ChannelGroup;
@Configuration(proxyBeanMethods = false)
@AutoConfigurationPackage
@EnableConfigurationProperties(GatewayServiceSettings.class)
public class DistributedBidibAutoConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(DistributedBidibAutoConfig.class);
@ConditionalOnProperty(prefix = "experimentalSettings", name = "bidibDistributedEnabled_disabled")
@Configuration
@Import(GatewayConfig.class)
protected static class NetBidibServerConfig {
@Autowired
private GatewayServiceSettingsInterface gatewayServiceConfiguration;
@Autowired
private NetBidibSettingsInterface netBidibSettings;
@Autowired
private BidibRequestFactory bidibRequestFactory;
@Autowired
private BidibResponseFactory bidibResponseFactory;
@Autowired
private SettingsService settingsService;
@Bean
@ConditionalOnMissingBean
WizardLabelWrapper wizardLabelWrapper() {
LOGGER.info("Create the WizardLabelWrapper.");
return new WizardLabelWrapper() {
@Override
public WizardLabelFactory getWizardLabelFactory() {
// TODO Auto-generated method stub
return null;
}
@Override
public NodeLabels loadLabels(Long uniqueId) {
LOGGER.warn("Do not load labels for uniqueId: {}", ByteUtils.formatHexUniqueId(uniqueId));
// TODO Auto-generated method stub
return null;
}
@Override
public void saveNodeLabels(Long uniqueId) {
// TODO Auto-generated method stub
}
@Override
public DefaultLabelsApplied getDefaultLabelsApplied(Long uniqueId) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setDefaultLabelsApplied(
String lang, Long uniqueId, final SoftwareVersion softwareVersion, int relevantPidBits,
PortModelEnum portModel, DefaultLabelsActionType defaultLabelsAction) {
// TODO Auto-generated method stub
}
@Override
public VersionedDefaultNodeLabelsWrapper isDefaultLabelsAvailable(
final String lang, Long uniqueId, final SoftwareVersion softwareVersion, int relevantPidBits) {
// TODO Auto-generated method stub
return null;
}
@Override
public void deleteNodeLabels(Long uniqueId) {
// TODO Auto-generated method stub
}
@Override
public NodeLabels prepareDefaultLabels(Long uniqueId) {
// TODO Auto-generated method stub
return null;
}
@Override
public void saveDefaultLabels(NodeLabels nodeLabels, File file) {
// TODO Auto-generated method stub
}
};
}
@Bean
@ConditionalOnMissingBean(BidibDistributedGatewayService.class)
BidibDistributedGatewayService bidibGatewayService() {
final NetBidibLinkData pairedPartner = pairedPartner();
return new BidibDistributedGatewayService(bidibRequestFactory, bidibResponseFactory, pairedPartner,
proxyConnectionAdapter());
}
@Bean
@ConditionalOnMissingBean(MessageHostAdapter.class)
MessageHostAdapter hostAdapter(final BidibMessageEvaluator bidibGatewayService) {
final MessageHostAdapter hostAdapter = new MessageHostAdapter(bidibGatewayService);
final Context context = new DefaultContext();
hostAdapter.initialize(context);
return hostAdapter;
}
/**
* This bean is only created if the profile "bidib-distributed" is not active.
*/
@Bean
@ConditionalOnMissingBean(ProxyConnectionAdapter.class)
ProxyConnectionAdapter proxyConnectionAdapter() {
LOGGER.info("Create the ProxyNetBidibConnectionAdapter instance.");
return new ProxyNetBidibConnectionAdapter(bidibResponseFactory);
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
NetBidibLinkData pairedPartner() {
NetBidibLinkData pairedPartner = new NetBidibLinkData(PartnerType.REMOTE);
pairedPartner.setPairingStatus(PairingStatus.UNKNOWN);
pairedPartner.setLogonStatus(LogonStatus.LOGGED_OFF);
return pairedPartner;
}
@Bean
NetBidibLinkData serverLinkData() {
NetBidibLinkData netBidibLinkData = new NetBidibLinkData(PartnerType.LOCAL);
netBidibLinkData.setRequestorName(LocalProtocolSignatureMessage.EMITTER_PREFIX_BIDIB + "-Gateway");
return netBidibLinkData;
}
@ConditionalOnMissingBean
@Bean
PairingStore pairingStore() {
String pairingStorePath = netBidibSettings.getPairingStoreLocation();
if (StringUtils.isBlank(pairingStorePath)) {
pairingStorePath =
new File(settingsService.getMiscSettings().getBidibConfigDir(), "/data/netBiDiB").getPath();
LOGGER.info("Set the default location of the pairing store: {}", pairingStorePath);
netBidibSettings.setPairingStoreLocation(pairingStorePath);
}
final File pairingStoreFile = new File(pairingStorePath, ".wizard2PairingStore");
LOGGER.info("Load pairingStore from file: {}", pairingStoreFile);
final LocalPairingStore pairingStore = new LocalPairingStore(pairingStoreFile);
try {
pairingStore.load();
}
catch (Exception ex) {
LOGGER.warn("Load pairingStore failed. Current pairingStoreFile: {}", pairingStoreFile.getPath(), ex);
}
return pairingStore;
}
@Bean
NetBidibServer netBidibServer(final MessageHostAdapter hostAdapter) {
String bindAddress = gatewayServiceConfiguration.getBindAddress();
int listenPort = gatewayServiceConfiguration.getListenPortNetBidib();
final RoleTypeEnum roleType = RoleTypeEnum.INTERFACE;
// TODO
String connectionId = ConnectionRegistry.CONNECTION_ID_MAIN;
// TODO shortcut
final BiFunction pairingCallback = (bidibLinkData, pairingTimeout) -> {
LOGGER
.warn("Accept the pairing request by default. Provided bidibLinkData: {}, pairingTimeout: {}",
bidibLinkData, pairingTimeout);
return true;
};
final NetBidibLinkData pairedPartner = pairedPartner();
LOGGER
.info(
"Create the netBidibServer, bindAddress: {}, listenPort: {}, connectionName: {}, pairedPartner: {}",
bindAddress, listenPort, connectionId, pairedPartner);
final PairingStore pairingStore = pairingStore();
final NetBidibServer netBidibServer =
new NetBidibServerMessage(bindAddress, listenPort, hostAdapter, connectionId, serverLinkData(),
roleType, pairedPartner) {
@Override
protected NetBidibServerHandler createNetBidibServerHandler(
ChannelGroup channelGroup, final NetBidibLinkData serverLinkData,
final HostAdapter hostAdapter, final String connectionName,
final Consumer> lazyInitializationCallback,
final RoleTypeEnum roleType, final NetBidibLinkData pairedPartner) {
LOGGER
.info(
"Create the NetBidibServerHandler instance, connectionName: {}, roleType: {}, pairedPartner: {}",
connectionName, roleType, pairedPartner);
// create the NerBidibServerHandler instance
NetBidibServerHandler netBidibServerHandler =
super.createNetBidibServerHandler(channelGroup, serverLinkData, hostAdapter, connectionName,
lazyInitializationCallback, roleType, pairedPartner);
// inject the pairing store and callback
netBidibServerHandler.setPairingStore(pairingStore);
netBidibServerHandler.setPairingCallback(pairingCallback);
// add the connection listener for the remote connection to the client
netBidibServerHandler.addRemoteConnectionListener(new ConnectionListener() {
@Override
public void status(String messageKey, final Context context) {
LOGGER
.info("Connection status received, messageKey: {}, context: {}", messageKey,
context);
}
@Override
public void opened(String port) {
LOGGER.info("Remote connection opened, port: {}", port);
}
@Override
public void closed(String port) {
LOGGER.info("The connection to the client was closed: {}", port);
// if (connector != null) {
// connector.showWaitForClient();
// }
}
@Override
public void stall(boolean stall) {
}
});
return netBidibServerHandler;
}
};
return netBidibServer;
}
}
}