Remove restricted notice from TOSCA file
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ServiceInstantiationCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.onap.vid.job.command;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.apache.commons.lang3.ObjectUtils;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.aai.ExceptionWithRequestInfo;
27 import org.onap.vid.changeManagement.RequestDetailsWrapper;
28 import org.onap.vid.exceptions.MaxRetriesException;
29 import org.onap.vid.job.Job;
30 import org.onap.vid.job.JobCommand;
31 import org.onap.vid.job.NextCommand;
32 import org.onap.vid.job.impl.JobSharedData;
33 import org.onap.vid.model.RequestReferencesContainer;
34 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
35 import org.onap.vid.mso.RestMsoImplementation;
36 import org.onap.vid.mso.RestObject;
37 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
38 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
39
40 import javax.inject.Inject;
41 import java.util.Map;
42
43
44 public abstract class ServiceInstantiationCommand extends BaseRootCommand implements JobCommand {
45
46     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
47
48     @Inject
49     protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
50
51     @Inject
52     private RestMsoImplementation restMso;
53
54     protected String optimisticUniqueServiceInstanceName;
55
56     public ServiceInstantiationCommand() {
57     }
58
59     @Override
60     public NextCommand call() {
61         RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
62         try {
63             requestDetailsWrapper = generateServiceInstantiationRequest();
64         }
65
66         //Aai return bad response while checking names uniqueness
67         catch (ExceptionWithRequestInfo exception) {
68             return handleAaiNameUniquenessBadResponse(exception);
69         }
70
71         //Vid reached to max retries while trying to find unique name in AAI
72         catch (MaxRetriesException exception) {
73             return handleMaxRetryInNameUniqueness(exception);
74         }
75
76         String path = asyncInstantiationBL.getServiceInstantiationPath(getRequest());
77
78         RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
79                 path, RequestReferencesContainer.class);
80
81         return handleRootResponse(msoResponse);
82
83     }
84
85     @Override
86     protected ServiceInstantiation getRequest() {
87         return (ServiceInstantiation) getSharedData().getRequest();
88     }
89
90     protected abstract RequestDetailsWrapper<ServiceInstantiationRequestDetails> generateServiceInstantiationRequest();
91
92     private NextCommand handleMaxRetryInNameUniqueness(MaxRetriesException exception) {
93         LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
94         return handleCommandFailed();
95     }
96
97     private NextCommand handleAaiNameUniquenessBadResponse(ExceptionWithRequestInfo exception) {
98         LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
99         //put the job in_progress so we will keep trying to check name uniqueness in AAI
100         //And then send the request to MSO
101         return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
102     }
103
104     @Override
105     public ServiceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
106
107         return init(
108                 sharedData,
109                 (String) commandData.get("optimisticUniqueServiceInstanceName")
110         );
111     }
112
113     protected ServiceInstantiationCommand init(JobSharedData sharedData, String optimisticUniqueServiceInstanceName) {
114         init(sharedData);
115         this.optimisticUniqueServiceInstanceName = ObjectUtils.defaultIfNull(optimisticUniqueServiceInstanceName,
116                 (getRequest()).getInstanceName());
117         return this;
118     }
119
120     @Override
121     public Map<String, Object> getData() {
122         return ImmutableMap.of(
123                 "optimisticUniqueServiceInstanceName", optimisticUniqueServiceInstanceName
124         );
125     }
126 }