6ccd1a2cab3725542a9e73dd9df4b79d528659a9
[so.git] /
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.aaiclient.client.graphinventory;
22
23 import java.net.URI;
24 import java.util.Optional;
25 import javax.ws.rs.client.ClientBuilder;
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.core.Response;
28 import org.javatuples.Pair;
29 import org.onap.aaiclient.client.CacheControlFeature;
30 import org.onap.aaiclient.client.FlushCache;
31 import org.onap.logging.filter.base.ONAPComponentsList;
32 import org.onap.so.client.AddCacheHeaders;
33 import org.onap.so.client.CacheFactory;
34 import org.onap.so.client.ResponseExceptionMapper;
35 import org.onap.so.client.RestClientSSL;
36 import org.onap.so.client.RestProperties;
37 import org.onap.so.client.policy.CommonObjectMapperProvider;
38
39 public abstract class GraphInventoryRestClient extends RestClientSSL {
40
41     protected static final GraphInventoryCommonObjectMapperProvider standardProvider =
42             new GraphInventoryCommonObjectMapperProvider();
43
44     protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
45
46     protected GraphInventoryRestClient(RestProperties props, URI uri) {
47         super(props, Optional.of(uri));
48     }
49
50
51     protected ClientBuilder enableCaching(ClientBuilder builder) {
52         builder.register(new AddCacheHeaders(props.getCacheProperties()));
53         builder.register(new FlushCache(props.getCacheProperties()));
54         CacheControlFeature cacheControlFeature = new CacheControlFeature();
55         cacheControlFeature.setCacheResponseInputStream(true);
56         cacheControlFeature.setExpiryPolicyFactory(new CacheFactory(props.getCacheProperties()));
57         builder.property("org.onap.aaiclient.client.CacheControlFeature.name",
58                 props.getCacheProperties().getCacheName());
59
60         builder.register(cacheControlFeature);
61
62         return builder;
63     }
64
65     @Override
66     public abstract ONAPComponentsList getTargetEntity();
67
68     @Override
69     protected abstract void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap);
70
71     @Override
72     protected abstract Optional<ResponseExceptionMapper> addResponseExceptionMapper();
73
74     @Override
75     protected CommonObjectMapperProvider getCommonObjectMapperProvider() {
76         return standardProvider;
77     }
78
79     @Override
80     public Response patch(Object obj) {
81         return super.patch(convertToPatchFormat(obj));
82     }
83
84     @Override
85     public <T> T patch(Object obj, Class<T> resultClass) {
86         return super.patch(convertToPatchFormat(obj), resultClass);
87     }
88
89     protected GraphInventoryPatchConverter getPatchConverter() {
90         return this.patchConverter;
91     }
92
93     protected String convertToPatchFormat(Object obj) {
94         return getPatchConverter().convertPatchFormat(obj);
95     }
96
97 }