35f631183e8a4460f29b3fad95ca1c170817b198
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / CommandParentData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.vid.job.command;
22
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.vid.mso.model.ModelInfo;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 public class CommandParentData {
31
32
33     public enum CommandDataKey{
34         SERVICE_MODEL_INFO,
35         SERVICE_INSTANCE_ID,
36         VNF_INSTANCE_ID,
37         VNF_MODEL_INFO,
38         VG_INSTANCE_ID,
39         ;
40     }
41
42     private static final String RESOURCE_INSTANCE_IDS = "resourceInstancesIds";
43     private static final String RESOURCE_MODEL_INFOS = "resourceModelInfos";
44
45     private final TypeReference<Map<CommandDataKey, String>> mapCommandKeyToString =
46             new TypeReference<Map<CommandDataKey, String>> () {};
47
48     private  final TypeReference<Map<CommandDataKey, ModelInfo>> mapCommandKeyToModelInfo =
49             new TypeReference<Map<CommandDataKey, ModelInfo>> () {};
50
51
52     private ObjectMapper objectMapper = new ObjectMapper();
53
54     private Map<CommandDataKey,ModelInfo> getModelInfosByCommandData(Map<String, Object> commandData) {
55         Object object = commandData.get(RESOURCE_MODEL_INFOS);
56         if (object != null) {
57             return objectMapper.convertValue(object, mapCommandKeyToModelInfo);
58         }
59         return null;
60     }
61
62     private Map<CommandDataKey,String> getInstanceIdsByCommandData(Map<String, Object> commandData) {
63         Object object = commandData.get(RESOURCE_INSTANCE_IDS);
64         if (object != null) {
65             return objectMapper.convertValue(object, mapCommandKeyToString);
66         }
67         return null;
68     }
69
70     public Map<String, Object> getParentData() {
71         Map<String, Object> data = new HashMap<>();
72         data.put(RESOURCE_INSTANCE_IDS, resourceInstancesIds);
73         data.put(RESOURCE_MODEL_INFOS, resourceModelInfos);
74         return data;
75     }
76     private Map<CommandDataKey, String> resourceInstancesIds = new HashMap<>();
77     private Map<CommandDataKey, ModelInfo> resourceModelInfos = new HashMap<>();
78
79     public void addModelInfo(CommandDataKey modelInfoKey, ModelInfo modelInfo) {
80         resourceModelInfos.put(modelInfoKey, modelInfo);
81     }
82
83     public void addInstanceId(CommandDataKey instanceIdKey, String instanceId) {
84          resourceInstancesIds.put(instanceIdKey, instanceId);
85     }
86     public ModelInfo getModelInfo(CommandDataKey modelInfoKey) {
87         return resourceModelInfos.get(modelInfoKey);
88     }
89
90     public String getInstanceId(CommandDataKey instanceIdKey) {
91         return resourceInstancesIds.get(instanceIdKey);
92     }
93
94     public CommandParentData initParentData(Map<String, Object> commandData) {
95         resourceModelInfos = getModelInfosByCommandData(commandData);
96         resourceInstancesIds = getInstanceIdsByCommandData(commandData);
97         return this;
98     }
99
100 }