Merge "Change the copyright to 2018"
[so.git] / common / src / main / java / org / openecomp / mso / client / aai / AAIRestClientImpl.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.openecomp.mso.client.aai;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.GenericVnfs;
31 import org.onap.aai.domain.yang.Pserver;
32 import org.onap.aai.domain.yang.Pservers;
33 import org.openecomp.mso.client.aai.entities.CustomQuery;
34 import org.openecomp.mso.client.aai.entities.Results;
35 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
36 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
37 import org.springframework.stereotype.Service;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46
47 @Service
48 public class AAIRestClientImpl implements AAIRestClientI {
49         
50         private static final EELFLogger logger = EELFManager.getInstance().getMetricsLogger();
51         private static final AAIVersion ENDPOINT_VERSION = AAIVersion.V10;
52         private static final String ENDPOINT_GET_ALL = ENDPOINT_VERSION + "/cloud-infrastructure/pservers";
53         private static final String ENDPOINT_GET_ALL_VNFS = ENDPOINT_VERSION + "/network/generic-vnfs";
54         private static final String ENDPOINT_CUSTOM_QUERY = ENDPOINT_VERSION + "/query";
55         private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
56         private static final String GENERIC_VNF_PATH = ENDPOINT_VERSION + "/network/generic-vnfs/generic-vnf";
57         private static final String SERVICE_TOPOLOGY_BY_SERVICE_INSTANCE_ID = "store(‘x’).union(__.in(‘subscribesTo’).has(‘aai-node-type’,’customer’).store(‘x’),__.out(‘uses’).has(‘aai-node-type’,’allotted-resource’).store(‘x’),__.in(‘hasInstance’).has(‘aai-node-type’,’generic-vnf’).store(‘x’).union("
58                         + ".out(‘has’).has(‘aai-node-type’,’vf-module’).store(‘x’),out(‘uses’).has(‘aai-node-type’,’volume-group’).store(‘x’),"
59                         + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
60                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
61                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
62                         + ")," + ".out(‘runsOnVserver’).has(‘aai-node-type’,’vserver’).store(‘x’).union("
63                         + ".in(‘owns’).has(‘aai-node-type’,’tenant’).store(‘x’).in(‘has’).has(‘aai-node-type’,’cloud-region’).store(‘x’),"
64                         + ".out(‘runsOnPserver’).has(‘aai-node-type’,’pserver’).store(‘x’),"
65                         + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
66                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
67                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
68                         + ")" + ")" + ")" + ").cap(‘x’).unfold().dedup()";
69
70         public AAIRestClientImpl() {            
71         
72                 
73         }
74
75         public AAIRestClientImpl(final String host) {   
76                 
77         
78         }
79
80         @Override
81         public Pservers getPhysicalServers(String hostName, String uuid) {
82                 UUID requestId;
83                 try {
84                         requestId = UUID.fromString(uuid);
85                 } catch (IllegalArgumentException e) {
86                         logger.warn("could not parse uuid: " + uuid + " creating valid uuid automatically");
87                         requestId = UUID.randomUUID();
88                 }
89                 return new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(Pservers.class, AAIUriFactory.createResourceUri(AAIObjectPlurals.PSERVER));
90         }
91
92         @Override
93         public List<Pserver> getPhysicalServerByVnfId(String vnfId, String transactionLoggingUuid)
94                         throws JsonParseException, JsonMappingException, IOException {
95                 UUID requestId;
96                 try {
97                         requestId = UUID.fromString(transactionLoggingUuid);
98                 } catch (IllegalArgumentException e) {
99                         logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
100                         requestId = UUID.randomUUID();
101                 }
102                 List<AAIResourceUri> startNodes = new ArrayList<>();
103                 startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
104                 String jsonInput = new AAIQueryClient(ENDPOINT_VERSION, requestId).query(Format.RESOURCE, new CustomQuery(startNodes,PSERVER_VNF_QUERY));
105
106                 return this.getListOfPservers(jsonInput);
107                 
108         }
109
110         protected List<Pserver> getListOfPservers(String jsonInput) throws JsonParseException, JsonMappingException, IOException {
111                 ObjectMapper mapper = new AAICommonObjectMapperProvider().getContext(Object.class);
112                 Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
113                                 new TypeReference<Results<Map<String, Pserver>>>() {
114                                 });
115                 List<Pserver> results = new ArrayList<>();
116                 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
117                         results.add(m.get("pserver"));
118                 }
119                 return results;
120         }
121         @Override
122         public void updateMaintenceFlag(String vnfName, boolean inMaint, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
123                 UUID requestId;
124                 try {
125                         requestId = UUID.fromString(transactionLoggingUuid);
126                 } catch (IllegalArgumentException e) {
127                         logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
128                         requestId = UUID.randomUUID();
129                 }
130                 GenericVnfs genericVnfs = new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(GenericVnfs.class, AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName));
131                 if(genericVnfs.getGenericVnf().size() > 1)
132                         throw new IndexOutOfBoundsException("Multiple Generic Vnfs Returned");
133                 
134                 GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
135                 updateMaintenceFlagVnfId(genericVnf.getVnfId(), inMaint, transactionLoggingUuid);
136         }
137
138         @Override
139         public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
140                 UUID requestId;
141                 try {
142                         requestId = UUID.fromString(transactionLoggingUuid);
143                 } catch (IllegalArgumentException e) {
144                         logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
145                         requestId = UUID.randomUUID();
146                 }
147                 GenericVnf genericVnf = new GenericVnf();
148                 genericVnf.setInMaint(inMaint);
149                 new AAIResourcesClient(ENDPOINT_VERSION, requestId).update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
150                 
151         }
152
153         @Override
154         public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
155                 UUID requestId;
156                 try {
157                         requestId = UUID.fromString(transactionLoggingUuid);
158                 } catch (IllegalArgumentException e) {
159                         logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
160                         requestId = UUID.randomUUID();
161                 }
162                 return new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
163         }
164
165 }