e2e7b1cc1e4236a20056f1c1c6e792c040467085
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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 package org.openecomp.sdc.versioning.dao.impl.zusammen;
21
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.item.Info;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import lombok.AllArgsConstructor;
30 import lombok.Getter;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.core.zusammen.api.ZusammenUtil;
33 import org.openecomp.sdc.versioning.dao.ItemDao;
34 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
35 import org.openecomp.sdc.versioning.types.Item;
36 import org.openecomp.sdc.versioning.types.ItemStatus;
37
38 public class ItemZusammenDaoImpl implements ItemDao {
39
40     private final ZusammenAdaptor zusammenAdaptor;
41
42     public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
43         this.zusammenAdaptor = zusammenAdaptor;
44     }
45
46     @Override
47     public Collection<Item> list() {
48         return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream().map(this::mapFromZusammenItem).collect(Collectors.toList());
49     }
50
51     @Override
52     public Item get(Item item) {
53         return mapFromZusammenItem(zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId())));
54     }
55
56     @Override
57     public Item create(Item item) {
58         Id itemId = zusammenAdaptor.createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item));
59         item.setId(itemId.getValue());
60         return item;
61     }
62
63     @Override
64     public void delete(Item item) {
65         zusammenAdaptor.deleteItem(ZusammenUtil.createSessionContext(), new Id(item.getId()));
66     }
67
68     @Override
69     public void update(Item item) {
70         zusammenAdaptor.updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()), mapToZusammenItemInfo(item));
71     }
72
73     private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) {
74         if (zusammenItem == null) {
75             return null;
76         }
77         Item item = new Item();
78         item.setId(zusammenItem.getId().getValue());
79         item.setName(zusammenItem.getInfo().getName());
80         item.setDescription(zusammenItem.getInfo().getDescription());
81         zusammenItem.getInfo().getProperties().forEach((key, value) -> addPropertyToItem(key, value, item));
82         item.setCreationTime(zusammenItem.getCreationTime());
83         item.setModificationTime(zusammenItem.getModificationTime());
84         if (item.getStatus() == null) {
85             item.setStatus(ItemStatus.ACTIVE);
86             update(item);
87         }
88         return item;
89     }
90
91     private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) {
92         final ItemInfoProperty itemInfoProperty = ItemInfoProperty.findByName(propertyKey).orElse(null);
93         if (itemInfoProperty == null) {
94             item.addProperty(propertyKey, propertyValue);
95             return;
96         }
97
98         switch (itemInfoProperty) {
99             case ITEM_TYPE:
100                 item.setType((String) propertyValue);
101                 break;
102             case ITEM_OWNER:
103                 item.setOwner((String) propertyValue);
104                 break;
105             case ITEM_STATUS:
106                 item.setStatus(ItemStatus.valueOf((String) propertyValue));
107                 break;
108             case ITEM_VERSIONS_STATUSES:
109                 for (Map.Entry<String, Number> statusCounter : ((Map<String, Number>) propertyValue).entrySet()) {
110                     item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()), statusCounter.getValue().intValue());
111                 }
112                 break;
113             default:
114                 item.addProperty(propertyKey, propertyValue);
115         }
116     }
117
118     private Info mapToZusammenItemInfo(Item item) {
119         Info info = new Info();
120         info.setName(item.getName());
121         info.setDescription(item.getDescription());
122         info.addProperty(ItemInfoProperty.ITEM_TYPE.getName(), item.getType());
123         info.addProperty(ItemInfoProperty.ITEM_OWNER.getName(), item.getOwner());
124         if (item.getStatus() != null) {
125             info.addProperty(ItemInfoProperty.ITEM_STATUS.getName(), item.getStatus());
126         }
127         info.addProperty(ItemInfoProperty.ITEM_VERSIONS_STATUSES.getName(), item.getVersionStatusCounters());
128         item.getProperties().forEach(info::addProperty);
129         return info;
130     }
131
132     @AllArgsConstructor
133     @Getter
134     public enum ItemInfoProperty {
135         ITEM_TYPE("item_type"),
136         ITEM_VERSIONS_STATUSES("item_versions_statuses"),
137         ITEM_OWNER("Owner"),
138         ITEM_STATUS("status");
139
140         private final String name;
141
142         public static Optional<ItemInfoProperty> findByName(final String name) {
143             return Arrays.stream(values()).filter(itemInfoProperty -> itemInfoProperty.getName().equals(name)).findFirst();
144         }
145
146     }
147 }