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