Containerization feature of SO
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / openstack / mappers / NetworkInfoMapper.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.onap.so.openstack.mappers;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import org.javatuples.Pair;
30 import org.onap.so.openstack.beans.NetworkInfo;
31 import org.onap.so.openstack.beans.NetworkStatus;
32
33 import com.woorea.openstack.quantum.model.Network;
34 import com.woorea.openstack.quantum.model.Segment;
35
36 public class NetworkInfoMapper {
37
38         private final Network network;
39         private final Map<String,NetworkStatus> networkStatusMap = new HashMap<>();
40
41         public NetworkInfoMapper(Network network) {
42                 this.network = network;
43                 configureNetworkStatusMap();
44         }
45         
46         /**
47          * Capture the data from a Neutron Network object.
48          *
49          * For MSO, there are assumptions regarding all networks.
50          * - Everything will be a provider network
51          * - All provider networks are VLANs
52          * - Multiple VLANs are supported, and indicated by multi-provider segments.
53          *   Each will have the same physical network & network type "vlan".
54          *
55          * @param network
56          */
57         public NetworkInfo map() {
58                 final NetworkInfo info = new NetworkInfo();
59                 if (network == null) {
60                         info.setStatus(NetworkStatus.NOTFOUND);
61                 } else {
62                         info.setName(network.getName());
63                         info.setId(network.getId());
64                         info.setStatus(this.mapStatus(network.getStatus()));
65                         Pair<Optional<String>, List<Integer>> result = locateVlanInformation(network);
66                         Optional<String> value0 = result.getValue0();
67                         if (value0.isPresent()) {
68                                 info.setProvider(value0.get());
69                         }
70                         info.setVlans(result.getValue1());
71                         info.setSubnets(network.getSubnets());
72                 }
73                 return info;
74         }
75
76         protected NetworkStatus mapStatus(String status) {
77                 return networkStatusMap.getOrDefault(status, NetworkStatus.UNKNOWN);
78         }
79         
80         protected Pair<Optional<String>, List<Integer>> locateVlanInformation(Network network) {
81                 final List<Integer> vlans = new ArrayList<>();
82                 Optional<String> provider = Optional.empty();
83                 if (network.getProviderPhysicalNetwork() != null) {
84                         provider = Optional.ofNullable(network.getProviderPhysicalNetwork());
85                         if ("vlan".equals(network.getProviderNetworkType())) {
86                                 vlans.add(network.getProviderSegmentationId());
87                         }
88                 } else if (network.getSegments() != null && !network.getSegments().isEmpty()) {
89                         Segment s = network.getSegments().get(0);
90                         provider = Optional.ofNullable(s.getProviderPhysicalNetwork());
91                         if ("vlan".equals(s.getProviderNetworkType())) {
92                                 for (Segment s1 : network.getSegments()) {
93                                         vlans.add(s1.getProviderSegmentationId());
94                                 }
95                         }
96                 }
97                 
98                 return Pair.with(provider, vlans);
99         }
100         
101         private void configureNetworkStatusMap() {
102                 networkStatusMap.put("ACTIVE", NetworkStatus.ACTIVE);
103                 networkStatusMap.put("DOWN", NetworkStatus.DOWN);
104                 networkStatusMap.put("BUILD", NetworkStatus.BUILD);
105                 networkStatusMap.put("ERROR", NetworkStatus.ERROR);
106         }
107 }