Springboot 2.0 upgrade
[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  * 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.so.asdc.tenantIsolation;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.onap.so.client.aai.AAIObjectType;
28 import org.onap.so.client.aai.AAIResourcesClient;
29 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
30 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
31 import org.onap.so.client.graphinventory.entities.uri.Depth;
32 import org.onap.so.db.catalog.data.repository.ServiceRepository;
33 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
34 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
35 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
36 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
37 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
38 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
39 import org.onap.so.logger.MsoLogger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class WatchdogDistribution {
46
47         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,WatchdogDistribution.class);
48
49         private AAIResourcesClient aaiClient;
50         
51         @Autowired
52         private WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
53         
54         @Autowired
55         private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
56         
57         @Autowired
58         private WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;
59         
60         @Autowired
61         private ServiceRepository serviceRepo;
62         
63         @Value("${mso.asdc.config.components.componentNames}")
64         private String[] componentNames;
65            
66         public String getOverallDistributionStatus(String distributionId) throws Exception {
67                 LOGGER.debug("Entered getOverallDistributionStatus method for distrubutionId: " + distributionId);
68                 
69                 String status = null;
70                 try {
71                         WatchdogDistributionStatus watchdogDistributionStatus = watchdogDistributionStatusRepository.findById(distributionId)
72                                 .orElseGet( () -> null);
73                         if(watchdogDistributionStatus == null){
74                                 watchdogDistributionStatus = new WatchdogDistributionStatus();
75                                 watchdogDistributionStatus.setDistributionId(distributionId);
76                                 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
77                         }
78                         String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
79                         
80                         if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
81                                 LOGGER.debug("Ignoring to update WatchdogDistributionStatus as distributionId: " + distributionId + " status is set to: " + distributionStatus);
82                                 return DistributionStatus.TIMEOUT.name();
83                         } else {
84                                 List<WatchdogComponentDistributionStatus> results = watchdogCDStatusRepository.findByDistributionId(distributionId);
85                                 LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);
86                 
87                                 //*************************************************************************************************************************************************
88                                 //**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC
89                                 //**************************************************************************************************************************************************
90                                 
91                                 List<WatchdogComponentDistributionStatus> cdStatuses = watchdogCDStatusRepository.findByDistributionId(distributionId);
92                                 
93                                 boolean allComponentsComplete = true;
94                       
95                        for(String name : componentNames ) {                              
96                                 
97                                 boolean match = false;
98                                 
99                                                 for(WatchdogComponentDistributionStatus cdStatus: cdStatuses){                                                  
100                                                         if(name.equals(cdStatus.getComponentName())){
101                                                                 LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");
102                                                                 match = true;
103                                                                 break;
104                                                         }
105                                                 }                                               
106                                                 if(!match){
107                                                         LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");
108                                                         allComponentsComplete = false;
109                                                         break;
110                                                 }
111                             }
112                          
113                                 
114                                 if(allComponentsComplete) {                             
115                                         LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
116                                         
117                                          for(WatchdogComponentDistributionStatus componentDist : results) {
118                                                  String componentDistributionStatus = componentDist.getComponentDistributionStatus();
119                                                  LOGGER.debug("Component status: " + componentDistributionStatus + " on componentName: " + componentDist.getComponentName());
120                                                  if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
121                                                          status = DistributionStatus.FAILURE.name();
122                                                          break;
123                                                  } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
124                                                          status = DistributionStatus.SUCCESS.name();
125                                                  } else {
126                                                          throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
127                                                  }
128                                          }
129                                          
130                                          LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);
131                                          
132                                          watchdogDistributionStatus.setDistributionIdStatus(status);
133                                          watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
134                                 } else {
135                                         LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
136                                         status = DistributionStatus.INCOMPLETE.name();
137                                         return status;
138                                 }
139                         }
140                 }catch (Exception e) {
141                         LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());
142                         LOGGER.error(e);
143                         throw new Exception(e);
144                 }               
145                 LOGGER.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
146                 return status;
147         }
148         
149         public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
150                 LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);
151                 
152                 try {
153                         WatchdogServiceModVerIdLookup lookup = watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
154                         String serviceModelVersionId = "";
155                         
156                         if(lookup != null) {
157                                 serviceModelVersionId = lookup.getServiceModelVersionId();
158                         }
159                         
160                         LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);
161                         LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);
162                         
163                         if(serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
164                                 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
165                                 LOGGER.debug(error);
166                                 throw new Exception(error);
167                         }
168                         
169                         AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);
170                         aaiUri.depth(Depth.ZERO); //Do not return relationships if any
171                         LOGGER.debug("Target A&AI Resource URI: " + aaiUri.build().toString());
172                         
173                         Map<String, String> payload = new HashMap<>();
174                         payload.put("distribution-status", distributionStatus);
175                         getAaiClient().update(aaiUri, payload);
176                         
177                         LOGGER.debug("A&AI UPDATE MODEL Version is success!");
178                 } catch (Exception e) {
179                         LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());
180                         LOGGER.error(e);
181                         throw new Exception(e);
182                 }
183         }
184
185         public AAIResourcesClient getAaiClient() {
186                 if(aaiClient == null) {
187                         aaiClient = new AAIResourcesClient();
188                 }
189                 return aaiClient;
190         }
191
192         public void setAaiClient(AAIResourcesClient aaiClient) {
193                 this.aaiClient = aaiClient;
194         }
195 }