Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / acs / nbi / src / main / java / org / commscope / tr069adapter / acs / nbi / mapper / service / MapperRequestRESTService.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.acs.nbi.mapper.service;
20
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.MAPPER_SERVICE_QUALILFIER;
22
23 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
24 import org.commscope.tr069adapter.acs.common.exception.DeviceOperationException;
25 import org.commscope.tr069adapter.acs.common.exception.MapperServiceException;
26 import org.commscope.tr069adapter.acs.common.mapper.ACSServiceAPI;
27 import org.commscope.tr069adapter.acs.common.utils.ConnectionStatusPOJO;
28 import org.commscope.tr069adapter.acs.common.utils.ErrorCode;
29 import org.commscope.tr069adapter.acs.requestprocessor.dao.DeviceRepository;
30 import org.commscope.tr069adapter.acs.requestprocessor.entity.TR069DeviceEntity;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Qualifier;
35 import org.springframework.web.bind.annotation.PostMapping;
36 import org.springframework.web.bind.annotation.RequestBody;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RestController;
39
40 @RestController
41 @RequestMapping("/MapperService")
42 public class MapperRequestRESTService {
43
44   private static final Logger logger = LoggerFactory.getLogger(MapperRequestRESTService.class);
45
46   @Qualifier(value = MAPPER_SERVICE_QUALILFIER)
47   @Autowired
48   ACSServiceAPI acsServiceAPI;
49
50   @Autowired
51   private DeviceRepository deviceRepository;
52
53   @PostMapping("/initiateDeviceOperation")
54   public Long initiateDeviceOperation(@RequestBody DeviceRPCRequest deviceRPCRequest) {
55     logger.debug("Received a Device operation request from Mapper");
56     Long operationId = 0l;
57     try {
58       operationId = acsServiceAPI.performDeviceOperation(deviceRPCRequest);
59       logger.debug(
60           "Successfully initiated device operation, The operation id for the request is: {}",
61           operationId);
62     } catch (MapperServiceException e) {
63       logger.error("An exception occurred while calling the device operation, Reason: {}",
64           e.getMessage());
65     }
66
67     return operationId;
68   }
69
70   @PostMapping("/connectionStatusOperation")
71   public ConnectionStatusPOJO connectionStatusOperation(@RequestBody String deviceId)
72       throws DeviceOperationException {
73     logger.debug("Received a Connection Status operation request from Mapper");
74     ConnectionStatusPOJO connStatusPOJO = new ConnectionStatusPOJO();
75     try {
76       TR069DeviceEntity tr069DeviceEntity = deviceRepository.findByDeviceId(deviceId);
77       if (tr069DeviceEntity == null) {
78         throw new DeviceOperationException(ErrorCode.DEVICE_NOT_EXISTS, deviceId);
79       }
80       connStatusPOJO.setStatus(tr069DeviceEntity.isConnStatus());
81       connStatusPOJO.setLastContactTime(tr069DeviceEntity.getLastUpdatedTime());
82       connStatusPOJO.setLastFailedAttemptTime(tr069DeviceEntity.getLastFailedAttemptTime());
83       connStatusPOJO.setErrorMessage(tr069DeviceEntity.getErrorMsg());
84       logger.info("connectionStatusOperation:: ConnectionStatusPOJO: {}", connStatusPOJO);
85       return connStatusPOJO;
86     } catch (DeviceOperationException doe) {
87       logger.error(doe.getMessage());
88       throw doe;
89     }
90   }
91
92 }