Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / adapter / network / NetworkAdapterClientImpl.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.network;
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.nwrest.CreateNetworkRequest;
28 import org.onap.so.adapters.nwrest.CreateNetworkResponse;
29 import org.onap.so.adapters.nwrest.DeleteNetworkRequest;
30 import org.onap.so.adapters.nwrest.DeleteNetworkResponse;
31 import org.onap.so.adapters.nwrest.QueryNetworkResponse;
32 import org.onap.so.adapters.nwrest.RollbackNetworkRequest;
33 import org.onap.so.adapters.nwrest.RollbackNetworkResponse;
34 import org.onap.so.adapters.nwrest.UpdateNetworkRequest;
35 import org.onap.so.adapters.nwrest.UpdateNetworkResponse;
36 import org.onap.so.client.adapter.rest.AdapterRestClient;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class NetworkAdapterClientImpl implements NetworkAdapterClient {
41
42         private final NetworkAdapterRestProperties props;
43
44         public NetworkAdapterClientImpl() {
45                 this.props = new NetworkAdapterRestProperties();
46         }
47
48         @Override
49         public CreateNetworkResponse createNetwork(CreateNetworkRequest req) throws NetworkAdapterClientException{
50                 try {
51                         return new AdapterRestClient(this.props, this.getUri("").build()).post(req,
52                                         CreateNetworkResponse.class);
53                 } catch (InternalServerErrorException e) {
54                         throw new NetworkAdapterClientException(e.getMessage());
55                 }
56         }
57
58         @Override
59         public DeleteNetworkResponse deleteNetwork(String aaiNetworkId, DeleteNetworkRequest req) throws NetworkAdapterClientException {
60                 try {
61                         return new AdapterRestClient(this.props, this.getUri("/" + aaiNetworkId).build()).delete(req,
62                                         DeleteNetworkResponse.class);
63                 } catch (InternalServerErrorException e) {
64                         throw new NetworkAdapterClientException(e.getMessage());
65                 }
66         }
67
68         @Override
69         public RollbackNetworkResponse rollbackNetwork(String aaiNetworkId, RollbackNetworkRequest req) throws NetworkAdapterClientException {
70                 try {
71                         return new AdapterRestClient(this.props, this.getUri("/" + aaiNetworkId).build()).delete(req,
72                                         RollbackNetworkResponse.class);
73                 } catch (InternalServerErrorException e) {
74                         throw new NetworkAdapterClientException(e.getMessage());
75                 }
76         }
77
78         @Override
79         public QueryNetworkResponse queryNetwork(String aaiNetworkId, String cloudSiteId, String tenantId,
80                         String networkStackId, boolean skipAAI, String requestId, String serviceInstanceId) throws NetworkAdapterClientException {
81                 UriBuilder builder = this.getUri("/" + aaiNetworkId);
82                 if (cloudSiteId != null) {
83                         builder.queryParam("cloudSiteId", cloudSiteId);
84                 }
85                 if (tenantId != null) {
86                         builder.queryParam("tenantId", tenantId);
87                 }
88                 if (networkStackId != null) {
89                         builder.queryParam("networkStackId", networkStackId);
90                 }
91
92                 builder.queryParam("skipAAI", skipAAI);
93
94                 if (requestId != null) {
95                         builder.queryParam("msoRequest.requestId", requestId);
96                 }
97                 if (serviceInstanceId != null) {
98                         builder.queryParam("msoRequest.serviceInstanceId", serviceInstanceId);
99                 }
100                 try {
101                         return new AdapterRestClient(this.props, builder.build(), MediaType.APPLICATION_XML, MediaType.APPLICATION_XML)
102                                 .get(QueryNetworkResponse.class).get();
103                 } catch (InternalServerErrorException e) {
104                         throw new NetworkAdapterClientException(e.getMessage());
105                 }
106         }
107
108         @Override
109         public UpdateNetworkResponse updateNetwork(String aaiNetworkId, UpdateNetworkRequest req) throws NetworkAdapterClientException {
110                 try {
111                         return new AdapterRestClient(this.props, this.getUri("/" + aaiNetworkId).build()).put(req,
112                                         UpdateNetworkResponse.class);
113                 } catch (InternalServerErrorException e) {
114                         throw new NetworkAdapterClientException(e.getMessage());
115                 }
116         }
117
118         protected UriBuilder getUri(String path) {
119                 return UriBuilder.fromPath(path);
120         }
121
122 }