2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.asdc.tenantIsolation;
23 import java.util.HashMap;
24 import java.util.List;
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;
45 public class WatchdogDistribution {
47 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,WatchdogDistribution.class);
49 private AAIResourcesClient aaiClient;
52 private WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
55 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
58 private WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;
61 private ServiceRepository serviceRepo;
63 @Value("${mso.asdc.config.components.componentNames}")
64 private String[] componentNames;
66 public String getOverallDistributionStatus(String distributionId) throws Exception {
67 LOGGER.debug("Entered getOverallDistributionStatus method for distrubutionId: " + distributionId);
71 WatchdogDistributionStatus watchdogDistributionStatus = watchdogDistributionStatusRepository.findOne(distributionId);
72 if(watchdogDistributionStatus == null){
73 watchdogDistributionStatus = new WatchdogDistributionStatus();
74 watchdogDistributionStatus.setDistributionId(distributionId);
75 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
77 String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
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();
83 List<WatchdogComponentDistributionStatus> results = watchdogCDStatusRepository.findByDistributionId(distributionId);
84 LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);
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 //**************************************************************************************************************************************************
90 List<WatchdogComponentDistributionStatus> cdStatuses = watchdogCDStatusRepository.findByDistributionId(distributionId);
92 boolean allComponentsComplete = true;
94 for(String name : componentNames ) {
96 boolean match = false;
98 for(WatchdogComponentDistributionStatus cdStatus: cdStatuses){
99 if(name.equals(cdStatus.getComponentName())){
100 LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");
106 LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");
107 allComponentsComplete = false;
113 if(allComponentsComplete) {
114 LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
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();
122 } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
123 status = DistributionStatus.SUCCESS.name();
125 throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
129 LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);
131 watchdogDistributionStatus.setDistributionIdStatus(status);
132 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
134 LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
135 status = DistributionStatus.INCOMPLETE.name();
139 }catch (Exception e) {
140 LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());
142 throw new Exception(e);
144 LOGGER.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
148 public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
149 LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);
152 WatchdogServiceModVerIdLookup lookup = watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
153 String serviceModelVersionId = "";
156 serviceModelVersionId = lookup.getServiceModelVersionId();
159 LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);
160 LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);
162 if(serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
163 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
165 throw new Exception(error);
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());
172 Map<String, String> payload = new HashMap<>();
173 payload.put("distribution-status", distributionStatus);
174 getAaiClient().update(aaiUri, payload);
176 LOGGER.debug("A&AI UPDATE MODEL Version is success!");
177 } catch (Exception e) {
178 LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());
180 throw new Exception(e);
184 public AAIResourcesClient getAaiClient() {
185 if(aaiClient == null) {
186 aaiClient = new AAIResourcesClient();
191 public void setAaiClient(AAIResourcesClient aaiClient) {
192 this.aaiClient = aaiClient;