2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.champ.entity;
23 import java.util.HashMap;
25 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
29 import org.onap.aai.champcore.model.ChampObject;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.champ.exception.ChampServiceException;
32 import org.onap.champ.util.ChampProperties;
33 import org.onap.champ.util.ChampServiceConstants;
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
38 public class ChampBulkOp {
39 private static final Gson gson = new GsonBuilder().create();
41 private static final String CREATED_TS_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_CREATED_TS_NAME);
42 private static final String LAST_MOD_TS_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_LAST_MOD_TS_NAME);
45 private String operation;
49 private String source;
50 private String target;
51 private Map<String, Object> properties;
54 public String toJson() {
55 return gson.toJson(this);
58 public static ChampBulkOp fromJson(String jsonString) {
59 return gson.fromJson(jsonString, ChampBulkOp.class);
62 public ChampObject toChampObject() throws ChampServiceException {
64 throw new ChampServiceException("Error constructing object from: " + toJson(), Status.INTERNAL_SERVER_ERROR);
67 ChampObject.Builder builder = new ChampObject.Builder(type);
70 builder = builder.key(id);
72 if (properties != null) {
74 //remove the create/updated timestamps as it cause mismatch issue while updating from graph
75 Map<String, Object> champProperties = properties.entrySet().stream()
76 .filter(x -> !(x.getKey().equals(CREATED_TS_NAME) || x.getKey().equals(LAST_MOD_TS_NAME)))
77 .collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
79 builder = builder.properties(champProperties);
82 return builder.build();
85 public ChampRelationship toChampRelationship() throws ChampServiceException {
86 if ( (type == null) || (source == null) || (target == null) ) {
87 throw new ChampServiceException("Error constructing relationship from: " + toJson(), Status.INTERNAL_SERVER_ERROR);
90 ChampObject srcObj = new ChampObject.Builder("").key(source).build();
91 ChampObject targetObj = new ChampObject.Builder("").key(target).build();
92 ChampRelationship.Builder builder = new ChampRelationship.Builder(srcObj, targetObj, type);
95 builder = builder.key(id);
97 if (properties != null) {
98 builder = builder.properties(properties);
101 return builder.build();
104 public String getId() {
108 public void setId(String id) {
112 public String getType() {
116 public void setType(String type) {
120 public Map<String, Object> getProperties() {
124 public Object getProperty(String key) {
125 return properties.get(key);
128 public void setProperties(Map<String, Object> properties) {
129 this.properties = properties;
132 public void setProperty(String key, String value) {
133 if (properties == null) {
134 properties = new HashMap<String,Object>();
137 properties.put(key, value);
140 public String getOperation() {
144 public void setOperation(String operation) {
145 this.operation = operation;
148 public String getLabel() {
152 public void setLabel(String label) {
156 public String getSource() {
160 public void setSource(String source) {
161 this.source = source;
164 public String getTarget() {
168 public void setTarget(String target) {
169 this.target = target;