aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloud/CloudConfig.java
blob: ef5f8232e0417e00e9986c8ecc46ef71a52b6e35 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. 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.onap.so.cloud;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import javax.annotation.PostConstruct;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;

/**
 * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
 * configuration file to Java. The CloudConfig contains information about
 * Openstack cloud configurations. It includes: 
 * - CloudIdentity objects,representing DCP nodes (Openstack Identity Service) 
 * - CloudSite objects, representing LCP nodes (Openstack Compute & other services)
 *
 * Note that this is only used to access Cloud Configurations loaded from a JSON
 * config file, so there are no explicit property setters.
 *
 * This class also contains methods to query cloud sites and/or identity
 * services by ID.
 *
 */

@Configuration
@JsonRootName("cloud_config")
@ConfigurationProperties(prefix="cloud_config")
public class CloudConfig {
	
    private static final String CLOUD_SITE_VERSION = "2.5";
    private static final String DEFAULT_CLOUD_SITE_ID = "default";
    
    @JsonProperty("identity_services")
    private Map<String, CloudIdentity> identityServices = new HashMap<>();
    
    @JsonProperty("cloud_sites")
    private Map <String, CloudSite> cloudSites = new HashMap<>();
    
    @JsonProperty("cloudify_managers")
    private Map <String, CloudifyManager> cloudifyManagers = new HashMap<>();

    @PostConstruct
    private void init() {
    	for (Entry<String, CloudIdentity> entry : identityServices.entrySet()) {
    		entry.getValue().setId(entry.getKey());
    	}
    	
    	for (Entry<String, CloudSite> entry : cloudSites.entrySet()) {
    		entry.getValue().setId(entry.getKey());
    	}
    	
    	for (Entry<String, CloudifyManager> entry : cloudifyManagers.entrySet()) {
    		entry.getValue().setId(entry.getKey());
    	}
    }
    
    /**
     * Get a map of all identity services that have been loaded.
     */
    public Map<String, CloudIdentity> getIdentityServices() {
        return identityServices;
    }

    /**
     * Get a map of all cloud sites that have been loaded.
     */
    public Map<String, CloudSite> getCloudSites() {
        return cloudSites;
    }

	/**
	 * Get a Map of all CloudifyManagers that have been loaded.
	 * @return the Map
	 */
    public Map<String,CloudifyManager> getCloudifyManagers() {
         return cloudifyManagers;
    }
    
    /**
     * Get a specific CloudSites, based on an ID. The ID is first checked
     * against the regions, and if no match is found there, then against
     * individual entries to try and find one with a CLLI that matches the ID
     * and an AIC version of 2.5.
     * 
     * @param id the ID to match
     * @return an Optional of CloudSite object.
     */
     public synchronized Optional<CloudSite> getCloudSite(String id) {
        if (id == null) {
            return Optional.empty();
        }
        if (cloudSites.containsKey(id)) {
            return Optional.ofNullable(cloudSites.get(id));
        } else {
        	return getCloudSiteWithClli(id);
        }
    }
    
    public String getCloudSiteId(CloudSite cloudSite) {
       for(Entry<String, CloudSite> entry : this.getCloudSites().entrySet()){
    	   if(entry.getValue().equals(cloudSite))
    		   return entry.getKey();
       }
       return null;
    }

    /**
     * Get a specific CloudSites, based on a CLLI and (optional) version, which
     * will be matched against the aic_version field of the CloudSite.
     * 
     * @param clli
     *            the CLLI to match
     * @param version
     *            the version to match; may be null in which case any version
     *            matches
     * @return a CloudSite, or null of no match found
     */
    private Optional<CloudSite> getCloudSiteWithClli(String clli) {
        Optional <CloudSite> cloudSiteOptional = cloudSites.values().stream().filter(cs ->
                cs.getClli() != null && clli.equals(cs.getClli()) && (CLOUD_SITE_VERSION.equals(cs.getAicVersion())))
                .findAny();
        if (cloudSiteOptional.isPresent()) {
        	return cloudSiteOptional;
        } else {
        	return getDefaultCloudSite(clli);
        }
    }

    private Optional<CloudSite> getDefaultCloudSite(String clli) {
        Optional<CloudSite> cloudSiteOpt = cloudSites.values().stream()
                .filter(cs -> cs.getId().equalsIgnoreCase(DEFAULT_CLOUD_SITE_ID)).findAny();
        if (cloudSiteOpt.isPresent()) {
            CloudSite defaultCloudSite = cloudSiteOpt.get();
            CloudSite clone = new CloudSite(defaultCloudSite);
            clone.setRegionId(clli);
            clone.setId(clli);
            return Optional.of(clone);
        } else {
            return Optional.empty();
        }
    }

    /**
     * Get a specific CloudIdentity, based on an ID.
     * 
     * @param id
     *            the ID to match
     * @return a CloudIdentity, or null of no match found
     */
    public CloudIdentity getIdentityService(String id) {
    		return identityServices.get(id);
    }

	/**
	 * Get a specific CloudifyManager, based on an ID.
	 * @param id the ID to match
	 * @return a CloudifyManager, or null of no match found
	 */
	public CloudifyManager getCloudifyManager (String id) {
			return cloudifyManagers.get(id);
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("identityServices", getIdentityServices()).append("cloudSites", getCloudSites()).toString();
	}
	
	@Override
	public boolean equals(final Object other) {
		if (other == null) {
			return false;
		}
		if (!getClass().equals(other.getClass())) {
			return false;
		}
		CloudConfig castOther = (CloudConfig) other;
		return new EqualsBuilder().append(getIdentityServices(), castOther.getIdentityServices())
				.append(getCloudSites(), castOther.getCloudSites()).isEquals();
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder(1, 31).append(getIdentityServices()).append(getCloudSites()).toHashCode();
	}
}