2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.asdc.tenantIsolation;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.onap.so.client.aai.AAIObjectType;
30 import org.onap.so.client.aai.AAIResourcesClient;
31 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
32 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
33 import org.onap.so.client.graphinventory.entities.uri.Depth;
34 import org.onap.so.db.catalog.data.repository.ServiceRepository;
35 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
36 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
37 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
38 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
39 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
40 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.stereotype.Component;
48 public class WatchdogDistribution {
50 private static final Logger logger = LoggerFactory.getLogger(WatchdogDistribution.class);
52 private AAIResourcesClient aaiClient;
55 private WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
58 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
61 private WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;
64 private ServiceRepository serviceRepo;
66 @Value("${mso.asdc.config.components.componentNames}")
67 private String[] componentNames;
69 public String getOverallDistributionStatus(String distributionId) throws Exception {
70 logger.debug("Entered getOverallDistributionStatus method for distrubutionId: {}", distributionId);
74 WatchdogDistributionStatus watchdogDistributionStatus = watchdogDistributionStatusRepository.findById(distributionId)
75 .orElseGet( () -> null);
76 if(watchdogDistributionStatus == null){
77 watchdogDistributionStatus = new WatchdogDistributionStatus();
78 watchdogDistributionStatus.setDistributionId(distributionId);
79 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
81 String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
83 if(DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
84 logger.debug("Ignoring to update WatchdogDistributionStatus as distributionId: {} status is set to: {}",
85 distributionId, distributionStatus);
86 return DistributionStatus.TIMEOUT.name();
88 List<WatchdogComponentDistributionStatus> results = watchdogCDStatusRepository.findByDistributionId(distributionId);
89 logger.debug("Executed RequestDB getWatchdogComponentDistributionStatus for distrubutionId: {}",
92 //*************************************************************************************************************************************************
93 //**** Compare config values verse DB watchdog component names to see if every component has reported status before returning final result back to ASDC
94 //**************************************************************************************************************************************************
96 List<WatchdogComponentDistributionStatus> cdStatuses = watchdogCDStatusRepository.findByDistributionId(distributionId);
98 boolean allComponentsComplete = true;
100 for(String name : componentNames ) {
102 boolean match = false;
104 for(WatchdogComponentDistributionStatus cdStatus: cdStatuses){
105 if(name.equals(cdStatus.getComponentName())){
106 logger.debug("Found componentName {} in the WatchDog Component DB", name);
112 logger.debug("{} has not be updated in the the WatchDog Component DB yet, so ending the loop", name);
113 allComponentsComplete = false;
119 if(allComponentsComplete) {
120 logger.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
122 for(WatchdogComponentDistributionStatus componentDist : results) {
123 String componentDistributionStatus = componentDist.getComponentDistributionStatus();
124 logger.debug("Component status: {} on componentName: ", componentDistributionStatus, componentDist
125 .getComponentName());
126 if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
127 status = DistributionStatus.FAILURE.name();
129 } else if(componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
130 status = DistributionStatus.SUCCESS.name();
132 throw new Exception("Invalid Component distribution status: " + componentDistributionStatus);
136 logger.debug("Updating overall DistributionStatus to: {} for distributionId: ", status, distributionId);
138 watchdogDistributionStatus.setDistributionIdStatus(status);
139 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
141 logger.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
142 status = DistributionStatus.INCOMPLETE.name();
146 }catch (Exception e) {
147 logger.debug("Exception occurred on getOverallDistributionStatus : {}", e.getMessage());
148 logger.error("Exception occurred", e);
149 throw new Exception(e);
151 logger.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
155 public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus) throws Exception {
156 logger.debug("Entered executePatchAAI method with distrubutionId: {} and distributionStatus: ", distributionId,
160 WatchdogServiceModVerIdLookup lookup = watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
161 String serviceModelVersionId = "";
164 serviceModelVersionId = lookup.getServiceModelVersionId();
167 logger.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: {} "
168 + "and serviceModelVersionId: {}", distributionId, serviceModelVersionId);
169 logger.debug("ASDC Notification ServiceModelInvariantUUID : {}", serviceModelInvariantUUID);
171 if(serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
172 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
174 throw new Exception(error);
177 AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID, serviceModelVersionId);
178 aaiUri.depth(Depth.ZERO); //Do not return relationships if any
179 logger.debug("Target A&AI Resource URI: {}", aaiUri.build().toString());
181 Map<String, String> payload = new HashMap<>();
182 payload.put("distribution-status", distributionStatus);
183 getAaiClient().update(aaiUri, payload);
185 logger.debug("A&AI UPDATE MODEL Version is success!");
186 } catch (Exception e) {
187 logger.debug("Exception occurred on executePatchAAI : {}", e.getMessage());
188 logger.error("Exception occurred", e);
189 throw new Exception(e);
193 public AAIResourcesClient getAaiClient() {
194 if(aaiClient == null) {
195 aaiClient = new AAIResourcesClient();
200 public void setAaiClient(AAIResourcesClient aaiClient) {
201 this.aaiClient = aaiClient;