Containerization feature of SO
[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.findOne(distributionId);
72                         if(watchdogDistributionStatus == null){
73                                 watchdogDistributionStatus = new WatchdogDistributionStatus();
74                                 watchdogDistributionStatus.setDistributionId(distributionId);
75                                 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
76                         }
77                         String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
78                         
79                         if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
80                                 LOGGER.debug("Ignoring to update WatchdogDistributionStatus as distributionId: " + distributionId + " status is set to: " + distributionStatus);
81                                 return DistributionStatus.TIMEOUT.name();
82                         } else {
83                                 List<WatchdogComponentDistributionStatus> results = watchdogCDStatusRepository.findByDistributionId(distributionId);
84                                 LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);
85                 
86                                 //*************************************************************************************************************************************************
87                                 //**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC
88                                 //**************************************************************************************************************************************************
89                                 
90                                 List<WatchdogComponentDistributionStatus> cdStatuses = watchdogCDStatusRepository.findByDistributionId(distributionId);
91                                 
92                                 boolean allComponentsComplete = true;
93                       
94                        for(String name : componentNames ) {                              
95                                 
96                                 boolean match = false;
97                                 
98                                                 for(WatchdogComponentDistributionStatus cdStatus: cdStatuses){                                                  
99                                                         if(name.equals(cdStatus.getComponentName())){
100                                                                 LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");
101                                                                 match = true;
102                                                                 break;
103                                                         }
104                                                 }                                               
105                                                 if(!match){
106                                                         LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");
107                                                         allComponentsComplete = false;
108                                                         break;
109                                                 }
110                             }
111                          
112                                 
113                                 if(allComponentsComplete) {                             
114                                         LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
115                                         
116                                          for(WatchdogComponentDistributionStatus componentDist : results) {
117                                                  String componentDistributionStatus = componentDist.getComponentDistributionStatus();
118                                                  LOGGER.debug("Component status: " + componentDistributionStatus + " on componentName: " + componentDist.getComponentName());
119                                                  if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
120                                                          status = DistributionStatus.FAILURE.name();
121                                                          break;
122                                                  } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
123                                                          status = DistributionStatus.SUCCESS.name();
124                                                  } else {
125                                                          throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
126                                                  }
127                                          }
128                                          
129                                          LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);
130                                          
131                                          watchdogDistributionStatus.setDistributionIdStatus(status);
132                                          watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
133                                 } else {
134                                         LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
135                                         status = DistributionStatus.INCOMPLETE.name();
136                                         return status;
137                                 }
138                         }
139                 }catch (Exception e) {
140                         LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());
141                         LOGGER.error(e);
142                         throw new Exception(e);
143                 }               
144                 LOGGER.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
145                 return status;
146         }
147         
148         public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
149                 LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);
150                 
151                 try {
152                         WatchdogServiceModVerIdLookup lookup = watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
153                         String serviceModelVersionId = "";
154                         
155                         if(lookup != null) {
156                                 serviceModelVersionId = lookup.getServiceModelVersionId();
157                         }
158                         
159                         LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);
160                         LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);
161                         
162                         if(serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
163                                 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
164                                 LOGGER.debug(error);
165                                 throw new Exception(error);
166                         }
167                         
168                         AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);
169                         aaiUri.depth(Depth.ZERO); //Do not return relationships if any
170                         LOGGER.debug("Target A&AI Resource URI: " + aaiUri.build().toString());
171                         
172                         Map<String, String> payload = new HashMap<>();
173                         payload.put("distribution-status", distributionStatus);
174                         getAaiClient().update(aaiUri, payload);
175                         
176                         LOGGER.debug("A&AI UPDATE MODEL Version is success!");
177                 } catch (Exception e) {
178                         LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());
179                         LOGGER.error(e);
180                         throw new Exception(e);
181                 }
182         }
183
184         public AAIResourcesClient getAaiClient() {
185                 if(aaiClient == null) {
186                         aaiClient = new AAIResourcesClient();
187                 }
188                 return aaiClient;
189         }
190
191         public void setAaiClient(AAIResourcesClient aaiClient) {
192                 this.aaiClient = aaiClient;
193         }
194 }