From c556b574b03a0458e1f8121351c80286ac02bb63 Mon Sep 17 00:00:00 2001 From: ShuhaoCai Date: Mon, 25 Jul 2022 11:01:46 +0800 Subject: Intent service impletation Signed-off-by: ShuhaoCai Issue-ID: USECASEUI-704 Change-Id: If31afadd3c81ffa308a51ad4e6ba65a82e557010 --- .../intentanalysis/IntentAnalysisApplication.java | 3 +- .../usecaseui/intentanalysis/bean/po/IntentPo.java | 1 + .../controller/IntentController.java | 2 +- .../intentanalysis/mapper/ExpectationMapper.java | 33 +++++++ .../intentanalysis/mapper/IntentMapper.java | 36 ++++++++ .../intentanalysis/mapper/StateMapper.java | 31 +++++++ .../intentanalysis/service/IntentService.java | 2 +- .../service/impl/ExpectationServiceImpl.java | 83 +++++++++++++++++ .../service/impl/IntentServiceImpl.java | 101 +++++++++++++++++++++ .../service/impl/StateServiceImpl.java | 56 ++++++++++++ intentanalysis/src/main/resources/application.yaml | 12 +++ .../src/main/resources/intent-analysis-init.sql | 20 ++++ .../resources/mybatis/sql/ExpectationMapper.xml | 28 ++++++ .../main/resources/mybatis/sql/IntentMapper.xml | 35 +++++++ .../src/main/resources/mybatis/sql/StateMapper.xml | 27 ++++++ 15 files changed, 467 insertions(+), 3 deletions(-) create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java create mode 100644 intentanalysis/src/main/resources/intent-analysis-init.sql create mode 100644 intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml create mode 100644 intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml create mode 100644 intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml (limited to 'intentanalysis') diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplication.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplication.java index 819c1e7..c2a0ec2 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplication.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplication.java @@ -16,9 +16,10 @@ package org.onap.usecaseui.intentanalysis; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; - +@MapperScan("org.onap.usecaseui.intentanalysis.mapper") @SpringBootApplication public class IntentAnalysisApplication { diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/po/IntentPo.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/po/IntentPo.java index 5372081..844ff6d 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/po/IntentPo.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/po/IntentPo.java @@ -35,6 +35,7 @@ public class IntentPo { Intent intent = new Intent(); intent.setIntentId(this.intentPoId); intent.setIntentName(this.intentPoName); + intent.setExpectationList(getExpectationList()); return intent; } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/controller/IntentController.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/controller/IntentController.java index 00d8d3a..d1b4aac 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/controller/IntentController.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/controller/IntentController.java @@ -53,7 +53,7 @@ public class IntentController { public ResponseEntity updateIntentById( @PathVariable(INTENT_ID) String intentId, @RequestBody Intent intent) { - return ResponseEntity.ok(intentService.updateIntentById(intentId, intent)); + return ResponseEntity.ok(intentService.updateIntent(intent)); } @DeleteMapping(value = "/{intentId}", produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java new file mode 100644 index 0000000..bcca440 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java @@ -0,0 +1,33 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.onap.usecaseui.intentanalysis.bean.po.ExpectationPo; + +import java.util.List; +@Mapper +public interface ExpectationMapper { + + void insertExpectation(List expectation); + + List selectExpectationByIntentId(String intentId); + + void deleteExpectationByIntentId(String intentId); + + void updateExpectation(List expectation); + +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java new file mode 100644 index 0000000..4de598a --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java @@ -0,0 +1,36 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.onap.usecaseui.intentanalysis.bean.po.IntentPo; + +import java.util.List; + +@Mapper +public interface IntentMapper { + + void insertIntent(IntentPo intentPo); + + void updateIntent(IntentPo intentPo); + + IntentPo selectIntentById(String intentId); + + List selectIntents(); + + void deleteIntentById(String intentId); +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java new file mode 100644 index 0000000..b1b1416 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java @@ -0,0 +1,31 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.onap.usecaseui.intentanalysis.bean.po.StatePo; + +import java.util.List; +@Mapper +public interface StateMapper { + + void insertState(List state); + + List selectStateByExpectation(String expectationId); + + void deleteStateByExpectationId(String expectationId); +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/IntentService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/IntentService.java index 4e8765a..36615ab 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/IntentService.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/IntentService.java @@ -27,7 +27,7 @@ public interface IntentService { Intent createIntent(Intent intent); - Intent updateIntentById(String intentId, Intent intent); + Intent updateIntent(Intent intent); void deleteIntentById(String intentId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java new file mode 100644 index 0000000..98e5364 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.service.impl; + + +import org.onap.usecaseui.intentanalysis.bean.po.ExpectationPo; +import org.onap.usecaseui.intentanalysis.bean.po.StatePo; +import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper; +import org.onap.usecaseui.intentanalysis.service.ExpectationService; +import org.onap.usecaseui.intentanalysis.service.StateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +import java.util.List; + +@Service +public class ExpectationServiceImpl implements ExpectationService { + + private static Logger LOGGER = LoggerFactory.getLogger(ExpectationServiceImpl.class); + @Autowired + private ExpectationMapper expectationMapper; + + @Autowired + private StateService stateService; + + @Override + public void createExpectationList(List expectationPoList, String intentId) { + for (ExpectationPo expectationPo : expectationPoList) { + if (null != expectationPo) { + expectationPo.setIntentPoId(intentId); + stateService.createStateList(expectationPo.getStatePoList(), expectationPo.getExpectationPoId()); + } + } + expectationMapper.insertExpectation(expectationPoList); + } + + @Override + public List getExpectationListByIntentId(String intentId) { + List expectationList = expectationMapper.selectExpectationByIntentId(intentId); + for (ExpectationPo expectation : expectationList) { + List stateList = stateService.getStateListByExpectationId(expectation.getExpectationPoId()); + expectation.setStatePoList(stateList); + } + return expectationList; + } + + @Override + public void deleteExpectationListById(String intentId) { + List expectationList = expectationMapper.selectExpectationByIntentId(intentId); + expectationMapper.deleteExpectationByIntentId(intentId); + for (ExpectationPo expectation : expectationList) { + stateService.deleteStateListByExpectationId(expectation.getExpectationPoId()); + } + } + + @Override + public void updateExpectationListById(List expectationPoList, String intentId) { + List expectationList = expectationMapper.selectExpectationByIntentId(intentId); + if (expectationList == null) { + LOGGER.error("Intent ID {} doesn't exist in database.", intentId); + throw new IllegalArgumentException("This intent ID doesn't exist in database."); + } + expectationMapper.updateExpectation(expectationPoList); + LOGGER.info("Expectations are successfully updated."); + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java new file mode 100644 index 0000000..29a7480 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java @@ -0,0 +1,101 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.service.impl; + + +import org.onap.usecaseui.intentanalysis.bean.models.Intent; +import org.onap.usecaseui.intentanalysis.bean.po.IntentPo; +import org.onap.usecaseui.intentanalysis.mapper.IntentMapper; +import org.onap.usecaseui.intentanalysis.service.ExpectationService; +import org.onap.usecaseui.intentanalysis.service.IntentService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class IntentServiceImpl implements IntentService { + private static Logger LOGGER = LoggerFactory.getLogger(IntentService.class); + + @Autowired + private IntentMapper intentMapper; + + @Autowired + private ExpectationService expectationService; + + @Override + public List getIntentList() { + List intentList = new ArrayList<>(); + List intentPoList = intentMapper.selectIntents(); + if (intentPoList == null || intentPoList.size() <= 0) { + return intentList; + } + for (IntentPo intentPo : intentPoList) { + if (intentPo != null) { + intentPo.setExpectationPoList(expectationService.getExpectationListByIntentId(intentPo.getIntentPoId())); + intentList.add(intentPo.transferToIntent()); + } + } + return intentList; + } + + @Override + public Intent getIntentById(String intentId) { + IntentPo intentPo = intentMapper.selectIntentById(intentId); + if (intentPo != null) { + intentPo.setExpectationPoList(expectationService.getExpectationListByIntentId(intentPo.getIntentPoId())); + return intentPo.transferToIntent(); + } else { + String msg = "Intent Id requested doesn't exist in the intent database"; + LOGGER.error(msg); + throw new IllegalArgumentException(msg); + } + } + + @Transactional(rollbackFor = RuntimeException.class) + @Override + public Intent createIntent(Intent intent) { + IntentPo intentPo = intent.transferToIntentPo(); + intentMapper.insertIntent(intentPo); + // saving expectation list into expectation table + expectationService.createExpectationList(intentPo.getExpectationPoList(), intentPo.getIntentPoId()); + LOGGER.info("Intent was successfully created."); + return intent; + } + + @Override + public Intent updateIntent(Intent intent) { + String intentId = intent.getIntentId(); + IntentPo intentPo = intentMapper.selectIntentById(intentId); + if (intentPo == null) { + LOGGER.error("intent id {} not exists in db.", intentId); + } + intentMapper.updateIntent(intentPo); + LOGGER.info("update intent successfully."); + return intentMapper.selectIntentById(intentId).transferToIntent(); + } + + @Override + public void deleteIntentById(String intentId) { + intentMapper.deleteIntentById(intentId); + expectationService.deleteExpectationListById(intentId); + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java new file mode 100644 index 0000000..c907ff8 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java @@ -0,0 +1,56 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * 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.onap.usecaseui.intentanalysis.service.impl; + + +import org.onap.usecaseui.intentanalysis.bean.po.StatePo; +import org.onap.usecaseui.intentanalysis.mapper.StateMapper; +import org.onap.usecaseui.intentanalysis.service.StateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class StateServiceImpl implements StateService { + + @Autowired + private StateMapper stateMapper; + + @Override + public void createStateList(List stateList, String expectationId) { + for (StatePo state : stateList) { + state.setStatePoId(expectationId); + } + stateMapper.insertState(stateList); + } + + @Override + public List getStateListByExpectationId(String expectationId) { + List stateList = stateMapper.selectStateByExpectation(expectationId); + return stateList; + } + + @Override + public void deleteStateListByExpectationId(String expectationId) { + stateMapper.deleteStateByExpectationId(expectationId); + } + + @Override + public void updateStateListByExpectationId(List statePoList, String expectationId){ + }; +} diff --git a/intentanalysis/src/main/resources/application.yaml b/intentanalysis/src/main/resources/application.yaml index 644d527..22f59a5 100644 --- a/intentanalysis/src/main/resources/application.yaml +++ b/intentanalysis/src/main/resources/application.yaml @@ -2,3 +2,15 @@ server: port: 8083 servlet: context-path: /api/usecaseui-intent-analysis/v1 +spring: + datasource: + url: jdbc:postgresql://${POSTGRES_IP:127.0.0.1}:${POSTGRES_PORT:5432}/${POSTGRES_DB_NAME:intentdb} + username: ${POSTGRES_USERNAME} + password: ${POSTGRES_PASSWORD} + driver-class-name: org.postgresql.Driver + schema: classpath:intent-analysis-init.sql + initialization-mode: always +mybatis: + configuration: + database-id: PostgreSQL + mapper-locations: classpath*:mybatis/sql/*.xml \ No newline at end of file diff --git a/intentanalysis/src/main/resources/intent-analysis-init.sql b/intentanalysis/src/main/resources/intent-analysis-init.sql new file mode 100644 index 0000000..64074fa --- /dev/null +++ b/intentanalysis/src/main/resources/intent-analysis-init.sql @@ -0,0 +1,20 @@ +create table if not exists intent( + intent_id varchar(255) primary key, + intent_name varchar(255) +); + +create table if not exists expectation( + expectation_id varchar(255) primary key, + expectation_name varchar(255), + target_moi varchar(255), + intent_id varchar(255) +); + +create table if not exists state( + state_id varchar(255) primary key, + state_name varchar(255), + is_satisfied boolean, + condition varchar(255), + expectation_id varchar(255) +); + diff --git a/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml new file mode 100644 index 0000000..5c5ac06 --- /dev/null +++ b/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml @@ -0,0 +1,28 @@ + + + + + + + + + insert into expectation(expectation_id, expectation_name, target_moi, intent_id) + values + + (#{item.expectationId}, #{item.expectationName}, #{item.targetMOI}, #{item.intentId}) + + + + + delete from expectation + where intent_id = #{intentId} + + + \ No newline at end of file diff --git a/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml new file mode 100644 index 0000000..c814bfe --- /dev/null +++ b/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml @@ -0,0 +1,35 @@ + + + + + + + + + + insert into Intent(intent_id, intent_name) + values(#{intentId}, #{intentName}) + + + + update intent + + intent_id = #{intentId}, + intent_name = #{intentName}, + + where intent_id = #{intentId} + + + + delete from intent + where intent_id = #{intentId} + + + \ No newline at end of file diff --git a/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml new file mode 100644 index 0000000..987c75f --- /dev/null +++ b/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml @@ -0,0 +1,27 @@ + + + + + + + + insert into state(state_id, state_name, expectation_id, is_satisfied, condition) + values + + (#{item.stateId}, #{item.stateName}, #{item.expectationId}, #{item.isSatisfied}, #{item.condition}) + + + + + delete from state + where expectation_id = #{expectationId} + + + \ No newline at end of file -- cgit 1.2.3-korg