1c2f6cf9ac0ff34acf1eb497f6d00c0a38949939
[aai/cacher.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.cacher.injestion.parser.strategy;
21
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import org.onap.aai.cacher.injestion.parser.AAIResourcesUriTemplates;
26 import org.onap.aai.cacher.model.CacheEntry;
27 import org.onap.aai.cacher.model.DBAction;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
30 import org.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Component;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37  * AAI resource get all parser strategy
38  */
39 @Component(value = "aai-resource-get-all")
40 @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
41 public class AAIResourceGetAllPayloadParserStrategy implements PayloadParserStrategy {
42
43     AAIResourcesUriTemplates aaiResourcesUriTemplates;
44
45     @Autowired
46     public AAIResourceGetAllPayloadParserStrategy(AAIResourcesUriTemplates aaiResourcesUriTemplates) {
47         this.aaiResourcesUriTemplates = aaiResourcesUriTemplates;
48     }
49
50     /**
51      * Parses aai resources specific payloads generating the details for .
52      * 
53      * @param originalKey
54      * @param jsonObject
55      * @return
56      */
57     @Override
58     public List<CacheEntry> process(String originalKey, JsonObject jsonObject) {
59         final List<CacheEntry> cacheEntries = new ArrayList<>();
60
61         String type = jsonObject.entrySet().iterator().next().getKey();
62
63         JsonArray ja = jsonObject.getAsJsonArray(type);
64         CacheEntry cacheEntry;
65         String uri;
66         JsonObject jo;
67         for (JsonElement jsonElement : ja) {
68             jo = jsonElement.getAsJsonObject();
69             uri = aaiResourcesUriTemplates.getUri(type, jo);
70             jsonObject.addProperty("_id", uri);
71             cacheEntry = CacheEntry.CacheEntryBuilder.createCacheEntry().withId(uri).inCollection(originalKey)
72                     .withFindQuery(getFindQuery(uri)).withPayload(jo).withDbAction(DBAction.INSERT_REPLACE).build();
73             cacheEntries.add(cacheEntry);
74         }
75
76         return cacheEntries;
77     }
78
79     protected JsonObject getFindQuery(String uri) {
80         JsonObject jo = new JsonObject();
81         jo.addProperty("_id", uri);
82         return jo;
83     }
84
85 }