Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / AppManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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 package org.oransc.ric.portal.dashboard.config;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.lang.invoke.MethodHandles;
28
29 import org.oransc.ric.plt.appmgr.client.api.HealthApi;
30 import org.oransc.ric.plt.appmgr.client.api.XappApi;
31 import org.oransc.ric.plt.appmgr.client.invoker.ApiClient;
32 import org.oransc.ric.plt.appmgr.client.model.AllDeployableXapps;
33 import org.oransc.ric.plt.appmgr.client.model.AllDeployedXapps;
34 import org.oransc.ric.plt.appmgr.client.model.AllXappConfig;
35 import org.oransc.ric.plt.appmgr.client.model.ConfigMetadata;
36 import org.oransc.ric.plt.appmgr.client.model.ConfigValidationError;
37 import org.oransc.ric.plt.appmgr.client.model.ConfigValidationErrors;
38 import org.oransc.ric.plt.appmgr.client.model.EventType;
39 import org.oransc.ric.plt.appmgr.client.model.SubscriptionRequest;
40 import org.oransc.ric.plt.appmgr.client.model.SubscriptionResponse;
41 import org.oransc.ric.plt.appmgr.client.model.XAppConfig;
42 import org.oransc.ric.plt.appmgr.client.model.Xapp;
43 import org.oransc.ric.plt.appmgr.client.model.Xapp.StatusEnum;
44 import org.oransc.ric.plt.appmgr.client.model.XappDescriptor;
45 import org.oransc.ric.plt.appmgr.client.model.XappInstance;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.context.annotation.Bean;
51 import org.springframework.context.annotation.Configuration;
52 import org.springframework.context.annotation.Profile;
53 import org.springframework.http.HttpStatus;
54
55 /**
56  * Creates an implementation of the xApp manager client that answers requests
57  * with mock data.
58  */
59 @Configuration
60 @Profile("test")
61 public class AppManagerMockConfiguration {
62
63         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64
65         // Simulate remote method delay for UI testing
66         private int delayMs;
67
68         @Autowired
69         public AppManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
70                 logger.debug("ctor: configured with delay {}", delayMs);
71                 this.delayMs = delayMs;
72         }
73
74         /**
75          * Builds a mock HealthApi object. Does not accept an instance key because this
76          * API answers no text.
77          * 
78          * @return mock HealthApi
79          */
80         private HealthApi healthApi() {
81                 ApiClient mockClient = mock(ApiClient.class);
82                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
83                 HealthApi mockApi = mock(HealthApi.class);
84                 when(mockApi.getApiClient()).thenReturn(mockClient);
85                 doAnswer(i -> null).when(mockApi).getHealthAlive();
86                 doAnswer(i -> null).when(mockApi).getHealthReady();
87                 return mockApi;
88         }
89
90         /**
91          * Builds a mock XappApi object.
92          * 
93          * @param instanceKey
94          *                        RIC instance
95          * @return Object that returns instance-specific results
96          */
97         private XappApi xappApi(String instanceKey) {
98                 logger.debug("Creating XappApi for instance {}", instanceKey);
99                 // Create instance-specific objects
100                 String[] appNames = { "AdmissionControl " + instanceKey, "UE Event Collector " + instanceKey };
101                 final String configJson = " { \"config\" : \"example-" + instanceKey + "\"}";
102                 final ConfigValidationErrors configValErrs = new ConfigValidationErrors();
103                 configValErrs.add(new ConfigValidationError().field("mock error"));
104                 final AllXappConfig allXappConfigs = new AllXappConfig();
105                 final AllDeployableXapps deployableApps = new AllDeployableXapps();
106                 final AllDeployedXapps deployedXapps = new AllDeployedXapps();
107                 for (String n : appNames) {
108                         ConfigMetadata metadata = new ConfigMetadata().xappName(n).namespace("namespace");
109                         XAppConfig config = new XAppConfig().config(configJson).metadata(metadata);
110                         allXappConfigs.add(config);
111                         deployableApps.add(n);
112                         Xapp xapp = new Xapp().name(n).version("version").status(StatusEnum.UNKNOWN);
113                         xapp.addInstancesItem(new XappInstance().name("abcd-1234").ip("127.0.0.1").port(200)
114                                         .status(XappInstance.StatusEnum.RUNNING));
115                         deployedXapps.add(xapp);
116                 }
117                 final SubscriptionResponse subRes = new SubscriptionResponse().eventType(EventType.ALL).id("subid").version(1);
118                 // Mock the methods to return the instance-specific objects
119                 ApiClient mockClient = mock(ApiClient.class);
120                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
121                 XappApi mockApi = mock(XappApi.class);
122                 when(mockApi.getApiClient()).thenReturn(mockClient);
123                 doAnswer(inv -> {
124                         if (delayMs > 0) {
125                                 logger.debug("getAllXappConfig sleeping {}", delayMs);
126                                 Thread.sleep(delayMs);
127                         }
128                         return allXappConfigs;
129                 }).when(mockApi).getAllXappConfig();
130                 doAnswer(inv -> {
131                         if (delayMs > 0) {
132                                 logger.debug("modifyXappConfig sleeping {}", delayMs);
133                                 Thread.sleep(delayMs);
134                         }
135                         return configValErrs;
136                 }).when(mockApi).modifyXappConfig(any(XAppConfig.class));
137                 doAnswer(inv -> {
138                         if (delayMs > 0) {
139                                 logger.debug("deployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
140                                 Thread.sleep(delayMs);
141                         }
142                         return deployedXapps.get(0);
143                 }).when(mockApi).deployXapp(any(XappDescriptor.class));
144                 doAnswer(inv -> {
145                         if (delayMs > 0) {
146                                 logger.debug("listAllDeployableXapps sleeping {}", delayMs);
147                                 Thread.sleep(delayMs);
148                         }
149                         return deployableApps;
150                 }).when(mockApi).listAllXapps();
151                 doAnswer(inv -> {
152                         if (delayMs > 0) {
153                                 logger.debug("getAllXapps sleeping {}", delayMs);
154                                 Thread.sleep(delayMs);
155                         }
156                         return deployedXapps;
157                 }).when(mockApi).getAllXapps();
158                 doAnswer(inv -> {
159                         if (delayMs > 0) {
160                                 logger.debug("getXappByName of {} sleeping {}", inv.getArgument(0), delayMs);
161                                 Thread.sleep(delayMs);
162                         }
163                         return deployedXapps.get(0);
164                 }).when(mockApi).getXappByName(any(String.class));
165                 doAnswer(inv -> {
166                         if (delayMs > 0) {
167                                 logger.debug("undeployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
168                                 Thread.sleep(delayMs);
169                         }
170                         return null;
171                 }).when(mockApi).undeployXapp(any(String.class));
172                 doAnswer(inv -> {
173                         if (delayMs > 0) {
174                                 logger.debug("addSubscription sleeping {}", delayMs);
175                                 Thread.sleep(delayMs);
176                         }
177                         return subRes;
178                 }).when(mockApi).addSubscription(any(SubscriptionRequest.class));
179                 doAnswer(inv -> {
180                         if (delayMs > 0) {
181                                 logger.debug("deleteSubscription sleeping {}", delayMs);
182                                 Thread.sleep(delayMs);
183                         }
184                         return null;
185                 }).when(mockApi).deleteSubscription(any(String.class));
186                 return mockApi;
187         }
188
189         @Bean
190         // Must use the same name as the non-mock configuration
191         public AppManagerApiBuilder appManagerApiBuilder() {
192                 final AppManagerApiBuilder mockBuilder = mock(AppManagerApiBuilder.class);
193                 final HealthApi mockHealthApi = healthApi();
194                 when(mockBuilder.getHealthApi(any(String.class))).thenReturn(mockHealthApi);
195                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
196                         final XappApi mockXappApi = xappApi(key);
197                         when(mockBuilder.getXappApi(key)).thenReturn(mockXappApi);
198                 }
199                 return mockBuilder;
200         }
201
202 }