5577398692f62d6014959bb336e059c4b3dc2e14
[sdnc/apps.git] /
1 /*
2  * ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.networkdiscovery.service;
19
20 import java.text.MessageFormat;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.ws.rs.client.Client;
26 import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30 import javax.ws.rs.core.Response.Status.Family;
31
32 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
33 import org.onap.pomba.common.datatypes.DataQuality;
34 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
35 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
36 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryNotification;
37 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse;
38 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Resource;
39 import org.onap.sdnc.apps.pomba.networkdiscovery.service.util.TransformationUtil;
40 import org.slf4j.Logger;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
43
44 @Service
45 public class SpringServiceImpl implements SpringService {
46     private static final String OPENSTACK_HEADER_TOKEN = "X-Auth-Token";
47     private static final String OPENSTACK_HEADER_API_VERSION = "X-OpenStack-Nova-API-Version";
48
49     @Autowired
50     private Client openstackClient;
51
52     @Autowired
53     private String openstackIdentityUrl;
54
55     @Autowired
56     private String openstackIdentityUser;
57
58     @Autowired
59     private String openstackIdentityPassword;
60
61     @Autowired
62     private String openstackApiMicroversion;
63
64     @javax.annotation.Resource
65     private Map<String, String> openstackTypeURLs;
66
67     @Override
68     public NetworkDiscoveryResponse findbyResourceIdAndType(String transactionId, String requestId, String resourceType,
69             List<String> resourceIds, ONAPLogAdapter adapter) throws ApplicationException {
70
71         NetworkDiscoveryResponse response = new NetworkDiscoveryResponse();
72         response.setRequestId(requestId);
73
74         // check if resourceType is supported
75         String openstackURL = this.openstackTypeURLs.get(resourceType);
76         if (openstackURL == null) {
77             throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.BAD_REQUEST,
78                     "Unsupported resourceType " + resourceType);
79         }
80         String token = OSAuthentication.getToken(openstackIdentityUrl, openstackIdentityUser, openstackIdentityPassword,
81                 openstackClient, adapter);
82         
83         NetworkDiscoveryNotification discoveryResponse = new NetworkDiscoveryNotification();
84         discoveryResponse.setRequestId(requestId);
85         
86         List<Resource> resources = new ArrayList<>();
87         MessageFormat format = new MessageFormat(openstackURL);
88         
89         for (String resourceId : resourceIds) {
90             String url = format.format(new Object[] { resourceId });
91             Resource resource = new Resource();
92             resource.setType(resourceType);
93             resource.setId(resourceId);
94             resources.add(resource);
95         
96             Response result;
97             try {
98                 result = openstackClient.target(url).request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
99                         .header(OPENSTACK_HEADER_TOKEN, token)
100                         .header(OPENSTACK_HEADER_API_VERSION, openstackApiMicroversion).get();
101             } catch (Exception e) {
102                 // in case of time-out, exit the loop and return a failure.
103                 throw new ApplicationException(ApplicationException.Error.GENERAL_FAILURE, Status.NOT_FOUND,
104                         "Openstack API GET failed - " + e.getMessage());
105             }
106         
107             String jsonResult = result.readEntity(String.class);
108             Logger log = adapter.unwrap();
109             
110             log.info("Openstack GET result for resourceID {}: {}", resourceId, jsonResult);
111
112             if (result.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
113                 String transformedOutput = TransformationUtil.transform(jsonResult, resourceType);
114         
115                 log.debug("Jolt transformed output: {}", transformedOutput);
116         
117                 resource.setDataQuality(DataQuality.ok());
118                 List<Attribute> attributeList = TransformationUtil.toAttributeList(transformedOutput);
119                 resource.setAttributeList(attributeList);
120             } else {
121                 resource.setDataQuality(DataQuality.error(jsonResult));
122             }
123         }
124
125         discoveryResponse.setResources(resources);
126         discoveryResponse.setCode(Status.OK.getStatusCode());
127         discoveryResponse.setMessage(Status.OK.getReasonPhrase());
128         discoveryResponse.setAckFinalIndicator(true);
129
130         return discoveryResponse;
131     }
132
133 }