f62a851b3c6b7a3864569411a13c3b2b9cb06d6f
[optf/cmso.git] /
1 /*
2  * ============LICENSE_START==============================================
3  * Copyright (c) 2019 AT&T Intellectual Property.
4  * =======================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License. You may obtain a
7  * copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing
15  * permissions and limitations under the License.
16  * ============LICENSE_END=================================================
17  *
18  */
19
20 package org.onap.optf.cmso.optimizer.clients.topology;
21
22 import com.fasterxml.jackson.core.JsonParseException;
23 import com.fasterxml.jackson.databind.JsonMappingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
26 import java.util.Optional;
27 import java.util.UUID;
28 import org.onap.optf.cmso.optimizer.clients.topology.models.TopologyResponse;
29 import org.onap.optf.cmso.optimizer.model.Request;
30 import org.onap.optf.cmso.optimizer.model.Topology;
31 import org.onap.optf.cmso.optimizer.model.dao.RequestDao;
32 import org.onap.optf.cmso.optimizer.model.dao.TopologyDao;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.core.env.Environment;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * The Class TopologyRequestManager.
39  */
40 @Component
41 public class TopologyRequestManager {
42
43     @Autowired
44     Environment env;
45
46     @Autowired
47     RequestDao requestDao;
48
49     @Autowired
50     TopologyDao topologyDao;
51
52     @Autowired
53     TopologyClient topologyClient;
54
55     /**
56      * Creates the topology request.
57      *
58      * @param requestRow the request row
59      * @return the topology response
60      */
61     public TopologyResponse createTopologyRequest(Request requestRow) {
62         Topology topology = getExistingTopology(requestRow.getUuid());
63         if (topology == null) {
64             topology = new Topology();
65             topology.setUuid(requestRow.getUuid());
66             topology.setTopologyRetries(0);
67         }
68         TopologyResponse topologyResponse = topologyClient.makeRequest(requestRow, topology);
69         topologyDao.save(topology);
70         return topologyResponse;
71
72     }
73
74
75     /**
76      * Gets the existing topology.
77      *
78      * @param uuid the uuid
79      * @return the existing topology
80      */
81     public Topology getExistingTopology(UUID uuid) {
82         Optional<Topology> topologyOpt = topologyDao.findById(uuid);
83         if (topologyOpt.isPresent()) {
84             return topologyOpt.get();
85         }
86         return null;
87     }
88
89
90     /**
91      * Gets the topology response.
92      *
93      * @param uuid the uuid
94      * @return the topology response
95      * @throws JsonParseException the json parse exception
96      * @throws JsonMappingException the json mapping exception
97      * @throws IOException Signals that an I/O exception has occurred.
98      */
99     public TopologyResponse getTopologyResponse(UUID uuid)
100                     throws JsonParseException, JsonMappingException, IOException {
101         Topology row = getExistingTopology(uuid);
102         if (row != null) {
103             String responseString = row.getTopology();
104             return new ObjectMapper().readValue(responseString, TopologyResponse.class);
105         }
106         return null;
107     }
108 }