5e59be526fd029e2b96b7353ae544df7bf0e6f4f
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / installer / ASDCElementInfo.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 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.mso.asdc.installer;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import org.openecomp.sdc.api.notification.IArtifactInfo;
30 import org.openecomp.sdc.api.notification.IResourceInstance;
31 import org.openecomp.sdc.api.notification.IVfModuleMetadata;
32 import org.openecomp.mso.asdc.client.ASDCConfiguration;
33
34 /**
35  * A class representing a generic element whose information can be used for example to log artifacts, resource... data.
36  */
37 public class ASDCElementInfo {
38         
39         /**
40          * A default, empty instance used in case a source element was not correctly provided.
41          */
42         public static final ASDCElementInfo EMPTY_INSTANCE = new ASDCElementInfo();
43         
44         /**
45          * Used to define the other possible ASDC Element types (usually in addition to existing artifact types, etc.).<br/>
46          * <br/>
47          * Possible types allowed:<br/>
48          * <ul>
49          * <li>{@link ASDCElementTypeEnum#VNF_RESOURCE}</li>
50          * <ul>
51          */
52         public static enum ASDCElementTypeEnum {
53                 /**
54                  * The type VNF_RESOURCE. Represents a VNF_RESOURCE element.
55                  */
56                 VNF_RESOURCE
57         };
58
59         /**
60          * The map of element information fields useful for logging. The complete contents of this list will be concatenated.
61          */
62         private final Map<String, String> elementInfoMap = new HashMap<>();
63         
64         /**
65          * The type of this element.
66          */
67         private final String type;
68         
69         private ASDCElementInfo () {
70                 // Private parameterless constructor. Not visible, only used for EMPTY_INSTANCE.
71                 this.type = "";
72         }
73         
74         /**
75          * Artifact-type based constructor. Requires a valid artifact type.
76          * @param artifactType The artifact type
77          */
78         private ASDCElementInfo (String artifactType) {
79                 // We need the exact type name here...
80                 this.type = artifactType;
81         }
82         
83         /**
84          * 'Other element type'-based constructor. Requires a valid element type.
85          * @param elementType An ASDCElementTypeEnum entry. This will usually contain enumerated types not in the existing
86          */
87         private ASDCElementInfo (ASDCElementTypeEnum elementType) {
88                 // We need the exact type name here...
89                 this.type = elementType.name();
90         }
91         
92         /**
93          * Add an information entry (name, UUID, etc.) from an artifact/resource/..., once a at time.
94          * 
95          * @param key The key (name) of the information entry (Artifact UUID, Resource Name, etc.)
96          * @param value The value bound to the information entry.
97          */
98         public final void addElementInfo(String key, String value) {
99                 if ((key != null) && (value != null)) {
100                         this.getElementInfoMap().put(key, value);
101                 }
102         }
103         
104         /**
105          * Returns an aggregated, formatted list of the expected details about an ASDC element.
106          * (non-Javadoc)
107          * @return An aggregated list of element information entries, comma-separated.
108          * @see java.lang.Object#toString()
109          */
110         @Override
111         public final String toString() {
112                 StringBuffer sb = new StringBuffer();
113                 List<String> aggregatedElements = new ArrayList<>();
114                 for (Entry<String, String> entry : this.getElementInfoMap().entrySet()) {
115                         aggregatedElements.add(entry.getKey() + ": " + entry.getValue());
116                 }
117                 sb.append(aggregatedElements.size() > 0 ? aggregatedElements.get(0) : "");
118                 if (aggregatedElements.size() > 1) {
119                         for (int i = 1; i < aggregatedElements.size(); ++i) {
120                                 sb.append (", ");
121                                 sb.append(aggregatedElements.get(i));
122                         }
123                 }
124                 return sb.toString();
125         }
126         
127         /**
128          * The type that was defined at creation time. This is typically VNF_RESOURCE, VF_MODULE_METADATA, HEAT_ENV, etc.
129          * @return The type of this element information type. This will usually be either an ArtifactTypeEnum entry name or an ASDCElementTypeEnum entry name.
130          * @see ASDCElementInfo.ASDCElementTypeEnum
131          */
132         public String getType() {
133                 return type;
134         }
135
136         /**
137          * Provides the map of all element information entries for this type.
138          * @return A map of all element information entries which will be used by the toString() method.
139          * @see ASDCElementInfo#toString()
140          */
141         protected Map<String, String> getElementInfoMap() {
142                 return elementInfoMap;
143         }
144
145         /**
146          * Create an ASDCElementInfo object from a VNF Resource.<br/>
147          * <br/>
148          * <b>Used information:</b><br/>
149          * <ul>
150          * <li>Resource Instance Name</li>
151          * <li>Resource Instance UUID</li>
152          * </ul>
153          * 
154          * @param vfResourceStructure The VfResourceStructure to use as source of information (see {@link VfResourceStructure}).
155          * @return an ASDCElementInfo using the information held in the VNF Resource.
156          */
157         public static final ASDCElementInfo createElementFromVfResourceStructure (VfResourceStructure vfResourceStructure) {
158                 if (vfResourceStructure == null) {
159                         return EMPTY_INSTANCE;
160                 }
161                 ASDCElementInfo elementInfo = new ASDCElementInfo(ASDCElementTypeEnum.VNF_RESOURCE);
162                 IResourceInstance resourceInstance = vfResourceStructure.getResourceInstance();
163                 elementInfo.addElementInfo("Resource Instance Name", resourceInstance.getResourceInstanceName());
164                 elementInfo.addElementInfo("Resource Instance Invariant UUID", resourceInstance.getResourceInvariantUUID());
165                 return elementInfo;
166         }
167
168         /**
169          * Create an ASDCElementInfo object from a VF Module.<br/>
170          * <br/>
171          * <b>Used information:</b><br/>
172          * <ul>
173          * <li>Module Model Name</li>
174          * <li>Module Model UUID</li>
175          * </ul>
176          * 
177          * @param vfModuleStructure The VfModuleStructure to use as source of information (see {@link VfModuleStructure}).
178          * @return an ASDCElementInfo using the information held in the VF Module.
179          */
180         public static final ASDCElementInfo createElementFromVfModuleStructure (VfModuleStructure vfModuleStructure) {
181                 if (vfModuleStructure == null) {
182                         return EMPTY_INSTANCE;
183                 }
184                 ASDCElementInfo elementInfo = new ASDCElementInfo(ASDCConfiguration.VF_MODULES_METADATA);
185                 IVfModuleMetadata moduleMetadata = vfModuleStructure.getVfModuleMetadata();
186                 elementInfo.addElementInfo("Module Model Name", moduleMetadata.getVfModuleModelName());
187                 elementInfo.addElementInfo("Module Model Invariant UUID", moduleMetadata.getVfModuleModelInvariantUUID());
188                 return elementInfo;
189         }
190         
191         /**
192          * Create an ASDCElementInfo object from an IArtfiactInfo instance.<br/>
193          * <br/>
194          * <b>Used information:</b><br/>
195          * <ul>
196          * <li>IArtifactInfo Name</li>
197          * <li>IArtifactInfo UUID</li>
198          * </ul>
199          * 
200          * @param artifactInfo The VfModuleStructure to use as source of information (see {@link IArtifactInfo}).
201          * @return an ASDCElementInfo using the information held in the IArtifactInfo instance.
202          */
203         public static final ASDCElementInfo createElementFromVfArtifactInfo (IArtifactInfo artifactInfo) {
204                 if (artifactInfo == null) {
205                         return EMPTY_INSTANCE;
206                 }
207                 ASDCElementInfo elementInfo = new ASDCElementInfo(artifactInfo.getArtifactType());
208                 elementInfo.addElementInfo(elementInfo.getType() + " Name", artifactInfo.getArtifactName());
209                 elementInfo.addElementInfo(elementInfo.getType() + " UUID", artifactInfo.getArtifactUUID());
210                 return elementInfo;
211         }
212 }