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