2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import org.onap.vid.model.Action;
25 import org.onap.vid.mso.model.ModelInfo;
27 import java.util.HashMap;
30 import static java.util.Collections.emptyMap;
31 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
33 public class CommandParentData {
36 public enum CommandDataKey{
42 VNF_GROUP_INSTANCE_ID,
46 private static final String RESOURCE_INSTANCE_IDS = "resourceInstancesIds";
47 private static final String RESOURCE_MODEL_INFOS = "resourceModelInfos";
49 private Map<CommandDataKey, String> resourceInstancesIds = new HashMap<>();
50 private Map<CommandDataKey, ModelInfo> resourceModelInfos = new HashMap<>();
51 private Action actionPhase = null;
53 private final TypeReference<Map<CommandDataKey, String>> mapCommandKeyToString =
54 new TypeReference<Map<CommandDataKey, String>> () {};
56 private final TypeReference<Map<CommandDataKey, ModelInfo>> mapCommandKeyToModelInfo =
57 new TypeReference<Map<CommandDataKey, ModelInfo>> () {};
60 private Map<CommandDataKey,ModelInfo> getModelInfosByCommandData(Map<String, Object> commandData) {
61 Object object = commandData.getOrDefault(RESOURCE_MODEL_INFOS, emptyMap());
62 return JACKSON_OBJECT_MAPPER.convertValue(object, mapCommandKeyToModelInfo);
65 private Map<CommandDataKey,String> getInstanceIdsByCommandData(Map<String, Object> commandData) {
66 Object object = commandData.getOrDefault(RESOURCE_INSTANCE_IDS, emptyMap());
67 return JACKSON_OBJECT_MAPPER.convertValue(object, mapCommandKeyToString);
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);
75 if (actionPhase != null) {
76 data.put(ResourceCommandKt.ACTION_PHASE, actionPhase);
82 public void addModelInfo(CommandDataKey modelInfoKey, ModelInfo modelInfo) {
83 resourceModelInfos.put(modelInfoKey, modelInfo);
86 public void addInstanceId(CommandDataKey instanceIdKey, String instanceId) {
87 resourceInstancesIds.put(instanceIdKey, instanceId);
90 public void setActionPhase(Action actionPhase) {
91 this.actionPhase = actionPhase;
94 public ModelInfo getModelInfo(CommandDataKey modelInfoKey) {
95 return resourceModelInfos.get(modelInfoKey);
98 public String getInstanceId(CommandDataKey instanceIdKey) {
99 return resourceInstancesIds.get(instanceIdKey);
102 public CommandParentData initParentData(Map<String, Object> commandData) {
103 resourceModelInfos = getModelInfosByCommandData(commandData);
104 resourceInstancesIds = getInstanceIdsByCommandData(commandData);