aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/controller/AaiServiceInstanceStandardQueryController.java
blob: 974ac9883be90d10cb472b97736188a25acfdcc6 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.onap.vid.controller;


import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Network;
import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.ServiceInstance;
import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vlan;
import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vnf;
import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.model.ServiceModel;
import org.onap.vid.model.VidNotions;
import org.onap.vid.properties.Features;
import org.onap.vid.services.VidService;
import org.onap.vid.utils.Multival;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.manager.FeatureManager;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;

import static java.util.stream.Collectors.toList;


@RestController
@RequestMapping(AaiServiceInstanceStandardQueryController.AAI_STANDARD_QUERY)
public class AaiServiceInstanceStandardQueryController extends VidRestrictedBaseController {

    public static final String AAI_STANDARD_QUERY = "aai/standardQuery";

    private final ServiceInstanceStandardQuery serviceInstanceStandardQuery;
    private final FeatureManager featureManager;
    private final VidService sdcService;

    @Autowired
    public AaiServiceInstanceStandardQueryController(FeatureManager featureManager, ServiceInstanceStandardQuery serviceInstanceStandardQuery, VidService sdcService) {
        this.featureManager = featureManager;
        this.serviceInstanceStandardQuery = serviceInstanceStandardQuery;
        this.sdcService = sdcService;
    }

    @RequestMapping(value = "vlansByNetworks", method = RequestMethod.GET)
    public VlansByNetworksHierarchy getNetworksToVlansByServiceInstance(HttpServletRequest request,
                                                                           @RequestParam("sdcModelUuid") UUID sdcModelUuid,
                                                                           @RequestParam("globalCustomerId") String globalCustomerId,
                                                                           @RequestParam("serviceType") String serviceType,
                                                                           @RequestParam("serviceInstanceId") String serviceInstanceId
    ) throws AsdcCatalogException {
        if (!featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)) {
            return new VlansByNetworksHierarchy();
        }

        if (!isModelOf5g(sdcModelUuid)) {
            return new VlansByNetworksHierarchy();
        }

        final ServiceInstance serviceInstance =
                serviceInstanceStandardQuery.fetchServiceInstance(globalCustomerId, serviceType, serviceInstanceId);

        Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> l3NetworksWithVlansForVnfForService =  fetchVnfsForService(serviceInstance);
        Multival<ServiceInstance, Multival<Network, Vlan>> l3NetworksWithVlansForService = fetchNetworksForService(serviceInstance);

        // translate to response's format
        return new VlansByNetworksHierarchy(
                l3NetworksWithVlansForService.getValues().stream().map(this::translateNetworksFormat
                    ).collect(toList()),

                l3NetworksWithVlansForVnfForService.getValues().stream().map(vnfWithNetworks ->
                        new VnfVlansByNetworks(vnfWithNetworks.getKey().getVnfId(),
                                vnfWithNetworks.getValues().stream().map(this::translateNetworksFormat
                                ).collect(toList())
                        )
                ).collect(toList())
        );
    }

    private Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> fetchVnfsForService(ServiceInstance serviceInstance) {
        final Multival<ServiceInstance, Vnf> vnfsForService =
                serviceInstanceStandardQuery.fetchRelatedVnfs(serviceInstance);

        final Multival<ServiceInstance, Multival<Vnf, Network>> vnfsWithL3NetworksForService =
                vnfsForService.mapEachVal(vnf -> serviceInstanceStandardQuery.fetchRelatedL3Networks("vnf", vnf));

        return  vnfsWithL3NetworksForService.mapEachVal(vnfMulti->
                        vnfMulti.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags)
                );

    }

    private Multival<ServiceInstance, Multival<Network, Vlan>> fetchNetworksForService(ServiceInstance serviceInstance) {
        final Multival<ServiceInstance, Network> l3NetworksForService =
                serviceInstanceStandardQuery.fetchRelatedL3Networks("service", serviceInstance);

        return l3NetworksForService.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags);
    }

    private NetworksToVlans translateNetworksFormat(Multival<Network, Vlan> networkWithVlan) {
        return new NetworksToVlans(
                networkWithVlan.getKey().getNetworkId(),
                networkWithVlan.getKey().getNetworkName(),
                networkWithVlan.getKey().getNetworkType(),
                networkWithVlan.getKey().getOrchestrationStatus(),
                networkWithVlan.getValues().stream().map(
                        vlan -> new NetworksToVlans.Vlan(vlan.getVlanIdInner())
                ).collect(toList())
        );
    }

    protected boolean isModelOf5g(UUID sdcModelUuid) throws AsdcCatalogException {
        final ServiceModel serviceModel = sdcService.getService(sdcModelUuid.toString());
        if (serviceModel == null) {
            throw new GenericUncheckedException("Internal error while fetching Service Model: " + sdcModelUuid);
        }
        VidNotions.ModelCategory serviceModelCategory = serviceModel.getService().getVidNotions().getModelCategory();
        return serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL) ||
                serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL);
    }

    protected static class VlansByNetworksHierarchy {
        public final Collection<NetworksToVlans> serviceNetworks;
        public final Collection<VnfVlansByNetworks> vnfNetworks;

        public VlansByNetworksHierarchy() {
            this(Collections.emptySet(), Collections.emptySet());
        }

        public VlansByNetworksHierarchy(Collection<NetworksToVlans> serviceNetworks, Collection<VnfVlansByNetworks> vnfNetworks) {
            this.serviceNetworks = serviceNetworks;
            this.vnfNetworks = vnfNetworks;
        }
    }

    protected static class VnfVlansByNetworks {
        public final String vnfId;
        public final Collection<NetworksToVlans> networks;

        public VnfVlansByNetworks(String vnfId, Collection<NetworksToVlans> networks) {
            this.vnfId = vnfId;
            this.networks = networks;
        }
    }

    protected static class NetworksToVlans {
        public final String networkId;
        public final String name;
        public final String nodeType;
        public final String nodeStatus;
        public final Collection<Vlan> vlans;

        private NetworksToVlans(String networkId, String name, String nodeType, String nodeStatus, Collection<Vlan> vlans) {
            this.networkId = networkId;
            this.name = name;
            this.nodeType = nodeType;
            this.nodeStatus = nodeStatus;
            this.vlans = vlans;
        }

        private static class Vlan {
            public final String vlanIdInner;

            private Vlan(String vlanIdInner) {
                this.vlanIdInner = vlanIdInner;
            }
        }

    }
}