aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/org/onap/usecaseui/server/controller/lcm/ServiceInstanceController.java
blob: 47f96de0aab81d283a8d50ed46343c3549ddf55e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * Copyright 2016-2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.usecaseui.server.controller.lcm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.onap.usecaseui.server.service.lcm.PackageDistributionService;
import org.onap.usecaseui.server.service.lcm.ServiceInstanceService;
import org.onap.usecaseui.server.util.UuiCommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class ServiceInstanceController {

    private static final Logger logger = LoggerFactory.getLogger(ServiceInstanceController.class);

    @Resource(name="ServiceInstanceService")
    private ServiceInstanceService serviceInstanceService;
    
    @Resource(name="PackageDistributionService")
    private PackageDistributionService packageDistributionService;

    public void setPackageDistributionService(PackageDistributionService packageDistributionService) {
        this.packageDistributionService = packageDistributionService;
    }

    public void setServiceInstanceService(ServiceInstanceService serviceInstanceService) {
        this.serviceInstanceService = serviceInstanceService;
    }

	@ResponseBody
    @GetMapping(value = {"/uui-lcm/service-instances"}, produces = "application/json")
    public String listServiceInstances(HttpServletRequest request) throws JsonProcessingException{
		ObjectMapper mapper = new ObjectMapper();
        String customerId = request.getParameter("customerId");
        String serviceType = request.getParameter("serviceType");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        List<String> serviceInstances =serviceInstanceService.listServiceInstances(customerId, serviceType);
        Map<String,Object> map = new HashMap<>();
        map.put("total", serviceInstances.size());
        map.put("tableList", UuiCommonUtil.getPageList(serviceInstances, Integer.parseInt(currentPage), Integer.parseInt(pageSize)));
        return mapper.writeValueAsString(map);
    }
	
	@ResponseBody
    @GetMapping(value = {"/uui-lcm/service-ns-instances"}, produces = "application/json")
    public String listNsOrServiceInstances(HttpServletRequest request) throws JsonProcessingException{
		ObjectMapper mapper = new ObjectMapper();
        String customerId = request.getParameter("customerId");
        String serviceType = request.getParameter("serviceType");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        List<String> serviceInstances =serviceInstanceService.listServiceInstances(customerId, serviceType);
        List<String> nsServiceInstances = packageDistributionService.getNetworkServiceInfo();
        List<String> result = new ArrayList<>();
        result.addAll(serviceInstances);
        result.addAll(nsServiceInstances);
        Map<String,Object> map = new HashMap<>();
        map.put("total", result.size());
        map.put("tableList", UuiCommonUtil.getPageList(result, Integer.parseInt(currentPage), Integer.parseInt(pageSize)));
        return mapper.writeValueAsString(map);
    }
	
	@ResponseBody
    @GetMapping(value = {"/uui-lcm/serviceNumByCustomer"}, produces = "application/json")
    public String serviceNumByCustomer(HttpServletRequest request) throws JsonProcessingException{
		return serviceInstanceService.serviceNumByCustomer();
	}
	
	@ResponseBody
    @GetMapping(value = {"/uui-lcm/serviceNumByServiceType/{customerId}"}, produces = "application/json")
    public String serviceNumByServiceType(@PathVariable String customerId) throws JsonProcessingException{
		return serviceInstanceService.serviceNumByServiceType(customerId);
	}
	
    @ResponseBody
    @GetMapping(value = {"/uui-lcm/getServiceInstanceById"}, produces = "application/json")
    public String getServiceInstanceById(HttpServletRequest request){
        String customerId = request.getParameter("customerId");
        String serviceType = request.getParameter("serviceType");
        String serviceId = request.getParameter("serviceId");
        JSONArray result = new JSONArray();
        String servicesString=serviceInstanceService.getRelationShipData(customerId, serviceType, serviceId);
        if(!UuiCommonUtil.isNotNullOrEmpty(servicesString)){
        	return result.toString();
        }
        JSONObject services = JSONObject.parseObject(servicesString);
        JSONArray relations =JSONObject.parseArray(JSONObject.parseObject(services.getString("relationship-list")).getString("relationship"));
        for (int i = 0; i < relations.size(); i++) {
        	JSONObject relation = JSONObject.parseObject(relations.getString(i));
        	JSONArray relationShipData = JSONObject.parseArray(relation.getString("relationship-data"));
        	for (int j = 0; j < relationShipData.size(); j++) {
        		JSONObject res = new JSONObject();
        		JSONObject data = JSONObject.parseObject(relationShipData.getString(j));
        		String relationshipKey=data.getString("relationship-key");
        		String netWorkServiceId=data.getString("relationship-value");
        		if("service-instance.service-instance-id".equals(relationshipKey)){
        			JSONObject netWorkSerSring=JSONObject.parseObject(serviceInstanceService.getRelationShipData(customerId, serviceType, netWorkServiceId));
        			if("NetworkService".equals(netWorkSerSring.get("service-type"))){
        				res.put("netWorkServiceId", netWorkSerSring.get("service-instance-id"));
        				res.put("netWorkServiceName", netWorkSerSring.get("service-instance-name"));
        				res.put("scaleType","");
        				res.put("aspectId","");
        				res.put("numberOfSteps","");
        				res.put("scalingDirection","");
        				result.add(res);
        			}
        		}
        	}
		}
        return result.toString() ;
    }
}