aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java
blob: 04dbf523d08f71c602227cf1b15e360c81417e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.openstack.utils;

import java.util.Set;
import org.openecomp.mso.logger.MsoLogger;

public class MsoHeatEnvironmentEntry {

	private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
	private Set<MsoHeatEnvironmentParameter> parameters;
	private String rawEntry;
	private boolean valid = true;
	private String errorString;
	private String resourceRegistryEntryRaw;

	private MsoHeatEnvironmentEntry(String rawEntry) {
		this.rawEntry = rawEntry;
	}

	private MsoHeatEnvironmentEntry(Set<MsoHeatEnvironmentParameter> parameters, String rawEntry, boolean valid,
			String errorString, String resourceRegistryEntryRaw) {
		this.parameters = parameters;
		this.rawEntry = rawEntry;
		this.valid = valid;
		this.errorString = errorString;
		this.resourceRegistryEntryRaw = resourceRegistryEntryRaw;
	}

	public boolean isValid() {
		return this.valid;
	}
	public String getErrorString() {
		return this.errorString;
	}

	public boolean containsParameter(String paramName) {
		if (this.parameters == null || this.parameters.size() < 1) {
			return false;
		}
		if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
			return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return "MsoHeatEnvironmentEntry{" + "parameters=" + parameters +
			", resourceRegistryEntryRaw='" + resourceRegistryEntryRaw + '\'' +
			'}';
	}

	public String getRawEntry() {
		return rawEntry;
	}
	
	private static String getResourceRegistryRawEntry(String rawEntry) {
		int indexOf = rawEntry.indexOf("resource_registry:");
		if (indexOf < 0) {
			return "";
		}
		return rawEntry.substring(indexOf);
	}

	public static MsoHeatEnvironmentEntry create(String rawEntry) {
		if (rawEntry == null || rawEntry.isEmpty()) {
			return new MsoHeatEnvironmentEntry(rawEntry);
		}
		try {
			Set<MsoHeatEnvironmentParameter> parameters = new MsoYamlEditorWithEnvt(rawEntry.getBytes())
					.getParameterListFromEnvt();
			return new MsoHeatEnvironmentEntry(parameters, rawEntry, true, null,
					getResourceRegistryRawEntry(rawEntry));
		} catch (Exception e) {
			LOGGER.debug(String.format("An exception occurred during processing the following raw entry: %s", rawEntry),
					e);
			return new MsoHeatEnvironmentEntry(null, rawEntry, false, e.getMessage(), null);
		}
	}

}