d487b6b21b8f88b0c644a179299f7ef54a8f77a5
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
23 import java.io.IOException;
24 import java.util.Optional;
25
26 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
27 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
28 import org.onap.dcaegen2.collectors.datafile.configuration.Config;
29 import org.onap.dcaegen2.collectors.datafile.exceptions.AaiNotFoundException;
30 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
31 import org.onap.dcaegen2.collectors.datafile.service.AaiConsumerClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class AaiConsumerTaskImpl extends AaiConsumerTask {
39
40     private final Logger logger = LoggerFactory.getLogger(this.getClass());
41
42     private final Config datafileAppConfig;
43     private AaiConsumerClient aaiConsumerClient;
44
45     @Autowired
46     public AaiConsumerTaskImpl(AppConfig datafileAppConfig) {
47         this.datafileAppConfig = datafileAppConfig;
48     }
49
50     @Override
51     Optional<String> consume(ConsumerDmaapModel consumerDmaapModel) throws AaiNotFoundException {
52         logger.trace("Method called with arg {}", consumerDmaapModel);
53         try {
54             return aaiConsumerClient.getHttpResponse(consumerDmaapModel);
55         } catch (IOException e) {
56             logger.warn("Get request not successful", e);
57             throw new AaiNotFoundException("Get request not successful");
58         }
59     }
60
61     @Override
62     public String execute(ConsumerDmaapModel consumerDmaapModel) throws AaiNotFoundException {
63         consumerDmaapModel = Optional.ofNullable(consumerDmaapModel)
64             .orElseThrow(() -> new AaiNotFoundException("Invoked null object to AAI task"));
65         logger.trace("Method called with arg {}", consumerDmaapModel);
66         aaiConsumerClient = resolveClient();
67         return consume(consumerDmaapModel).orElseThrow(() -> new AaiNotFoundException("Null response code"));
68     }
69
70     protected AaiClientConfiguration resolveConfiguration() {
71         return datafileAppConfig.getAaiClientConfiguration();
72     }
73
74     @Override
75     AaiConsumerClient resolveClient() {
76         return Optional.ofNullable(aaiConsumerClient).orElseGet(() -> new AaiConsumerClient(resolveConfiguration()));
77     }
78 }