8c0ff14dec3cbadd5398ff68ee054d707474b112
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / XappManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.xappmgr.client.api.HealthApi;
30 import org.oransc.ric.xappmgr.client.api.XappApi;
31 import org.oransc.ric.xappmgr.client.invoker.ApiClient;
32 import org.oransc.ric.xappmgr.client.model.AllXappConfig;
33 import org.oransc.ric.xappmgr.client.model.AllXapps;
34 import org.oransc.ric.xappmgr.client.model.ConfigMetadata;
35 import org.oransc.ric.xappmgr.client.model.SubscriptionRequest;
36 import org.oransc.ric.xappmgr.client.model.SubscriptionResponse;
37 import org.oransc.ric.xappmgr.client.model.XAppConfig;
38 import org.oransc.ric.xappmgr.client.model.XAppInfo;
39 import org.oransc.ric.xappmgr.client.model.Xapp;
40 import org.oransc.ric.xappmgr.client.model.Xapp.StatusEnum;
41 import org.oransc.ric.xappmgr.client.model.XappInstance;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.context.annotation.Profile;
47 import org.springframework.http.HttpStatus;
48
49 /**
50  * Creates an implementation of the xApp manager client that answers requests
51  * with mock data.
52  */
53 @Profile("mock")
54 @Configuration
55 public class XappManagerMockConfiguration {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         private final AllXapps allXapps;
60         private final AllXappConfig allXappConfigs;
61
62         public XappManagerMockConfiguration() {
63                 logger.info("Configuring mock xApp Manager");
64                 final String[] appNames = { "AdmissionControl", "Automatic Neighbor Relation", "Dual Connectivity" };
65                 final String configJson = " { \"config\" : \"example\" }";
66                 final String descriptorJson = " { \"descriptor\" : \"example\" }";
67                 allXappConfigs = new AllXappConfig();
68                 allXapps = new AllXapps();
69                 for (String n : appNames) {
70                         ConfigMetadata metadata = new ConfigMetadata().configName("config-" + n).name(n).namespace("namespace");
71                         XAppConfig config = new XAppConfig().config(configJson).descriptor(descriptorJson).metadata(metadata);
72                         allXappConfigs.add(config);
73                         Xapp xapp = new Xapp().name(n).version("version").status(StatusEnum.UNKNOWN);
74                         xapp.addInstancesItem(new XappInstance().name("abcd-1234").ip("1.2.3.4").port(200)
75                                         .status(XappInstance.StatusEnum.RUNNING));
76                         allXapps.add(xapp);
77                 }
78         }
79
80         @Bean
81         // Use the same name as regular configuration
82         public HealthApi xappMgrHealthApi() {
83                 ApiClient mockClient = mock(ApiClient.class);
84                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
85                 HealthApi mockApi = mock(HealthApi.class);
86                 when(mockApi.getApiClient()).thenReturn(mockClient);
87                 doAnswer(i -> {
88                         return null;
89                 }).when(mockApi).getHealthAlive();
90                 doAnswer(i -> {
91                         return null;
92                 }).when(mockApi).getHealthReady();
93                 return mockApi;
94         }
95
96         @Bean
97         // Use the same name as regular configuration
98         public XappApi xappMgrXappApi() {
99                 ApiClient mockClient = mock(ApiClient.class);
100                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
101
102                 XappApi mockApi = mock(XappApi.class);
103                 when(mockApi.getApiClient()).thenReturn(mockClient);
104
105                 when(mockApi.getAllXappConfig()).thenReturn(allXappConfigs);
106
107                 when(mockApi.createXappConfig(any(XAppConfig.class))).thenReturn(new XAppConfig());
108
109                 when(mockApi.modifyXappConfig(any(XAppConfig.class))).thenReturn(new XAppConfig());
110
111                 doAnswer(i -> {
112                         return null;
113                 }).when(mockApi).deleteXappConfig(any(ConfigMetadata.class));
114
115                 when(mockApi.deployXapp(any(XAppInfo.class))).thenReturn(new Xapp());
116
117                 when(mockApi.getAllXapps()).thenReturn(allXapps);
118
119                 Xapp xappByName = new Xapp().name("name").status(StatusEnum.UNKNOWN).version("v1");
120                 when(mockApi.getXappByName(any(String.class))).thenReturn(xappByName);
121
122                 doAnswer(i -> {
123                         return null;
124                 }).when(mockApi).undeployXapp(any(String.class));
125
126                 SubscriptionResponse subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL)
127                                 .id("subid").version(1);
128                 when(mockApi.addSubscription(any(SubscriptionRequest.class))).thenReturn(subRes);
129
130                 doAnswer(i -> {
131                         return null;
132                 }).when(mockApi).deleteSubscription(any(String.class));
133
134                 return mockApi;
135         }
136
137 }