Merge "Rename the default recipe for VFC NS"
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / MsoHeatEnvironmentEntry.java
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                 return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
69                         ", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
70                         '}';
71         }
72
73         public String getRawEntry() {
74                 return rawEntry;
75         }
76         
77         private static String getResourceRegistryRawEntry(String rawEntry) {
78                 int indexOf = rawEntry.indexOf("resource_registry:");
79                 if (indexOf < 0) {
80                         return "";
81                 }
82                 return rawEntry.substring(indexOf);
83         }
84
85         public static MsoHeatEnvironmentEntry create(String rawEntry) {
86                 if (rawEntry == null || rawEntry.isEmpty()) {
87                         return new MsoHeatEnvironmentEntry(rawEntry);
88                 }
89                 try {
90                         Set<MsoHeatEnvironmentParameter> parameters = new MsoYamlEditorWithEnvt(rawEntry.getBytes())
91                                         .getParameterListFromEnvt();
92                         return new MsoHeatEnvironmentEntry(parameters, rawEntry, true, null,
93                                         getResourceRegistryRawEntry(rawEntry));
94                 } catch (Exception e) {
95                         LOGGER.debug(String.format("An exception occurred during processing the following raw entry: %s", rawEntry),
96                                         e);
97                         return new MsoHeatEnvironmentEntry(null, rawEntry, false, e.getMessage(), null);
98                 }
99         }
100
101 }