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