fuweng11 commented on code in PR #8892: URL: https://github.com/apache/inlong/pull/8892#discussion_r1327894263
########## inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/node/cls/ClsDataNodeInfo.java: ########## @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.pojo.node.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonTypeDefine; +import org.apache.inlong.manager.pojo.node.DataNodeInfo; +import org.apache.inlong.manager.pojo.node.DataNodeRequest; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.SuperBuilder; + +/** + * cloud log service data node info Review Comment: Cloud ########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/cls/ClsResourceOperator.java: ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.service.resource.sink.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.consts.InlongConstants; +import org.apache.inlong.manager.common.consts.SinkType; +import org.apache.inlong.manager.common.enums.SinkStatus; +import org.apache.inlong.manager.common.exceptions.BusinessException; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonUtils; +import org.apache.inlong.manager.dao.entity.DataNodeEntity; +import org.apache.inlong.manager.dao.entity.StreamSinkEntity; +import org.apache.inlong.manager.dao.mapper.DataNodeEntityMapper; +import org.apache.inlong.manager.dao.mapper.StreamSinkEntityMapper; +import org.apache.inlong.manager.pojo.node.cls.ClsDataNodeDTO; +import org.apache.inlong.manager.pojo.sink.SinkInfo; +import org.apache.inlong.manager.pojo.sink.cls.ClsSinkDTO; +import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator; +import org.apache.inlong.manager.service.sink.StreamSinkService; + +import com.tencentcloudapi.cls.v20201016.ClsClient; +import com.tencentcloudapi.cls.v20201016.models.CreateIndexRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicResponse; +import com.tencentcloudapi.cls.v20201016.models.FullTextInfo; +import com.tencentcloudapi.cls.v20201016.models.RuleInfo; +import com.tencentcloudapi.cls.v20201016.models.Tag; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import org.apache.directory.api.util.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ClsResourceOperator implements SinkResourceOperator { + + private static final Logger LOG = LoggerFactory.getLogger(ClsResourceOperator.class); + + @Autowired + private DataNodeEntityMapper dataNodeEntityMapper; + @Autowired + private StreamSinkService sinkService; + @Autowired + private StreamSinkEntityMapper streamSinkEntityMapper; + + @Override + public Boolean accept(String sinkType) { + return SinkType.CLS.equals(sinkType); + } + + @Override + public void createSinkResource(SinkInfo sinkInfo) { + LOG.info("begin to create sink resources sinkId={}", sinkInfo.getId()); + if (SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) { + LOG.warn("sink resource [" + sinkInfo.getId() + "] already success, skip to create"); + return; + } else if (InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource())) { + LOG.warn("create resource was disabled, skip to create for [" + sinkInfo.getId() + "]"); + return; + } + this.createTopicID(sinkInfo); + } + + private void createTopicID(SinkInfo sinkInfo) { + ClsDataNodeDTO clsDataNode = getClsDataNode(sinkInfo); + ClsSinkDTO clsSinkDTO = JsonUtils.parseObject(sinkInfo.getExtParams(), ClsSinkDTO.class); + try { + ClsClient client = getClsClient(clsDataNode); + CreateTopicRequest req = new CreateTopicRequest(); + String[] allTags = clsSinkDTO.getTag().split(InlongConstants.CENTER_LINE); + Tag[] tags = convertTags(allTags); + req.setTags(tags); + req.setLogsetId(clsDataNode.getLogSetId()); + req.setTopicName(clsSinkDTO.getTopicName()); + CreateTopicResponse resp = client.CreateTopic(req); + LOG.info("create cls topic {} success ,topicId {}", clsSinkDTO.getTopicName(), resp.getTopicId()); + clsSinkDTO.setTopicId(resp.getTopicId()); + sinkInfo.setExtParams(JsonUtils.toJsonString(clsSinkDTO)); + StreamSinkEntity streamSinkEntity = new StreamSinkEntity(); + CommonBeanUtils.copyProperties(sinkInfo, streamSinkEntity, true); + streamSinkEntityMapper.updateByIdSelective(streamSinkEntity); + this.createTopicIndex(sinkInfo); + String info = "success to create cls resource"; + sinkService.updateStatus(sinkInfo.getId(), SinkStatus.CONFIG_SUCCESSFUL.getCode(), info); + LOG.info("update cls sink = {}info status success ,topicName {}", streamSinkEntity.getSinkName(), + clsSinkDTO.getTopicName()); + } catch (TencentCloudSDKException e) { + String errMsg = "Create cls topic failed: " + e.getMessage(); + LOG.error(errMsg, e); + sinkService.updateStatus(sinkInfo.getId(), SinkStatus.CONFIG_FAILED.getCode(), errMsg); + throw new BusinessException(errMsg); + } + } + + private ClsClient getClsClient(ClsDataNodeDTO clsDataNode) { + Credential cred = new Credential(clsDataNode.getManageSecretId(), + clsDataNode.getManageSecretId()); + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint(clsDataNode.getEndpoint()); + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + return new ClsClient(cred, clsDataNode.getRegion(), clientProfile); + } + + private void createTopicIndex(SinkInfo sinkInfo) throws BusinessException { + ClsSinkDTO clsSinkDTO = JsonUtils.parseObject(sinkInfo.getExtParams(), ClsSinkDTO.class); + if (Strings.isEmpty(clsSinkDTO.getTokenizer())) { Review Comment: StringUtils.isNotBlank ########## inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/node/cls/ClsDataNodeRequest.java: ########## @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.pojo.node.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.util.JsonTypeDefine; +import org.apache.inlong.manager.pojo.node.DataNodeRequest; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +/** + * cloud log service data node request + */ +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +@JsonTypeDefine(value = DataNodeType.CLS) +@ApiModel("cloud log service data node request") Review Comment: Ditto. ########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/cls/ClsResourceOperator.java: ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.service.resource.sink.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.consts.InlongConstants; +import org.apache.inlong.manager.common.consts.SinkType; +import org.apache.inlong.manager.common.enums.SinkStatus; +import org.apache.inlong.manager.common.exceptions.BusinessException; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonUtils; +import org.apache.inlong.manager.dao.entity.DataNodeEntity; +import org.apache.inlong.manager.dao.entity.StreamSinkEntity; +import org.apache.inlong.manager.dao.mapper.DataNodeEntityMapper; +import org.apache.inlong.manager.dao.mapper.StreamSinkEntityMapper; +import org.apache.inlong.manager.pojo.node.cls.ClsDataNodeDTO; +import org.apache.inlong.manager.pojo.sink.SinkInfo; +import org.apache.inlong.manager.pojo.sink.cls.ClsSinkDTO; +import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator; +import org.apache.inlong.manager.service.sink.StreamSinkService; + +import com.tencentcloudapi.cls.v20201016.ClsClient; +import com.tencentcloudapi.cls.v20201016.models.CreateIndexRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicResponse; +import com.tencentcloudapi.cls.v20201016.models.FullTextInfo; +import com.tencentcloudapi.cls.v20201016.models.RuleInfo; +import com.tencentcloudapi.cls.v20201016.models.Tag; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import org.apache.directory.api.util.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ClsResourceOperator implements SinkResourceOperator { + + private static final Logger LOG = LoggerFactory.getLogger(ClsResourceOperator.class); + + @Autowired + private DataNodeEntityMapper dataNodeEntityMapper; + @Autowired + private StreamSinkService sinkService; + @Autowired + private StreamSinkEntityMapper streamSinkEntityMapper; + + @Override + public Boolean accept(String sinkType) { + return SinkType.CLS.equals(sinkType); + } + + @Override + public void createSinkResource(SinkInfo sinkInfo) { + LOG.info("begin to create sink resources sinkId={}", sinkInfo.getId()); + if (SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) { + LOG.warn("sink resource [" + sinkInfo.getId() + "] already success, skip to create"); + return; + } else if (InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource())) { + LOG.warn("create resource was disabled, skip to create for [" + sinkInfo.getId() + "]"); + return; + } + this.createTopicID(sinkInfo); + } + + private void createTopicID(SinkInfo sinkInfo) { + ClsDataNodeDTO clsDataNode = getClsDataNode(sinkInfo); + ClsSinkDTO clsSinkDTO = JsonUtils.parseObject(sinkInfo.getExtParams(), ClsSinkDTO.class); + try { + ClsClient client = getClsClient(clsDataNode); + CreateTopicRequest req = new CreateTopicRequest(); + String[] allTags = clsSinkDTO.getTag().split(InlongConstants.CENTER_LINE); + Tag[] tags = convertTags(allTags); + req.setTags(tags); + req.setLogsetId(clsDataNode.getLogSetId()); Review Comment: Wrap this code into a method. ########## inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/node/cls/ClsDataNodeRequest.java: ########## @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.pojo.node.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.util.JsonTypeDefine; +import org.apache.inlong.manager.pojo.node.DataNodeRequest; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +/** + * cloud log service data node request Review Comment: Ditto. ########## inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/node/cls/ClsDataNodeInfo.java: ########## @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.pojo.node.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonTypeDefine; +import org.apache.inlong.manager.pojo.node.DataNodeInfo; +import org.apache.inlong.manager.pojo.node.DataNodeRequest; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.SuperBuilder; + +/** + * cloud log service data node info + */ +@Data +@SuperBuilder +@AllArgsConstructor +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +@JsonTypeDefine(value = DataNodeType.ELASTICSEARCH) +@ApiModel("cloud log service data node info") Review Comment: Cloud ########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/cls/ClsResourceOperator.java: ########## @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.service.resource.sink.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.consts.InlongConstants; +import org.apache.inlong.manager.common.consts.SinkType; +import org.apache.inlong.manager.common.enums.SinkStatus; +import org.apache.inlong.manager.common.exceptions.BusinessException; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonUtils; +import org.apache.inlong.manager.dao.entity.DataNodeEntity; +import org.apache.inlong.manager.dao.entity.StreamSinkEntity; +import org.apache.inlong.manager.dao.mapper.DataNodeEntityMapper; +import org.apache.inlong.manager.dao.mapper.StreamSinkEntityMapper; +import org.apache.inlong.manager.pojo.node.cls.ClsDataNodeDTO; +import org.apache.inlong.manager.pojo.sink.SinkInfo; +import org.apache.inlong.manager.pojo.sink.cls.ClsSinkDTO; +import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator; +import org.apache.inlong.manager.service.sink.StreamSinkService; + +import com.tencentcloudapi.cls.v20201016.ClsClient; +import com.tencentcloudapi.cls.v20201016.models.CreateIndexRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicRequest; +import com.tencentcloudapi.cls.v20201016.models.CreateTopicResponse; +import com.tencentcloudapi.cls.v20201016.models.FullTextInfo; +import com.tencentcloudapi.cls.v20201016.models.RuleInfo; +import com.tencentcloudapi.cls.v20201016.models.Tag; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import org.apache.directory.api.util.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ClsResourceOperator implements SinkResourceOperator { + + private static final Logger LOG = LoggerFactory.getLogger(ClsResourceOperator.class); + + @Autowired + private DataNodeEntityMapper dataNodeEntityMapper; + @Autowired + private StreamSinkService sinkService; + @Autowired + private StreamSinkEntityMapper streamSinkEntityMapper; + + @Override + public Boolean accept(String sinkType) { + return SinkType.CLS.equals(sinkType); + } + + @Override + public void createSinkResource(SinkInfo sinkInfo) { + LOG.info("begin to create sink resources sinkId={}", sinkInfo.getId()); + if (SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) { + LOG.warn("sink resource [" + sinkInfo.getId() + "] already success, skip to create"); + return; + } else if (InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource())) { + LOG.warn("create resource was disabled, skip to create for [" + sinkInfo.getId() + "]"); + return; + } + this.createTopicID(sinkInfo); + } + + private void createTopicID(SinkInfo sinkInfo) { + ClsDataNodeDTO clsDataNode = getClsDataNode(sinkInfo); + ClsSinkDTO clsSinkDTO = JsonUtils.parseObject(sinkInfo.getExtParams(), ClsSinkDTO.class); + try { + ClsClient client = getClsClient(clsDataNode); + CreateTopicRequest req = new CreateTopicRequest(); + String[] allTags = clsSinkDTO.getTag().split(InlongConstants.CENTER_LINE); + Tag[] tags = convertTags(allTags); + req.setTags(tags); + req.setLogsetId(clsDataNode.getLogSetId()); + req.setTopicName(clsSinkDTO.getTopicName()); + CreateTopicResponse resp = client.CreateTopic(req); + LOG.info("create cls topic {} success ,topicId {}", clsSinkDTO.getTopicName(), resp.getTopicId()); + clsSinkDTO.setTopicId(resp.getTopicId()); + sinkInfo.setExtParams(JsonUtils.toJsonString(clsSinkDTO)); + StreamSinkEntity streamSinkEntity = new StreamSinkEntity(); + CommonBeanUtils.copyProperties(sinkInfo, streamSinkEntity, true); + streamSinkEntityMapper.updateByIdSelective(streamSinkEntity); + this.createTopicIndex(sinkInfo); + String info = "success to create cls resource"; + sinkService.updateStatus(sinkInfo.getId(), SinkStatus.CONFIG_SUCCESSFUL.getCode(), info); + LOG.info("update cls sink = {}info status success ,topicName {}", streamSinkEntity.getSinkName(), + clsSinkDTO.getTopicName()); + } catch (TencentCloudSDKException e) { + String errMsg = "Create cls topic failed: " + e.getMessage(); + LOG.error(errMsg, e); + sinkService.updateStatus(sinkInfo.getId(), SinkStatus.CONFIG_FAILED.getCode(), errMsg); + throw new BusinessException(errMsg); + } + } + + private ClsClient getClsClient(ClsDataNodeDTO clsDataNode) { + Credential cred = new Credential(clsDataNode.getManageSecretId(), + clsDataNode.getManageSecretId()); + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint(clsDataNode.getEndpoint()); + ClientProfile clientProfile = new ClientProfile(); Review Comment: Add Blank Line. ########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/sink/cls/ClsSinkOperator.java: ########## @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.inlong.manager.service.sink.cls; + +import org.apache.inlong.manager.common.consts.DataNodeType; +import org.apache.inlong.manager.common.consts.SinkType; +import org.apache.inlong.manager.common.enums.ErrorCodeEnum; +import org.apache.inlong.manager.common.exceptions.BusinessException; +import org.apache.inlong.manager.common.util.CommonBeanUtils; +import org.apache.inlong.manager.common.util.JsonUtils; +import org.apache.inlong.manager.dao.entity.DataNodeEntity; +import org.apache.inlong.manager.dao.entity.StreamSinkEntity; +import org.apache.inlong.manager.dao.mapper.DataNodeEntityMapper; +import org.apache.inlong.manager.pojo.node.cls.ClsDataNodeDTO; +import org.apache.inlong.manager.pojo.sink.SinkField; +import org.apache.inlong.manager.pojo.sink.SinkRequest; +import org.apache.inlong.manager.pojo.sink.StreamSink; +import org.apache.inlong.manager.pojo.sink.cls.ClsSink; +import org.apache.inlong.manager.pojo.sink.cls.ClsSinkDTO; +import org.apache.inlong.manager.pojo.sink.cls.ClsSinkRequest; +import org.apache.inlong.manager.service.sink.AbstractSinkOperator; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * Cloud log service sink operator + */ +@Service +public class ClsSinkOperator extends AbstractSinkOperator { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClsSinkOperator.class); + private static final String KEY_FIELDS = "fieldNames"; + private static final String SECRET_KEY = "secretKey"; + private static final String SECRET_ID = "secretId"; + private static final String END_POINT = "endpoint"; + private static final String TOPIC_ID = "topicId"; + + @Autowired + private ObjectMapper objectMapper; + @Autowired + private DataNodeEntityMapper dataNodeEntityMapper; + + @Override + protected void setTargetEntity(SinkRequest request, StreamSinkEntity targetEntity) { + if (!this.getSinkType().equals(request.getSinkType())) { + throw new BusinessException(ErrorCodeEnum.SINK_TYPE_NOT_SUPPORT, + ErrorCodeEnum.SINK_TYPE_NOT_SUPPORT.getMessage() + ": " + getSinkType()); + } + ClsSinkRequest sinkRequest = (ClsSinkRequest) request; + try { + ClsSinkDTO dto = ClsSinkDTO.getFromRequest(sinkRequest, targetEntity.getExtParams()); + targetEntity.setExtParams(objectMapper.writeValueAsString(dto)); + } catch (Exception e) { + throw new BusinessException(ErrorCodeEnum.SINK_SAVE_FAILED, + String.format("serialize extParams of Doris SinkDTO failure: %s", e.getMessage())); + } + } + + @Override + protected String getSinkType() { + return SinkType.CLS; + } + + @Override + public Boolean accept(String sinkType) { + return SinkType.CLS.equals(sinkType); + } + + @Override + public StreamSink getFromEntity(StreamSinkEntity entity) { + ClsSink sink = new ClsSink(); + if (entity == null) { + return sink; + } + + ClsSinkDTO dto = ClsSinkDTO.getFromJson(entity.getExtParams()); + DataNodeEntity dataNodeEntity = dataNodeEntityMapper.selectByUniqueKey(entity.getDataNodeName(), + DataNodeType.CLS); + ClsDataNodeDTO clsDataNodeDTO = JsonUtils.parseObject(dataNodeEntity.getExtParams(), + ClsDataNodeDTO.class); + CommonBeanUtils.copyProperties(entity, sink, true); + CommonBeanUtils.copyProperties(dto, sink, true); + CommonBeanUtils.copyProperties(clsDataNodeDTO, sink, true); + List<SinkField> sinkFields = getSinkFields(entity.getId()); + sink.setSinkFieldList(sinkFields); + return sink; + } + + @Override + public Map<String, String> parse2IdParams(StreamSinkEntity streamSink, List<String> fields) { + Map<String, String> params = super.parse2IdParams(streamSink, fields); + ClsSinkDTO clsSinkDTO = JsonUtils.parseObject(streamSink.getExtParams(), ClsSinkDTO.class); + params.put(TOPIC_ID, clsSinkDTO.getTopicId()); + DataNodeEntity dataNodeEntity = dataNodeEntityMapper.selectByUniqueKey(streamSink.getDataNodeName(), + DataNodeType.CLS); + ClsDataNodeDTO clsDataNodeDTO = JsonUtils.parseObject(dataNodeEntity.getExtParams(), + ClsDataNodeDTO.class); + params.put(SECRET_ID, clsDataNodeDTO.getSendSecretId()); + params.put(SECRET_KEY, clsDataNodeDTO.getSendSecretKey()); + params.put(END_POINT, clsDataNodeDTO.getEndpoint()); + StringBuilder fieldNames = new StringBuilder(); + for (String field : fields) { + fieldNames.append(field).append(" "); Review Comment: Use `InlongConstants.BLANK` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
