aboutsummaryrefslogtreecommitdiffstats
path: root/asdc-controller/src/main/java/org/openecomp/mso/asdc/tenantIsolation/WatchdogDistribution.java
blob: 6efcd64204a85e82f6c823547a8f77b885de734b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.asdc.tenantIsolation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.openecomp.mso.asdc.client.ASDCConfiguration;
import org.openecomp.mso.client.aai.AAIObjectType;
import org.openecomp.mso.client.aai.AAIResourcesClient;
import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
import org.openecomp.mso.client.aai.entities.uri.Depth;
import org.openecomp.mso.db.catalog.CatalogDatabase;
import org.openecomp.mso.db.catalog.beans.Service;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.properties.MsoJsonProperties;
import org.openecomp.mso.properties.MsoPropertiesException;
import org.openecomp.mso.properties.MsoPropertiesFactory;
import org.openecomp.mso.requestsdb.WatchdogComponentDistributionStatus;
import org.openecomp.mso.requestsdb.WatchdogComponentDistributionStatusDb;
import org.openecomp.mso.requestsdb.WatchdogDistributionStatusDb;
import org.openecomp.mso.requestsdb.WatchdogServiceModVerIdLookupDb;

import com.fasterxml.jackson.databind.JsonNode;

public class WatchdogDistribution {

	private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
	private static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
	private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
	private WatchdogDistributionStatusDb watchdogDistDb;
	private WatchdogComponentDistributionStatusDb watchdogCompDistDb;
	private WatchdogServiceModVerIdLookupDb watchdogSerlookupDb;
	private CatalogDatabase catalogDb;
	private AAIResourcesClient aaiClient;
	//protected ASDCConfiguration asdcConfig;
	   
	public String getOverallDistributionStatus(String distributionId) throws MsoPropertiesException, Exception {
		LOGGER.debug("Entered getOverallDistributionStatus method for distrubutionId: " + distributionId);
		
		String status = null;
		try { 
			String distributionStatus = getWatchdogDistDb().getWatchdogDistributionIdStatus(distributionId);
			
			if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
				LOGGER.debug("Ignoring to update WatchdogDistributionStatus as distributionId: " + distributionId + " status is set to: " + distributionStatus);
				return DistributionStatus.TIMEOUT.name();
			} else {
				List<WatchdogComponentDistributionStatus> results = getWatchdogCompDistDb().getWatchdogComponentDistributionStatus(distributionId);
				LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);
		
				MsoJsonProperties properties = msoPropertiesFactory.getMsoJsonProperties(MSO_PROP_ASDC);
				
				//*************************************************************************************************************************************************
				//**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC
				//**************************************************************************************************************************************************
				
				//List<String> configNames = asdcConfig.getComponentNames();
				
				List<String> dbNames = watchdogCompDistDb.getWatchdogComponentNames(distributionId);
				
				boolean allComponentsComplete = true;
							
				JsonNode masterConfigNode = properties.getJsonRootNode().get("componentNames");
				
		        if (masterConfigNode != null) { 
		            
		            Iterator<JsonNode> config = masterConfigNode.elements();
		      
		            while( config.hasNext() ) {
		                String name = (String)config.next().asText();	                
		                
		                boolean match = false;
		                
						for(String dbName: dbNames){
							
							if(name.equals(dbName)){
								LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");
								match = true;
								break;
							}
						}
						
						if(match==false){
							LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");
							allComponentsComplete = false;
							break;
						}

		            }

		        } 
				
				if(allComponentsComplete) {
				//if(node.asInt() == results.size()) {
					LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
					
					 for(WatchdogComponentDistributionStatus componentDist : results) {
						 String componentDistributionStatus = componentDist.getComponentDistributionStatus();
						 LOGGER.debug("Component status: " + componentDistributionStatus + " on componentName: " + componentDist.getComponentName());
						 if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
							 status = DistributionStatus.FAILURE.name();
							 break;
						 } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
							 status = DistributionStatus.SUCCESS.name();
						 } else {
							 throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
						 }
					 }
					 
					 LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);
					 getWatchdogDistDb().updateWatchdogDistributionIdStatus(distributionId, status);
				} else {
					LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
					status = DistributionStatus.INCOMPLETE.name();
					return status;
				}
			}
		} catch (MsoPropertiesException e) {
			String error = "Error occurred when trying to load MSOJson Properties.";
			LOGGER.debug(error);
			throw new MsoPropertiesException(e.getMessage());
		} catch (Exception e) {
			LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());
			throw new Exception(e);
		}
		
		LOGGER.debug("Exciting getOverallDistributionStatus method in WatchdogDistribution");
		return status;
	}
	
	public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
		LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);
		
		try { 
			String serviceModelVersionId = getWatchdogSerlookupDb().getWatchdogServiceModVerId(distributionId);
			LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);
			
			LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);
			
			if(serviceModelInvariantUUID == null) {
				String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
				LOGGER.debug(error);
				throw new Exception(error);
			}
			
			AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);
			aaiUri.depth(Depth.ZERO); //Do not return relationships if any
			LOGGER.debug("Target A&AI Resource URI: " + aaiUri.build().toString());
			
			Map<String, String> payload = new HashMap<>();
			payload.put("distribution-status", distributionStatus);
			getAaiClient().update(aaiUri, payload);
			
			LOGGER.debug("A&AI UPDATE MODEL Version is success!");
		} catch (Exception e) {
			LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());
			throw new Exception(e);
		}
	}
	
	public WatchdogDistributionStatusDb getWatchdogDistDb() {
		if(watchdogDistDb == null) {
			watchdogDistDb =  WatchdogDistributionStatusDb.getInstance();
		}
		return watchdogDistDb;
	}

	public void setWatchdogDistDb(WatchdogDistributionStatusDb watchdogDistDb) {
		this.watchdogDistDb = watchdogDistDb;
	}

	public WatchdogComponentDistributionStatusDb getWatchdogCompDistDb() {
		if(watchdogCompDistDb == null) {
			watchdogCompDistDb =  WatchdogComponentDistributionStatusDb.getInstance();
		}
		return watchdogCompDistDb;
	}

	public void setWatchdogCompDistDb(WatchdogComponentDistributionStatusDb watchdogCompDistDb) {
		this.watchdogCompDistDb = watchdogCompDistDb;
	}

	public WatchdogServiceModVerIdLookupDb getWatchdogSerlookupDb() {
		if(watchdogSerlookupDb == null) {
			watchdogSerlookupDb =  WatchdogServiceModVerIdLookupDb.getInstance();
		}
		return watchdogSerlookupDb;
	}

	public void setWatchdogSerlookupDb(WatchdogServiceModVerIdLookupDb watchdogSerlookupDb) {
		this.watchdogSerlookupDb = watchdogSerlookupDb;
	}

	public CatalogDatabase getCatalogDb() {
		if(catalogDb == null) {
			catalogDb = CatalogDatabase.getInstance();
		}
		return catalogDb;
	}

	public void setCatalogDb(CatalogDatabase catalogDb) {
		this.catalogDb = catalogDb;
	}

	public AAIResourcesClient getAaiClient() {
		if(aaiClient == null) {
			aaiClient = new AAIResourcesClient();
		}
		return aaiClient;
	}

	public void setAaiClient(AAIResourcesClient aaiClient) {
		this.aaiClient = aaiClient;
	}
	
}