7b9a8c531277eec752dcce22ce4f44b4184d109e
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / adapter / vnf / VnfAdapterClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.client.adapter.vnf;
22
23 import javax.ws.rs.InternalServerErrorException;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.UriBuilder;
26
27 import org.onap.so.adapters.vnfrest.CreateVfModuleRequest;
28 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
29 import org.onap.so.adapters.vnfrest.DeleteVfModuleRequest;
30 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
31 import org.onap.so.adapters.vnfrest.QueryVfModuleResponse;
32 import org.onap.so.adapters.vnfrest.RollbackVfModuleRequest;
33 import org.onap.so.adapters.vnfrest.RollbackVfModuleResponse;
34 import org.onap.so.adapters.vnfrest.UpdateVfModuleRequest;
35 import org.onap.so.adapters.vnfrest.UpdateVfModuleResponse;
36 import org.onap.so.client.adapter.rest.AdapterRestClient;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class VnfAdapterClientImpl implements VnfAdapterClient {
41
42         private static final String VF_MODULES = "/vf-modules/";
43
44         private VnfAdapterRestProperties props;
45
46         public VnfAdapterClientImpl() {
47                 this.props = new VnfAdapterRestProperties();
48         }
49         
50         public VnfAdapterClientImpl(VnfAdapterRestProperties props) {
51                 this.props = props;
52         }
53
54         @Override
55         public CreateVfModuleResponse createVfModule(String aaiVnfId, CreateVfModuleRequest req)
56                         throws VnfAdapterClientException {
57                 try {
58                         return new AdapterRestClient(this.props, this.getUri("/" + aaiVnfId + "/vf-modules").build()).post(req,
59                                         CreateVfModuleResponse.class);
60                 } catch (InternalServerErrorException e) {
61                         throw new VnfAdapterClientException(e.getMessage());
62                 }
63         }
64
65         @Override
66         public RollbackVfModuleResponse rollbackVfModule(String aaiVnfId, String aaiVfModuleId, RollbackVfModuleRequest req)
67                         throws VnfAdapterClientException {
68                 try {
69                         return new AdapterRestClient(this.props,
70                                         this.getUri("/" + aaiVnfId + VF_MODULES + aaiVfModuleId + "/rollback").build()).delete(req,
71                                                         RollbackVfModuleResponse.class);
72                 } catch (InternalServerErrorException e) {
73                         throw new VnfAdapterClientException(e.getMessage());
74                 }
75         }
76
77         @Override
78         public DeleteVfModuleResponse deleteVfModule(String aaiVnfId, String aaiVfModuleId, DeleteVfModuleRequest req)
79                         throws VnfAdapterClientException {
80                 try {
81                         return new AdapterRestClient(this.props, this.getUri("/" + aaiVnfId + VF_MODULES + aaiVfModuleId).build())
82                                         .delete(req, DeleteVfModuleResponse.class);
83                 } catch (InternalServerErrorException e) {
84                         throw new VnfAdapterClientException(e.getMessage());
85                 }
86         }
87
88         @Override
89         public UpdateVfModuleResponse updateVfModule(String aaiVnfId, String aaiVfModuleId, UpdateVfModuleRequest req)
90                         throws VnfAdapterClientException {
91                 try {
92                         return new AdapterRestClient(this.props, this.getUri("/" + aaiVnfId + VF_MODULES + aaiVfModuleId).build())
93                                         .put(req, UpdateVfModuleResponse.class);
94                 } catch (InternalServerErrorException e) {
95                         throw new VnfAdapterClientException(e.getMessage());
96                 }
97         }
98
99         @Override
100         public QueryVfModuleResponse queryVfModule(String aaiVnfId, String aaiVfModuleId, String cloudSiteId,
101                         String tenantId, String vfModuleName, boolean skipAAI, String requestId, String serviceInstanceId)
102                         throws VnfAdapterClientException {
103                 UriBuilder builder = this.getUri("/" + aaiVnfId + VF_MODULES + aaiVfModuleId);
104                 if (cloudSiteId != null) {
105                         builder.queryParam("cloudSiteId", cloudSiteId);
106                 }
107                 if (tenantId != null) {
108                         builder.queryParam("tenantId", tenantId);
109                 }
110                 if (vfModuleName != null) {
111                         builder.queryParam("vfModuleName", vfModuleName);
112                 }
113
114                 builder.queryParam("skipAAI", skipAAI);
115
116                 if (requestId != null) {
117                         builder.queryParam("msoRequest.requestId", requestId);
118                 }
119                 if (serviceInstanceId != null) {
120                         builder.queryParam("msoRequest.serviceInstanceId", serviceInstanceId);
121                 }
122                 try {
123                         return new AdapterRestClient(this.props, builder.build(), MediaType.APPLICATION_JSON,
124                                         MediaType.APPLICATION_JSON).get(QueryVfModuleResponse.class).get();
125                 } catch (InternalServerErrorException e) {
126                         throw new VnfAdapterClientException(e.getMessage());
127                 }
128         }
129
130         public UriBuilder getUri(String path) {
131                 return UriBuilder.fromPath(path);
132         }
133
134 }