Add or Delete a PNF to an Active Service
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / orchestration / NamingServiceResources.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Modifications Copyright (c) 2020 Nokia
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.so.client.orchestration;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import org.onap.namingservice.model.Element;
32 import org.onap.namingservice.model.Deleteelement;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
34 import org.onap.so.client.exception.BadResponseException;
35 import org.onap.so.client.namingservice.NamingClient;
36 import org.onap.so.client.namingservice.NamingRequestObject;
37 import org.onap.so.client.namingservice.NamingRequestObjectBuilder;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class NamingServiceResources {
43     private static final String NAMING_TYPE = "instanceGroup";
44
45     @Autowired
46     private NamingClient namingClient;
47
48     @Autowired
49     private NamingRequestObjectBuilder namingRequestObjectBuilder;
50
51     public String generateInstanceGroupName(InstanceGroup instanceGroup, String policyInstanceName, String nfNamingCode)
52             throws BadResponseException, IOException {
53         Element element = namingRequestObjectBuilder.elementMapper(instanceGroup.getId(), policyInstanceName,
54                 NAMING_TYPE, nfNamingCode, instanceGroup.getInstanceGroupName());
55         List<Element> elements = new ArrayList<>();
56         elements.add(element);
57         return (namingClient.postNameGenRequest(namingRequestObjectBuilder.nameGenRequestMapper(elements)));
58     }
59
60     public String deleteInstanceGroupName(InstanceGroup instanceGroup) throws BadResponseException, IOException {
61         Deleteelement deleteElement = namingRequestObjectBuilder.deleteElementMapper(instanceGroup.getId());
62         List<Deleteelement> deleteElements = new ArrayList<>();
63         deleteElements.add(deleteElement);
64         return (namingClient
65                 .deleteNameGenRequest(namingRequestObjectBuilder.nameGenDeleteRequestMapper(deleteElements)));
66     }
67
68     public String generateServiceInstanceName(NamingRequestObject namingRequestObject)
69             throws BadResponseException, IOException {
70         HashMap<String, String> nsRequestObject = namingRequestObject.getNamingRequestObjectMap();
71         Element element = new Element();
72         nsRequestObject.forEach(element::put);
73         List<Element> elements = new ArrayList<>();
74         elements.add(element);
75         return (namingClient.postNameGenRequest(namingRequestObjectBuilder.nameGenRequestMapper(elements)));
76     }
77
78     public String deleteServiceInstanceName(NamingRequestObject namingRequestObject)
79             throws BadResponseException, IOException {
80         HashMap<String, String> nsRequestObject = namingRequestObject.getNamingRequestObjectMap();
81         Deleteelement delElement = new Deleteelement();
82         nsRequestObject.forEach((k, v) -> delElement.setExternalKey(v));
83         List<Deleteelement> delElements = new ArrayList<>();
84         delElements.add(delElement);
85         return (namingClient.deleteNameGenRequest(namingRequestObjectBuilder.nameGenDeleteRequestMapper(delElements)));
86     }
87
88 }