Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / dao / impl / zusammen / ComponentDependencyModelDaoZusammenImpl.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.vendorsoftwareproduct.dao.impl.zusammen;
22
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.datatypes.item.Info;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToComponentDependencyModelConvertor;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
36 import org.openecomp.types.ElementPropertyName;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
44
45 /**
46  * Created by ayalaben on 5/16/2017.
47  */
48 public class ComponentDependencyModelDaoZusammenImpl implements ComponentDependencyModelDao {
49
50   private ZusammenAdaptor zusammenAdaptor;
51
52   public ComponentDependencyModelDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
53     this.zusammenAdaptor = zusammenAdaptor;
54   }
55
56   @Override
57   public ComponentDependencyModelEntity get(ComponentDependencyModelEntity dependency) {
58
59     SessionContext context = createSessionContext();
60     ElementContext elementContext =
61         new ElementContext(dependency.getVspId(), dependency.getVersion().getId());
62
63     Optional<ElementInfo> componentDependencyElement =
64         zusammenAdaptor.getElementInfo(context, elementContext, new Id(dependency.getId()));
65
66     if (componentDependencyElement.isPresent()) {
67       ElementToComponentDependencyModelConvertor convertor = new
68           ElementToComponentDependencyModelConvertor();
69
70       ComponentDependencyModelEntity entity = convertor.convert(componentDependencyElement.get());
71       entity.setVspId(dependency.getVspId());
72       entity.setVersion(dependency.getVersion());
73       return entity;
74     }
75
76     return null;
77   }
78
79   @Override
80   public void create(ComponentDependencyModelEntity dependency) {
81     ZusammenElement componentDependency =
82         buildComponentDependencyElement(dependency, Action.CREATE);
83
84     ZusammenElement componentDependencies =
85         buildStructuralElement(ElementType.ComponentDependencies, Action.IGNORE);
86     componentDependencies.addSubElement(componentDependency);
87
88     ZusammenElement vspModel = buildStructuralElement(ElementType.VspModel, Action.IGNORE);
89     vspModel.addSubElement(componentDependencies);
90
91     SessionContext context = createSessionContext();
92     ElementContext elementContext =
93         new ElementContext(dependency.getVspId(), dependency.getVersion().getId());
94
95     Element compDepsSavedElement = zusammenAdaptor
96         .saveElement(context, elementContext, vspModel, "Create component dependency model");
97
98     dependency.setId(compDepsSavedElement.getSubElements().iterator().next()
99         .getSubElements().iterator().next().getElementId().getValue());
100   }
101
102   @Override
103   public void update(ComponentDependencyModelEntity dependency) {
104     ZusammenElement componentDependencyElement =
105         buildComponentDependencyElement(dependency, Action.UPDATE);
106
107     SessionContext context = createSessionContext();
108     ElementContext elementContext =
109         new ElementContext(dependency.getVspId(), dependency.getVersion().getId());
110
111     zusammenAdaptor.saveElement(context, elementContext, componentDependencyElement,
112         String.format("Update component dependency model with id %s", dependency.getId()));
113   }
114
115   @Override
116   public void delete(ComponentDependencyModelEntity dependency) {
117     ZusammenElement componentDependencyElement =
118         buildElement(new Id(dependency.getId()), Action.DELETE);
119
120     SessionContext context = createSessionContext();
121     ElementContext elementContext =
122         new ElementContext(dependency.getVspId(), dependency.getVersion().getId());
123
124     zusammenAdaptor.saveElement(context, elementContext, componentDependencyElement,
125         String.format("Delete component dependency model with id %s", dependency.getId()));
126   }
127
128   @Override
129   public void registerVersioning(String versionableEntityType) {
130     //not implemented?
131   }
132
133   @Override
134   public Collection<ComponentDependencyModelEntity> list(
135       ComponentDependencyModelEntity dependency) {
136     SessionContext context = createSessionContext();
137     ElementContext elementContext =
138         new ElementContext(dependency.getVspId(), dependency.getVersion().getId());
139
140     Optional<ElementInfo> vspModel = zusammenAdaptor
141         .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
142     if (!vspModel.isPresent()) {
143       return new ArrayList<>();
144     }
145
146     ElementToComponentDependencyModelConvertor convertor =
147         new ElementToComponentDependencyModelConvertor();
148     return zusammenAdaptor.listElementsByName(context, elementContext, vspModel.get().getId(),
149         ElementType.ComponentDependencies.name()).stream()
150         .map(elementInfo -> {
151           ComponentDependencyModelEntity entity = convertor.convert(elementInfo);
152           entity.setVspId(dependency.getVspId());
153           entity.setVersion(dependency.getVersion());
154           entity.setId(elementInfo.getId().getValue());
155           return entity;
156         })
157         .collect(Collectors.toList());
158   }
159
160   private ZusammenElement buildComponentDependencyElement(ComponentDependencyModelEntity compDep,
161                                                           Action action) {
162     ZusammenElement componentDependencyElement =
163         buildElement(compDep.getId() == null ? null : new Id(compDep.getId()), action);
164
165     Info info = new Info();
166     info.addProperty(ElementPropertyName.elementType.name(), ElementType.ComponentDependency);
167     info.addProperty(ComponentDependencyModelPropertyName.RELATION.getVal(), compDep.getRelation());
168     info.addProperty(ComponentDependencyModelPropertyName.SOURCE_COMPONENT_ID.getVal(),
169         compDep.getSourceComponentId());
170     info.addProperty(ComponentDependencyModelPropertyName.TARGET_COMPONENT_ID.getVal(),
171         compDep.getTargetComponentId());
172
173     componentDependencyElement.setInfo(info);
174
175     return componentDependencyElement;
176   }
177
178   private enum ComponentDependencyModelPropertyName {
179     ID("id"),
180     RELATION("relation"),
181     SOURCE_COMPONENT_ID("sourcecomponent_id"),
182     TARGET_COMPONENT_ID("targetcomponent_id");
183
184     private String val;
185
186     ComponentDependencyModelPropertyName(String val){
187       this.val=val;
188     }
189
190     public String getVal() {
191       return val;
192     }
193   }
194 }