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