AT&T 1712 and 1802 release code
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / 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.openecomp.mso.asdc.tenantIsolation;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.HashMap;\r
25 import java.util.Iterator;\r
26 import java.util.List;\r
27 import java.util.Map;\r
28 \r
29 import org.openecomp.mso.asdc.client.ASDCConfiguration;\r
30 import org.openecomp.mso.client.aai.AAIObjectType;\r
31 import org.openecomp.mso.client.aai.AAIResourcesClient;\r
32 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;\r
33 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;\r
34 import org.openecomp.mso.client.aai.entities.uri.Depth;\r
35 import org.openecomp.mso.db.catalog.CatalogDatabase;\r
36 import org.openecomp.mso.db.catalog.beans.Service;\r
37 import org.openecomp.mso.logger.MsoLogger;\r
38 import org.openecomp.mso.properties.MsoJsonProperties;\r
39 import org.openecomp.mso.properties.MsoPropertiesException;\r
40 import org.openecomp.mso.properties.MsoPropertiesFactory;\r
41 import org.openecomp.mso.requestsdb.WatchdogComponentDistributionStatus;\r
42 import org.openecomp.mso.requestsdb.WatchdogComponentDistributionStatusDb;\r
43 import org.openecomp.mso.requestsdb.WatchdogDistributionStatusDb;\r
44 import org.openecomp.mso.requestsdb.WatchdogServiceModVerIdLookupDb;\r
45 \r
46 import com.fasterxml.jackson.databind.JsonNode;\r
47 \r
48 public class WatchdogDistribution {\r
49 \r
50         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);\r
51         private static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";\r
52         private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();\r
53         private WatchdogDistributionStatusDb watchdogDistDb;\r
54         private WatchdogComponentDistributionStatusDb watchdogCompDistDb;\r
55         private WatchdogServiceModVerIdLookupDb watchdogSerlookupDb;\r
56         private CatalogDatabase catalogDb;\r
57         private AAIResourcesClient aaiClient;\r
58         //protected ASDCConfiguration asdcConfig;\r
59            \r
60         public String getOverallDistributionStatus(String distributionId) throws MsoPropertiesException, Exception {\r
61                 LOGGER.debug("Entered getOverallDistributionStatus method for distrubutionId: " + distributionId);\r
62                 \r
63                 String status = null;\r
64                 try { \r
65                         String distributionStatus = getWatchdogDistDb().getWatchdogDistributionIdStatus(distributionId);\r
66                         \r
67                         if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {\r
68                                 LOGGER.debug("Ignoring to update WatchdogDistributionStatus as distributionId: " + distributionId + " status is set to: " + distributionStatus);\r
69                                 return DistributionStatus.TIMEOUT.name();\r
70                         } else {\r
71                                 List<WatchdogComponentDistributionStatus> results = getWatchdogCompDistDb().getWatchdogComponentDistributionStatus(distributionId);\r
72                                 LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);\r
73                 \r
74                                 MsoJsonProperties properties = msoPropertiesFactory.getMsoJsonProperties(MSO_PROP_ASDC);\r
75                                 \r
76                                 //*************************************************************************************************************************************************\r
77                                 //**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC\r
78                                 //**************************************************************************************************************************************************\r
79                                 \r
80                                 //List<String> configNames = asdcConfig.getComponentNames();\r
81                                 \r
82                                 List<String> dbNames = watchdogCompDistDb.getWatchdogComponentNames(distributionId);\r
83                                 \r
84                                 boolean allComponentsComplete = true;\r
85                                                         \r
86                                 JsonNode masterConfigNode = properties.getJsonRootNode().get("componentNames");\r
87                                 \r
88                         if (masterConfigNode != null) { \r
89                             \r
90                             Iterator<JsonNode> config = masterConfigNode.elements();\r
91                       \r
92                             while( config.hasNext() ) {\r
93                                 String name = (String)config.next().asText();                   \r
94                                 \r
95                                 boolean match = false;\r
96                                 \r
97                                                 for(String dbName: dbNames){\r
98                                                         \r
99                                                         if(name.equals(dbName)){\r
100                                                                 LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");\r
101                                                                 match = true;\r
102                                                                 break;\r
103                                                         }\r
104                                                 }\r
105                                                 \r
106                                                 if(match==false){\r
107                                                         LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");\r
108                                                         allComponentsComplete = false;\r
109                                                         break;\r
110                                                 }\r
111 \r
112                             }\r
113 \r
114                         } \r
115                                 \r
116                                 if(allComponentsComplete) {\r
117                                 //if(node.asInt() == results.size()) {\r
118                                         LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");\r
119                                         \r
120                                          for(WatchdogComponentDistributionStatus componentDist : results) {\r
121                                                  String componentDistributionStatus = componentDist.getComponentDistributionStatus();\r
122                                                  LOGGER.debug("Component status: " + componentDistributionStatus + " on componentName: " + componentDist.getComponentName());\r
123                                                  if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {\r
124                                                          status = DistributionStatus.FAILURE.name();\r
125                                                          break;\r
126                                                  } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {\r
127                                                          status = DistributionStatus.SUCCESS.name();\r
128                                                  } else {\r
129                                                          throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);\r
130                                                  }\r
131                                          }\r
132                                          \r
133                                          LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);\r
134                                          getWatchdogDistDb().updateWatchdogDistributionIdStatus(distributionId, status);\r
135                                 } else {\r
136                                         LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");\r
137                                         status = DistributionStatus.INCOMPLETE.name();\r
138                                         return status;\r
139                                 }\r
140                         }\r
141                 } catch (MsoPropertiesException e) {\r
142                         String error = "Error occurred when trying to load MSOJson Properties.";\r
143                         LOGGER.debug(error);\r
144                         throw new MsoPropertiesException(e.getMessage());\r
145                 } catch (Exception e) {\r
146                         LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());\r
147                         throw new Exception(e);\r
148                 }\r
149                 \r
150                 LOGGER.debug("Exciting getOverallDistributionStatus method in WatchdogDistribution");\r
151                 return status;\r
152         }\r
153         \r
154         public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {\r
155                 LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);\r
156                 \r
157                 try { \r
158                         String serviceModelVersionId = getWatchdogSerlookupDb().getWatchdogServiceModVerId(distributionId);\r
159                         LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);\r
160                         \r
161                         LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);\r
162                         \r
163                         if(serviceModelInvariantUUID == null) {\r
164                                 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;\r
165                                 LOGGER.debug(error);\r
166                                 throw new Exception(error);\r
167                         }\r
168                         \r
169                         AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);\r
170                         aaiUri.depth(Depth.ZERO); //Do not return relationships if any\r
171                         LOGGER.debug("Target A&AI Resource URI: " + aaiUri.build().toString());\r
172                         \r
173                         Map<String, String> payload = new HashMap<>();\r
174                         payload.put("distribution-status", distributionStatus);\r
175                         getAaiClient().update(aaiUri, payload);\r
176                         \r
177                         LOGGER.debug("A&AI UPDATE MODEL Version is success!");\r
178                 } catch (Exception e) {\r
179                         LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());\r
180                         throw new Exception(e);\r
181                 }\r
182         }\r
183         \r
184         public WatchdogDistributionStatusDb getWatchdogDistDb() {\r
185                 if(watchdogDistDb == null) {\r
186                         watchdogDistDb =  WatchdogDistributionStatusDb.getInstance();\r
187                 }\r
188                 return watchdogDistDb;\r
189         }\r
190 \r
191         public void setWatchdogDistDb(WatchdogDistributionStatusDb watchdogDistDb) {\r
192                 this.watchdogDistDb = watchdogDistDb;\r
193         }\r
194 \r
195         public WatchdogComponentDistributionStatusDb getWatchdogCompDistDb() {\r
196                 if(watchdogCompDistDb == null) {\r
197                         watchdogCompDistDb =  WatchdogComponentDistributionStatusDb.getInstance();\r
198                 }\r
199                 return watchdogCompDistDb;\r
200         }\r
201 \r
202         public void setWatchdogCompDistDb(WatchdogComponentDistributionStatusDb watchdogCompDistDb) {\r
203                 this.watchdogCompDistDb = watchdogCompDistDb;\r
204         }\r
205 \r
206         public WatchdogServiceModVerIdLookupDb getWatchdogSerlookupDb() {\r
207                 if(watchdogSerlookupDb == null) {\r
208                         watchdogSerlookupDb =  WatchdogServiceModVerIdLookupDb.getInstance();\r
209                 }\r
210                 return watchdogSerlookupDb;\r
211         }\r
212 \r
213         public void setWatchdogSerlookupDb(WatchdogServiceModVerIdLookupDb watchdogSerlookupDb) {\r
214                 this.watchdogSerlookupDb = watchdogSerlookupDb;\r
215         }\r
216 \r
217         public CatalogDatabase getCatalogDb() {\r
218                 if(catalogDb == null) {\r
219                         catalogDb = CatalogDatabase.getInstance();\r
220                 }\r
221                 return catalogDb;\r
222         }\r
223 \r
224         public void setCatalogDb(CatalogDatabase catalogDb) {\r
225                 this.catalogDb = catalogDb;\r
226         }\r
227 \r
228         public AAIResourcesClient getAaiClient() {\r
229                 if(aaiClient == null) {\r
230                         aaiClient = new AAIResourcesClient();\r
231                 }\r
232                 return aaiClient;\r
233         }\r
234 \r
235         public void setAaiClient(AAIResourcesClient aaiClient) {\r
236                 this.aaiClient = aaiClient;\r
237         }\r
238         \r
239 }\r