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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=================================================
20 package org.onap.optf.cmso.optimizer.clients.topology;
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;
38 * The Class TopologyRequestManager.
41 public class TopologyRequestManager {
47 RequestDao requestDao;
50 TopologyDao topologyDao;
53 TopologyClient topologyClient;
56 * Creates the topology request.
58 * @param requestRow the request row
59 * @return the topology response
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);
68 TopologyResponse topologyResponse = topologyClient.makeRequest(requestRow, topology);
69 topologyDao.save(topology);
70 return topologyResponse;
76 * Gets the existing topology.
78 * @param uuid the uuid
79 * @return the existing topology
81 public Topology getExistingTopology(UUID uuid) {
82 Optional<Topology> topologyOpt = topologyDao.findById(uuid);
83 if (topologyOpt.isPresent()) {
84 return topologyOpt.get();
91 * Gets the topology response.
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.
99 public TopologyResponse getTopologyResponse(UUID uuid)
100 throws JsonParseException, JsonMappingException, IOException {
101 Topology row = getExistingTopology(uuid);
103 String responseString = row.getTopology();
104 return new ObjectMapper().readValue(responseString, TopologyResponse.class);