Added oparent to sdc main
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / utils / IdMapper.java
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
21 package org.openecomp.sdc.be.model.jsonjanusgraph.utils;
22
23 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
24 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
26 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
27 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
28 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import org.springframework.stereotype.Component;
30
31 import java.util.Map;
32 import java.util.Optional;
33
34 /**
35  * Created by yavivi on 12/02/2018.
36  */
37 @Component
38 public class IdMapper {
39
40     private static final Logger log = Logger.getLogger(ExternalReferencesOperation.class.getName());
41
42     public String mapComponentNameToUniqueId(String componentInstanceName, GraphVertex serviceVertex) {
43         return map(componentInstanceName, serviceVertex, true);
44     }
45
46     public String mapUniqueIdToComponentNameTo(String compUniqueId, GraphVertex serviceVertex) {
47         return map(compUniqueId, serviceVertex, false);
48     }
49
50     private String map(String componentUniqueIdOrName, GraphVertex serviceVertex, boolean fromCompName) {
51         String result = null;
52         try {
53             Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) serviceVertex.getJson();
54             CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
55
56             Optional<ComponentInstanceDataDefinition> componentInstanceDataDefinitionOptional = null;
57             if (fromCompName) {
58                 componentInstanceDataDefinitionOptional = compositionDataDefinition.getComponentInstances().values().stream().filter(c -> c.getNormalizedName().equals(componentUniqueIdOrName)).findAny();
59                 result = componentInstanceDataDefinitionOptional.get().getUniqueId();
60                 log.debug("Compponent Instance Unique Id = {}", result);
61             } else {
62                 componentInstanceDataDefinitionOptional = compositionDataDefinition.getComponentInstances().values().stream().filter(c -> c.getUniqueId().equals(componentUniqueIdOrName)).findAny();
63                 result = componentInstanceDataDefinitionOptional.get().getNormalizedName();
64                 log.debug("Compponent Instance Normalized Name = {}", result);
65             }
66
67         } catch (Exception e) {
68             log.error("Failed to map UUID or Normalized name of " + componentUniqueIdOrName, e);
69         }
70         return result;
71     }
72
73 }