Replaced all tabs with spaces in java and pom.xml
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / tenantIsolation / WatchdogDistribution.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.asdc.tenantIsolation;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import org.onap.so.client.aai.AAIObjectType;
29 import org.onap.so.client.aai.AAIResourcesClient;
30 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
31 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
32 import org.onap.so.client.graphinventory.entities.uri.Depth;
33 import org.onap.so.db.catalog.data.repository.ServiceRepository;
34 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
35 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
36 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
37 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
38 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
39 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.stereotype.Component;
45
46 @Component
47 public class WatchdogDistribution {
48
49     private static final Logger logger = LoggerFactory.getLogger(WatchdogDistribution.class);
50
51     private AAIResourcesClient aaiClient;
52
53     @Autowired
54     private WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
55
56     @Autowired
57     private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
58
59     @Autowired
60     private WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;
61
62     @Autowired
63     private ServiceRepository serviceRepo;
64
65     @Value("${mso.asdc.config.components.componentNames}")
66     private String[] componentNames;
67
68     public String getOverallDistributionStatus(String distributionId) throws Exception {
69         logger.debug("Entered getOverallDistributionStatus method for distrubutionId: {}", distributionId);
70
71         String status = null;
72         try {
73             WatchdogDistributionStatus watchdogDistributionStatus =
74                     watchdogDistributionStatusRepository.findById(distributionId).orElseGet(() -> null);
75             if (watchdogDistributionStatus == null) {
76                 watchdogDistributionStatus = new WatchdogDistributionStatus();
77                 watchdogDistributionStatus.setDistributionId(distributionId);
78                 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
79             }
80             String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
81
82             if (DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
83                 logger.debug("Ignoring to update WatchdogDistributionStatus as distributionId: {} status is set to: {}",
84                         distributionId, distributionStatus);
85                 return DistributionStatus.TIMEOUT.name();
86             } else {
87                 List<WatchdogComponentDistributionStatus> results =
88                         watchdogCDStatusRepository.findByDistributionId(distributionId);
89                 logger.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: {}",
90                         distributionId);
91
92                 // *************************************************************************************************************************************************
93                 // **** Compare config values verse DB watchdog component names to see if every component has reported
94                 // status before returning final result back to ASDC
95                 // **************************************************************************************************************************************************
96
97                 List<WatchdogComponentDistributionStatus> cdStatuses =
98                         watchdogCDStatusRepository.findByDistributionId(distributionId);
99
100                 boolean allComponentsComplete = true;
101
102                 for (String name : componentNames) {
103
104                     boolean match = false;
105
106                     for (WatchdogComponentDistributionStatus cdStatus : cdStatuses) {
107                         if (name.equals(cdStatus.getComponentName())) {
108                             logger.debug("Found componentName {} in the WatchDog Component DB", name);
109                             match = true;
110                             break;
111                         }
112                     }
113                     if (!match) {
114                         logger.debug("{} has not be updated in the the WatchDog Component DB yet, so ending the loop",
115                                 name);
116                         allComponentsComplete = false;
117                         break;
118                     }
119                 }
120
121
122                 if (allComponentsComplete) {
123                     logger.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
124
125                     for (WatchdogComponentDistributionStatus componentDist : results) {
126                         String componentDistributionStatus = componentDist.getComponentDistributionStatus();
127                         logger.debug("Component status: {} on componentName: ", componentDistributionStatus,
128                                 componentDist.getComponentName());
129                         if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
130                             status = DistributionStatus.FAILURE.name();
131                             break;
132                         } else if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
133                             status = DistributionStatus.SUCCESS.name();
134                         } else {
135                             throw new Exception(
136                                     "Invalid Component distribution status: " + componentDistributionStatus);
137                         }
138                     }
139
140                     logger.debug("Updating overall DistributionStatus to: {} for distributionId: ", status,
141                             distributionId);
142
143                     watchdogDistributionStatus.setDistributionIdStatus(status);
144                     watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
145                 } else {
146                     logger.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
147                     status = DistributionStatus.INCOMPLETE.name();
148                     return status;
149                 }
150             }
151         } catch (Exception e) {
152             logger.debug("Exception occurred on getOverallDistributionStatus : {}", e.getMessage());
153             logger.error("Exception occurred", e);
154             throw new Exception(e);
155         }
156         logger.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
157         return status;
158     }
159
160     public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus)
161             throws Exception {
162         logger.debug("Entered executePatchAAI method with distrubutionId: {} and distributionStatus: ", distributionId,
163                 distributionStatus);
164
165         try {
166             WatchdogServiceModVerIdLookup lookup =
167                     watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
168             String serviceModelVersionId = "";
169
170             if (lookup != null) {
171                 serviceModelVersionId = lookup.getServiceModelVersionId();
172             }
173
174             logger.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: {} "
175                     + "and serviceModelVersionId: {}", distributionId, serviceModelVersionId);
176             logger.debug("ASDC Notification ServiceModelInvariantUUID : {}", serviceModelInvariantUUID);
177
178             if (serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
179                 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
180                 logger.debug(error);
181                 throw new Exception(error);
182             }
183
184             AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID,
185                     serviceModelVersionId);
186             aaiUri.depth(Depth.ZERO); // Do not return relationships if any
187             logger.debug("Target A&AI Resource URI: {}", aaiUri.build().toString());
188
189             Map<String, String> payload = new HashMap<>();
190             payload.put("distribution-status", distributionStatus);
191             getAaiClient().update(aaiUri, payload);
192
193             logger.debug("A&AI UPDATE MODEL Version is success!");
194         } catch (Exception e) {
195             logger.debug("Exception occurred on executePatchAAI : {}", e.getMessage());
196             logger.error("Exception occurred", e);
197             throw new Exception(e);
198         }
199     }
200
201     public AAIResourcesClient getAaiClient() {
202         if (aaiClient == null) {
203             aaiClient = new AAIResourcesClient();
204         }
205         return aaiClient;
206     }
207
208     public void setAaiClient(AAIResourcesClient aaiClient) {
209         this.aaiClient = aaiClient;
210     }
211 }