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.findById(distributionId)
72 .orElseGet( () -> null);
73 if(watchdogDistributionStatus == null){
74 watchdogDistributionStatus = new WatchdogDistributionStatus();
75 watchdogDistributionStatus.setDistributionId(distributionId);
76 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
78 String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
80 if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
81 LOGGER.debug("Ignoring to update WatchdogDistributionStatus as distributionId: " + distributionId + " status is set to: " + distributionStatus);
82 return DistributionStatus.TIMEOUT.name();
84 List<WatchdogComponentDistributionStatus> results = watchdogCDStatusRepository.findByDistributionId(distributionId);
85 LOGGER.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: " + distributionId);
87 //*************************************************************************************************************************************************
88 //**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC
89 //**************************************************************************************************************************************************
91 List<WatchdogComponentDistributionStatus> cdStatuses = watchdogCDStatusRepository.findByDistributionId(distributionId);
93 boolean allComponentsComplete = true;
95 for(String name : componentNames ) {
97 boolean match = false;
99 for(WatchdogComponentDistributionStatus cdStatus: cdStatuses){
100 if(name.equals(cdStatus.getComponentName())){
101 LOGGER.debug("Found componentName " + name + " in the WatchDog Component DB");
107 LOGGER.debug(name + " has not be updated in the the WatchDog Component DB yet, so ending the loop");
108 allComponentsComplete = false;
114 if(allComponentsComplete) {
115 LOGGER.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
117 for(WatchdogComponentDistributionStatus componentDist : results) {
118 String componentDistributionStatus = componentDist.getComponentDistributionStatus();
119 LOGGER.debug("Component status: " + componentDistributionStatus + " on componentName: " + componentDist.getComponentName());
120 if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
121 status = DistributionStatus.FAILURE.name();
123 } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
124 status = DistributionStatus.SUCCESS.name();
126 throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
130 LOGGER.debug("Updating overall DistributionStatus to: " + status + " for distributionId: " + distributionId);
132 watchdogDistributionStatus.setDistributionIdStatus(status);
133 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
135 LOGGER.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
136 status = DistributionStatus.INCOMPLETE.name();
140 }catch (Exception e) {
141 LOGGER.debug("Exception occurred on getOverallDistributionStatus : " + e.getMessage());
143 throw new Exception(e);
145 LOGGER.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
149 public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
150 LOGGER.debug("Entered executePatchAAI method with distrubutionId: " + distributionId + " and distributionStatus: " + distributionStatus);
153 WatchdogServiceModVerIdLookup lookup = watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
154 String serviceModelVersionId = "";
157 serviceModelVersionId = lookup.getServiceModelVersionId();
160 LOGGER.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: " + distributionId + " and serviceModelVersionId: " + serviceModelVersionId);
161 LOGGER.debug("ASDC Notification ServiceModelInvariantUUID : " + serviceModelInvariantUUID);
163 if(serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
164 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
166 throw new Exception(error);
169 AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);
170 aaiUri.depth(Depth.ZERO); //Do not return relationships if any
171 LOGGER.debug("Target A&AI Resource URI: " + aaiUri.build().toString());
173 Map<String, String> payload = new HashMap<>();
174 payload.put("distribution-status", distributionStatus);
175 getAaiClient().update(aaiUri, payload);
177 LOGGER.debug("A&AI UPDATE MODEL Version is success!");
178 } catch (Exception e) {
179 LOGGER.debug("Exception occurred on executePatchAAI : " + e.getMessage());
181 throw new Exception(e);
185 public AAIResourcesClient getAaiClient() {
186 if(aaiClient == null) {
187 aaiClient = new AAIResourcesClient();
192 public void setAaiClient(AAIResourcesClient aaiClient) {
193 this.aaiClient = aaiClient;