Merge "Change the copyright to 2018"
[so.git] / bpmn / MSOCommonBPMN / 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.File;
24 import java.io.IOException;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.logging.Logger;
30
31 import javax.net.ssl.SSLContext;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.WebTarget;
36 import javax.ws.rs.core.MediaType;
37
38 import org.onap.aai.domain.yang.GenericVnf;
39 import org.onap.aai.domain.yang.GenericVnfs;
40 import org.onap.aai.domain.yang.Pserver;
41 import org.onap.aai.domain.yang.Pservers;
42 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
43 import org.openecomp.mso.client.aai.entities.CustomQuery;
44 import org.openecomp.mso.client.aai.entities.Results;
45 import org.springframework.stereotype.Service;
46
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.core.type.TypeReference;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 @Service
53 public class AAIRestClientImpl implements AAIRestClient {
54
55         private final WebTarget webTarget;
56
57         private static final String ENDPOINT_VERSION = "v10";
58         private static final String ENDPOINT_GET_ALL = ENDPOINT_VERSION + "/cloud-infrastructure/pservers";
59         private static final String ENDPOINT_GET_ALL_VNFS = ENDPOINT_VERSION + "/network/generic-vnfs";
60         private static final String ENDPOINT_CUSTOM_QUERY = ENDPOINT_VERSION + "/query";
61         private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
62         private static final String GENERIC_VNF_PATH = ENDPOINT_VERSION + "/network/generic-vnfs/generic-vnf";
63         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("
64                         + ".out(‘has’).has(‘aai-node-type’,’vf-module’).store(‘x’),out(‘uses’).has(‘aai-node-type’,’volume-group’).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                         + ")," + ".out(‘runsOnVserver’).has(‘aai-node-type’,’vserver’).store(‘x’).union("
69                         + ".in(‘owns’).has(‘aai-node-type’,’tenant’).store(‘x’).in(‘has’).has(‘aai-node-type’,’cloud-region’).store(‘x’),"
70                         + ".out(‘runsOnPserver’).has(‘aai-node-type’,’pserver’).store(‘x’),"
71                         + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
72                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
73                         + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
74                         + ")" + ")" + ")" + ").cap(‘x’).unfold().dedup()";
75
76         public AAIRestClientImpl() throws NoSuchAlgorithmException {
77
78                 Logger logger = Logger.getLogger(getClass().getName());
79                 Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties");
80                 Client client = this.getSSLClient();
81                 webTarget = client.register(logger).register(new AAIClientResponseExceptionMapper())
82                                 .target(properties.get("aai.endpoint") + "/aai");
83         }
84
85         public AAIRestClientImpl(final String host) throws NoSuchAlgorithmException {
86                 Logger logger = Logger.getLogger(getClass().getName());
87                 Client client = this.getSSLClient();
88                 webTarget = client.register(logger).register(new AAIClientResponseExceptionMapper()).target(host + "/aai");
89         }
90
91         @Override
92         public Pservers getPhysicalServers(String hostName, String uuid) {
93                 return webTarget.register(AAIResourcesObjectMapperProvider.class).path(ENDPOINT_GET_ALL).request()
94                                 .header("X-FromAppId", "MSO").header("X-TransactionId", uuid)
95                                 .header("Content-Type", MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).get()
96                                 .readEntity(Pservers.class);
97         }
98
99         @Override
100         public List<Pserver> getPhysicalServerByVnfId(String vnfId, String transactionLoggingUuid)
101                         throws JsonParseException, JsonMappingException, IOException {
102                 List<String> startNodes = new ArrayList<>();
103                 startNodes.add("network/generic-vnfs/generic-vnf/" + vnfId);
104                 String jsonInput = webTarget.register(AAIQueryObjectMapperProvider.class).path(ENDPOINT_CUSTOM_QUERY)
105                                 .queryParam("format", "resource").request().header("X-FromAppId", "MSO")
106                                 .header("X-TransactionId", transactionLoggingUuid)
107                                 .header("Content-Type", MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE)
108                                 .put(Entity.entity(new CustomQuery(startNodes, PSERVER_VNF_QUERY), MediaType.APPLICATION_JSON))
109                                 .readEntity(String.class);
110
111                 
112                 return this.getListOfPservers(jsonInput);
113         }
114
115         protected List<Pserver> getListOfPservers(String jsonInput) throws JsonParseException, JsonMappingException, IOException
116         {
117                 ObjectMapper mapper = new AAIQueryObjectMapperProvider().getContext(Object.class);
118                 Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
119                                 new TypeReference<Results<Map<String, Pserver>>>() {
120                                 });
121                 List<Pserver> results = new ArrayList<>();
122                 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
123                         results.add(m.get("pserver"));
124                 }
125                 return results;
126         }
127         
128         protected List<Pserver> getListOfPservers(File jsonInput) throws JsonParseException, JsonMappingException, IOException
129         {
130                 ObjectMapper mapper = new AAIQueryObjectMapperProvider().getContext(Object.class);
131                 Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
132                                 new TypeReference<Results<Map<String, Pserver>>>() {
133                                 });
134                 List<Pserver> results = new ArrayList<>();
135                 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
136                         results.add(m.get("pserver"));
137                 }
138                 return results;
139         }
140         
141         @Override
142         public void updateMaintenceFlag(String vnfName, boolean inMaint, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
143                 GenericVnfs genericVnfs = webTarget.register(AAIResourcesObjectMapperProvider.class).path(ENDPOINT_GET_ALL_VNFS)
144                                 .queryParam("vnf-name", vnfName).request().header("X-FromAppId", "MSO")
145                                 .header("X-TransactionId", transactionLoggingUuid).header("Content-Type", "application/json")
146                                 .accept(MediaType.APPLICATION_JSON_TYPE).get().readEntity(GenericVnfs.class);
147
148                 if (genericVnfs.getGenericVnf().size() > 1)
149                         throw new IndexOutOfBoundsException ("Multiple Generic Vnfs Returned");
150
151                 GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
152                 updateMaintenceFlagVnfId(genericVnf.getVnfId(), inMaint, transactionLoggingUuid);
153         }
154
155         @Override
156         public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid)
157                         throws JsonParseException, JsonMappingException, IOException {
158                 GenericVnf genericVnf = new GenericVnf();
159                 genericVnf.setInMaint(inMaint);
160                 webTarget.register(AAIResourcesObjectMapperProvider.class).path(GENERIC_VNF_PATH + "/" + vnfId).request()
161                                 .header("X-FromAppId", "MSO").header("X-TransactionId", transactionLoggingUuid)
162                                 .header("Content-Type", "application/merge-patch+json")
163                                 .header("Accept", MediaType.APPLICATION_JSON_TYPE).header("X-HTTP-Method-Override", "PATCH")
164                                 .put(Entity.entity(genericVnf, MediaType.valueOf("application/merge-patch+json")));
165         }
166
167         @Override
168         public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
169                 return webTarget.register(AAIResourcesObjectMapperProvider.class).path(GENERIC_VNF_PATH + "/" + vnfId).request()
170                                 .header("X-FromAppId", "MSO").header("X-TransactionId", transactionLoggingUuid)
171                                 .header("Content-Type", "application/json").accept(MediaType.APPLICATION_JSON_TYPE).get()
172                                 .readEntity(GenericVnf.class);
173         }
174
175         protected Client getSSLClient() throws NoSuchAlgorithmException {
176                 return ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
177         }
178
179 }