ce0d583d98ac4f1c76600d9ca2694f18d372cdbf
[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 java.util.Optional;
23 import java.util.UUID;
24 import org.onap.optf.cmso.optimizer.clients.topology.models.TopologyResponse;
25 import org.onap.optf.cmso.optimizer.model.Request;
26 import org.onap.optf.cmso.optimizer.model.Topology;
27 import org.onap.optf.cmso.optimizer.model.dao.RequestDao;
28 import org.onap.optf.cmso.optimizer.model.dao.TopologyDao;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.core.env.Environment;
31 import org.springframework.stereotype.Component;
32
33 /**
34  * The Class TopologyRequestManager.
35  */
36 @Component
37 public class TopologyRequestManager {
38
39     @Autowired
40     Environment env;
41
42     @Autowired
43     RequestDao requestDao;
44
45     @Autowired
46     TopologyDao topologyDao;
47
48     @Autowired
49     TopologyClient topologyClient;
50
51     /**
52      * Creates the topology request.
53      *
54      * @param requestRow the request row
55      * @return the topology response
56      */
57     public TopologyResponse createTopologyRequest(Request requestRow) {
58         Topology topology = getExistingTopology(requestRow.getUuid());
59         if (topology == null) {
60             topology = new Topology();
61             topology.setUuid(requestRow.getUuid());
62             topology.setTopologyRetries(0);
63         }
64         TopologyResponse topologyResponse = topologyClient.makeRequest(requestRow, topology);
65         topologyDao.save(topology);
66         return topologyResponse;
67
68     }
69
70
71     /**
72      * Gets the existing topology.
73      *
74      * @param uuid the uuid
75      * @return the existing topology
76      */
77     public Topology getExistingTopology(UUID uuid) {
78         Optional<Topology> topologyOpt = topologyDao.findById(uuid);
79         if (topologyOpt.isPresent()) {
80             return topologyOpt.get();
81         }
82         return null;
83     }
84 }