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;
28 import org.onap.so.client.aai.AAIObjectType;
29 import org.onap.so.client.aai.AAIResourcesClient;
30 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
31 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
32 import org.onap.so.client.graphinventory.entities.uri.Depth;
33 import org.onap.so.db.catalog.data.repository.ServiceRepository;
34 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
35 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
36 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
37 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
38 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
39 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.stereotype.Component;
47 public class WatchdogDistribution {
49 private static final Logger logger = LoggerFactory.getLogger(WatchdogDistribution.class);
51 private AAIResourcesClient aaiClient;
54 private WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
57 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
60 private WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;
63 private ServiceRepository serviceRepo;
65 @Value("${mso.asdc.config.components.componentNames}")
66 private String[] componentNames;
68 public String getOverallDistributionStatus(String distributionId) throws Exception {
69 logger.debug("Entered getOverallDistributionStatus method for distrubutionId: {}", distributionId);
73 WatchdogDistributionStatus watchdogDistributionStatus =
74 watchdogDistributionStatusRepository.findById(distributionId).orElseGet(() -> null);
75 if (watchdogDistributionStatus == null) {
76 watchdogDistributionStatus = new WatchdogDistributionStatus();
77 watchdogDistributionStatus.setDistributionId(distributionId);
78 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
80 String distributionStatus = watchdogDistributionStatus.getDistributionIdStatus();
82 if (DistributionStatus.TIMEOUT.name().equalsIgnoreCase(distributionStatus)) {
83 logger.debug("Ignoring to update WatchdogDistributionStatus as distributionId: {} status is set to: {}",
84 distributionId, distributionStatus);
85 return DistributionStatus.TIMEOUT.name();
87 List<WatchdogComponentDistributionStatus> results =
88 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
94 // status before returning final result back to ASDC
95 // **************************************************************************************************************************************************
97 List<WatchdogComponentDistributionStatus> cdStatuses =
98 watchdogCDStatusRepository.findByDistributionId(distributionId);
100 boolean allComponentsComplete = true;
102 for (String name : componentNames) {
104 boolean match = false;
106 for (WatchdogComponentDistributionStatus cdStatus : cdStatuses) {
107 if (name.equals(cdStatus.getComponentName())) {
108 logger.debug("Found componentName {} in the WatchDog Component DB", name);
114 logger.debug("{} has not be updated in the the WatchDog Component DB yet, so ending the loop",
116 allComponentsComplete = false;
122 if (allComponentsComplete) {
123 logger.debug("Components Size matched with the WatchdogComponentDistributionStatus results.");
125 for (WatchdogComponentDistributionStatus componentDist : results) {
126 String componentDistributionStatus = componentDist.getComponentDistributionStatus();
127 logger.debug("Component status: {} on componentName: ", componentDistributionStatus,
128 componentDist.getComponentName());
129 if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_ERROR")) {
130 status = DistributionStatus.FAILURE.name();
132 } else if (componentDistributionStatus.equalsIgnoreCase("COMPONENT_DONE_OK")) {
133 status = DistributionStatus.SUCCESS.name();
136 "Invalid Component distribution status: " + componentDistributionStatus);
140 logger.debug("Updating overall DistributionStatus to: {} for distributionId: ", status,
143 watchdogDistributionStatus.setDistributionIdStatus(status);
144 watchdogDistributionStatusRepository.save(watchdogDistributionStatus);
146 logger.debug("Components Size Didn't match with the WatchdogComponentDistributionStatus results.");
147 status = DistributionStatus.INCOMPLETE.name();
151 } catch (Exception e) {
152 logger.debug("Exception occurred on getOverallDistributionStatus : {}", e.getMessage());
153 logger.error("Exception occurred", e);
154 throw new Exception(e);
156 logger.debug("Exiting getOverallDistributionStatus method in WatchdogDistribution");
160 public void executePatchAAI(String distributionId, String serviceModelInvariantUUID, String distributionStatus)
162 logger.debug("Entered executePatchAAI method with distrubutionId: {} and distributionStatus: ", distributionId,
166 WatchdogServiceModVerIdLookup lookup =
167 watchdogModVerIdLookupRepository.findOneByDistributionId(distributionId);
168 String serviceModelVersionId = "";
170 if (lookup != null) {
171 serviceModelVersionId = lookup.getServiceModelVersionId();
174 logger.debug("Executed RequestDB getWatchdogServiceModVerIdLookup with distributionId: {} "
175 + "and serviceModelVersionId: {}", distributionId, serviceModelVersionId);
176 logger.debug("ASDC Notification ServiceModelInvariantUUID : {}", serviceModelInvariantUUID);
178 if (serviceModelInvariantUUID == null || "".equals(serviceModelVersionId)) {
179 String error = "No Service found with serviceModelInvariantUUID: " + serviceModelInvariantUUID;
181 throw new Exception(error);
184 AAIResourceUri aaiUri = AAIUriFactory.createResourceUri(AAIObjectType.MODEL_VER, serviceModelInvariantUUID,
185 serviceModelVersionId);
186 aaiUri.depth(Depth.ZERO); // Do not return relationships if any
187 logger.debug("Target A&AI Resource URI: {}", aaiUri.build().toString());
189 Map<String, String> payload = new HashMap<>();
190 payload.put("distribution-status", distributionStatus);
191 getAaiClient().update(aaiUri, payload);
193 logger.debug("A&AI UPDATE MODEL Version is success!");
194 } catch (Exception e) {
195 logger.debug("Exception occurred on executePatchAAI : {}", e.getMessage());
196 logger.error("Exception occurred", e);
197 throw new Exception(e);
201 public AAIResourcesClient getAaiClient() {
202 if (aaiClient == null) {
203 aaiClient = new AAIResourcesClient();
208 public void setAaiClient(AAIResourcesClient aaiClient) {
209 this.aaiClient = aaiClient;