Implant vid-app-common org.onap.vid.job (main and test)
[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 org.onap.vid.model.Action;
25 import org.onap.vid.mso.model.ModelInfo;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import static java.util.Collections.emptyMap;
31 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
32
33 public class CommandParentData {
34
35
36     public enum CommandDataKey{
37         SERVICE_MODEL_INFO,
38         SERVICE_INSTANCE_ID,
39         VNF_INSTANCE_ID,
40         VNF_MODEL_INFO,
41         VG_INSTANCE_ID,
42         VNF_GROUP_INSTANCE_ID,
43         ;
44     }
45
46     private static final String RESOURCE_INSTANCE_IDS = "resourceInstancesIds";
47     private static final String RESOURCE_MODEL_INFOS = "resourceModelInfos";
48
49     private final TypeReference<Map<CommandDataKey, String>> mapCommandKeyToString =
50             new TypeReference<Map<CommandDataKey, String>> () {};
51
52     private  final TypeReference<Map<CommandDataKey, ModelInfo>> mapCommandKeyToModelInfo =
53             new TypeReference<Map<CommandDataKey, ModelInfo>> () {};
54
55
56     private Map<CommandDataKey,ModelInfo> getModelInfosByCommandData(Map<String, Object> commandData) {
57         Object object = commandData.getOrDefault(RESOURCE_MODEL_INFOS, emptyMap());
58         return JACKSON_OBJECT_MAPPER.convertValue(object, mapCommandKeyToModelInfo);
59     }
60
61     private Map<CommandDataKey,String> getInstanceIdsByCommandData(Map<String, Object> commandData) {
62         Object object = commandData.getOrDefault(RESOURCE_INSTANCE_IDS, emptyMap());
63         return JACKSON_OBJECT_MAPPER.convertValue(object, mapCommandKeyToString);
64     }
65
66     public Map<String, Object> getParentData() {
67         Map<String, Object> data = new HashMap<>();
68         data.put(RESOURCE_INSTANCE_IDS, resourceInstancesIds);
69         data.put(RESOURCE_MODEL_INFOS, resourceModelInfos);
70
71         if (actionPhase != null) {
72                 data.put(ResourceCommandKt.ACTION_PHASE, actionPhase);
73         }
74
75         return data;
76     }
77     private Map<CommandDataKey, String> resourceInstancesIds = new HashMap<>();
78     private Map<CommandDataKey, ModelInfo> resourceModelInfos = new HashMap<>();
79     private Action actionPhase = null;
80
81     public void addModelInfo(CommandDataKey modelInfoKey, ModelInfo modelInfo) {
82         resourceModelInfos.put(modelInfoKey, modelInfo);
83     }
84
85     public void addInstanceId(CommandDataKey instanceIdKey, String instanceId) {
86          resourceInstancesIds.put(instanceIdKey, instanceId);
87     }
88
89     public void setActionPhase(Action actionPhase) {
90         this.actionPhase = actionPhase;
91     }
92
93     public ModelInfo getModelInfo(CommandDataKey modelInfoKey) {
94         return resourceModelInfos.get(modelInfoKey);
95     }
96
97     public String getInstanceId(CommandDataKey instanceIdKey) {
98         return resourceInstancesIds.get(instanceIdKey);
99     }
100
101     public CommandParentData initParentData(Map<String, Object> commandData) {
102         resourceModelInfos = getModelInfosByCommandData(commandData);
103         resourceInstancesIds = getInstanceIdsByCommandData(commandData);
104         return this;
105     }
106
107 }