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