re base code
[sdc.git] / openecomp-be / lib / openecomp-conflict-lib / openecomp-conflict-core / src / main / java / org / openecomp / conflicts / ItemMergeHandlerFactoryImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.conflicts;
18
19 import org.openecomp.conflicts.dao.ConflictsDaoFactory;
20 import org.openecomp.conflicts.impl.VspMergeHandler;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.datatypes.model.ItemType;
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDaoFactory;
24 import org.openecomp.sdc.versioning.AsdcItemManagerFactory;
25 import org.openecomp.sdc.versioning.errors.EntityNotExistErrorBuilder;
26 import org.openecomp.sdc.versioning.types.Item;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Optional;
31
32 public class ItemMergeHandlerFactoryImpl extends ItemMergeHandlerFactory {
33   // TODO: 11/1/2017 read this map from configuration, move Vsp merge handler to vsp lib, rearrange lib deps
34   private static final Map<ItemType, ItemMergeHandler> MERGE_HANDLER_BY_ITEM_TYPE =
35       new HashMap<>();
36
37   static {
38     MERGE_HANDLER_BY_ITEM_TYPE.put(ItemType.vsp,
39         new VspMergeHandler(ConflictsDaoFactory.getInstance().createInterface(),
40             VspMergeDaoFactory.getInstance().createInterface()));
41   }
42
43   @Override
44   public Optional<ItemMergeHandler> createInterface(String itemId) {
45     Item item = AsdcItemManagerFactory.getInstance().createInterface().get(itemId);
46     if (item == null) {
47       throw new CoreException(new EntityNotExistErrorBuilder("", itemId).build());
48     }
49     return Optional.ofNullable(MERGE_HANDLER_BY_ITEM_TYPE.get(ItemType.valueOf(item.getType())));
50   }
51
52   @Override
53   public ItemMergeHandler createInterface() {
54     return null; // call the one with the item id arg
55   }
56 }