AT&T 1712 and 1802 release code
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / tenantisolation / helpers / AAIClientHelper.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.apihandlerinfra.tenantisolation.helpers;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Map;
26
27 import org.openecomp.mso.apihandlerinfra.tenantisolation.exceptions.AAIClientCallFailed;
28 import org.openecomp.mso.client.aai.AAIObjectType;
29 import org.openecomp.mso.client.aai.AAIResourcesClient;
30 import org.openecomp.mso.client.aai.entities.AAIResultWrapper;
31 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
32 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
33 import org.openecomp.mso.client.aai.entities.uri.Depth;
34 import org.openecomp.mso.client.aai.objects.AAIOperationalEnvironment;
35 import org.openecomp.mso.logger.MsoLogger;
36
37 public class AAIClientHelper {
38         
39     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
40     
41     public AAIClientHelper() {
42                 super();
43         }
44     
45     public AAIClientHelper(String serviceName, String requestId) {
46                 super();
47                 MsoLogger.setServiceName (serviceName);
48                 MsoLogger.setLogContext(requestId, "");
49         }
50
51         /**
52          * Get managing ECOMP Environment Info from A&AI
53          * @param id = operationalEnvironmentId 
54          * @return AAIResultWrapper object
55          */
56         public AAIResultWrapper getAaiOperationalEnvironment(String id) throws Exception {
57                 try {
58                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, id);
59                         uri.depth(Depth.ZERO); //Do not return relationships if any
60                         AAIResourcesClient aaiClient = this.getClient();
61                         AAIResultWrapper result = aaiClient.get(uri);
62                         return result;
63                 }
64                 catch(Exception ex) {
65                         logStackTrace(ex);
66                         throw new AAIClientCallFailed("Call to A&AI failed!", ex);
67                 } 
68         }
69         
70
71         /**
72          * Update managing ECOMP Environment Info from A&AI
73          * @param id = operationalEnvironmentId
74          * @param AAIOperationalEnvironment object
75          */
76         public void updateAaiOperationalEnvironment(String id, AAIOperationalEnvironment aaiRequest) throws Exception {
77                 try {
78                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, id);
79                         AAIResourcesClient aaiClient = this.getClient();
80                         aaiClient.update(uri, aaiRequest);
81                 }
82                 catch(Exception ex) {
83                         logStackTrace(ex);
84                         throw new AAIClientCallFailed("Call to A&AI failed!", ex);
85                 } 
86         }
87         
88
89         public void updateAaiOperationalEnvironment(String operationalEnvironmentId, Map<String, String> payload) throws Exception {
90                 try {
91                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, operationalEnvironmentId);
92                         AAIResourcesClient aaiClient = this.getClient();
93                         aaiClient.update(uri, payload);
94                 }
95                 catch(Exception ex) {
96                         logStackTrace(ex);
97                         throw new AAIClientCallFailed("Call to A&AI failed!", ex);
98                 } 
99         }
100         
101         /**
102          * Create an Operational Environment object in A&AI
103          * @param AAIOperationalEnvironment object
104          */
105         public void createOperationalEnvironment(AAIOperationalEnvironment operationalEnvironment) throws Exception {
106                 try {
107                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, operationalEnvironment.getOperationalEnvironmentId());
108                         AAIResourcesClient aaiClient = this.getClient();
109                         aaiClient.create(uri, operationalEnvironment);
110                 }
111                 catch(Exception ex) {
112                         logStackTrace(ex);
113                         throw new AAIClientCallFailed("Call to A&AI failed!", ex);
114                 } 
115         }
116         
117         /**
118          * Create a relationship between ECOMP managing and VNF Operational Environments
119          * @param managingEcompOperationalEnvironmentId
120          * @param vnfOperationalEnvironmentId
121          * @throws Exception
122          */
123         public void createRelationship(String managingEcompOperationalEnvironmentId, String vnfOperationalEnvironmentId) throws Exception {
124                 try {
125                         AAIResourceUri ecompEnvUri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, managingEcompOperationalEnvironmentId);
126                         AAIResourceUri vnfEnvUri = AAIUriFactory.createResourceUri(AAIObjectType.OPERATIONAL_ENVIRONMENT, vnfOperationalEnvironmentId);
127                         AAIResourcesClient aaiClient = this.getClient();
128                         aaiClient.connect(vnfEnvUri, ecompEnvUri);
129                 }
130                 catch(Exception ex) {
131                         logStackTrace(ex);
132                         throw new AAIClientCallFailed("Call to A&AI failed!", ex);
133                 } 
134         }
135         
136         private void logStackTrace(Exception e) {
137                 StringWriter sw = new StringWriter();
138                 e.printStackTrace(new PrintWriter(sw));
139                 msoLogger.debug(sw.toString());
140         }
141         
142         protected AAIResourcesClient getClient() {
143                 return new AAIResourcesClient();
144         }
145 }