69da437797b88ce7eab1d26548ca4f481c545381
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.openstack.utils;
23
24 import java.util.Set;
25 import org.openecomp.mso.logger.MsoLogger;
26
27 public class MsoHeatEnvironmentEntry {
28
29         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
30         private Set<MsoHeatEnvironmentParameter> parameters;
31         private String rawEntry;
32         private boolean valid = true;
33         private String errorString;
34         private String resourceRegistryEntryRaw;
35
36         private MsoHeatEnvironmentEntry(String rawEntry) {
37                 this.rawEntry = rawEntry;
38         }
39
40         private MsoHeatEnvironmentEntry(Set<MsoHeatEnvironmentParameter> parameters, String rawEntry, boolean valid,
41                         String errorString, String resourceRegistryEntryRaw) {
42                 this.parameters = parameters;
43                 this.rawEntry = rawEntry;
44                 this.valid = valid;
45                 this.errorString = errorString;
46                 this.resourceRegistryEntryRaw = resourceRegistryEntryRaw;
47         }
48
49         public boolean isValid() {
50                 return this.valid;
51         }
52         public String getErrorString() {
53                 return this.errorString;
54         }
55
56         public boolean containsParameter(String paramName) {
57                 if (this.parameters == null || this.parameters.size() < 1) {
58                         return false;
59                 }
60                 if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
61                         return true;
62                 }
63                 return false;
64         }
65
66         @Override
67         public String toString() {
68                 final StringBuilder sb = new StringBuilder("MsoHeatEnvironmentEntry{");
69                 sb.append("parameters=").append(parameters);
70                 sb.append(", resourceRegistryEntryRaw='").append(resourceRegistryEntryRaw).append('\'');
71                 sb.append('}');
72                 return sb.toString();
73         }
74
75         public String getRawEntry() {
76                 return rawEntry;
77         }
78         
79         private static String getResourceRegistryRawEntry(String rawEntry) {
80                 int indexOf = rawEntry.indexOf("resource_registry:");
81                 if (indexOf < 0) {
82                         return "";
83                 }
84                 return rawEntry.substring(indexOf);
85         }
86
87         public static MsoHeatEnvironmentEntry create(String rawEntry) {
88                 if (rawEntry == null || rawEntry.isEmpty()) {
89                         return new MsoHeatEnvironmentEntry(rawEntry);
90                 }
91                 try {
92                         Set<MsoHeatEnvironmentParameter> parameters = new MsoYamlEditorWithEnvt(rawEntry.getBytes())
93                                         .getParameterListFromEnvt();
94                         return new MsoHeatEnvironmentEntry(parameters, rawEntry, true, null,
95                                         getResourceRegistryRawEntry(rawEntry));
96                 } catch (Exception e) {
97                         LOGGER.debug(String.format("An exception occurred during processing the following raw entry: %s", rawEntry),
98                                         e);
99                         return new MsoHeatEnvironmentEntry(null, rawEntry, false, e.getMessage(), null);
100                 }
101         }
102
103 }