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