Code changes in SO api-handler for RAN Slice
[so.git] / graph-inventory / aai-client / src / main / java / org / onap / aaiclient / 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.onap.aaiclient.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.Optional;
28 import javax.ws.rs.core.Response;
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.Pnf;
31 import org.onap.aai.domain.yang.Pserver;
32 import org.onap.aai.domain.yang.ServiceInstance;
33 import org.onap.aaiclient.client.aai.entities.CustomQuery;
34 import org.onap.aaiclient.client.aai.entities.Results;
35 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
36 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
37 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
38 import org.onap.aaiclient.client.graphinventory.Format;
39 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public class AAIRestClientImpl implements AAIRestClientI {
46
47     private final Logger log = LoggerFactory.getLogger(getClass());
48     private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
49
50     @Override
51     public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
52         List<AAIResourceUri> startNodes = new ArrayList<>();
53         startNodes.add(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)));
54         String jsonInput = new AAIQueryClient().query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
55
56         return this.getListOfPservers(jsonInput);
57
58     }
59
60     protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
61         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
62         Results<Map<String, Pserver>> resultsFromJson =
63                 mapper.readValue(jsonInput, new TypeReference<Results<Map<String, Pserver>>>() {});
64         List<Pserver> results = new ArrayList<>();
65         for (Map<String, Pserver> m : resultsFromJson.getResult()) {
66             results.add(m.get("pserver"));
67         }
68         return results;
69     }
70
71     @Override
72     public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint) {
73         GenericVnf genericVnf = new GenericVnf();
74         genericVnf.setInMaint(inMaint);
75         new AAIResourcesClient()
76                 .update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)), genericVnf);
77
78     }
79
80     @Override
81     public GenericVnf getVnfByName(String vnfId) {
82         return new AAIResourcesClient().get(GenericVnf.class,
83                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))).orElse(null);
84     }
85
86     @Override
87     public Optional<Pnf> getPnfByName(String pnfId) {
88         Response response = new AAIResourcesClient()
89                 .getFullResponse(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)));
90         if (response.getStatus() != 200) {
91             return Optional.empty();
92         } else {
93             return Optional.of(response.readEntity(Pnf.class));
94         }
95     }
96
97     @Override
98     public void createPnf(String pnfId, Pnf pnf) {
99         new AAIResourcesClient().createIfNotExists(
100                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)), Optional.of(pnf));
101     }
102
103     @Override
104     public void updatePnf(String pnfId, Pnf pnf) {
105         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)),
106                 pnf);
107     }
108
109     @Override
110     public Optional<ServiceInstance> getServiceInstanceById(String serviceInstanceId, String serviceType,
111             String globalSubscriberId) {
112         Response response = new AAIResourcesClient().getFullResponse(
113                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId)
114                         .serviceSubscription(serviceType).serviceInstance(serviceInstanceId)));
115         return Optional.ofNullable(response.readEntity(ServiceInstance.class));
116     }
117
118     public Optional<ServiceInstance> getServiceInstanceByIdWithDepth(String serviceInstanceId, String serviceType,
119             String globalSubscriberId) {
120         Response response =
121                 new AAIResourcesClient()
122                         .getFullResponse(AAIUriFactory
123                                 .createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId)
124                                         .serviceSubscription(serviceType).serviceInstance(serviceInstanceId))
125                                 .depth(Depth.ALL));
126         return Optional.ofNullable(response.readEntity(ServiceInstance.class));
127     }
128
129     @Override
130     public void updateServiceInstance(String serviceInstanceId, String serviceType, String globalSubscriberId,
131             ServiceInstance serviceInstance) {
132         try {
133             new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
134                     .customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId)),
135                     serviceInstance);
136         } catch (Throwable ex) {
137             log.error("Exception happened while updating ServiceInstance, Exception: {}", ex.getLocalizedMessage());
138             throw new RuntimeException(ex);
139         }
140     }
141 }