2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.openstack.mappers;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
29 import org.javatuples.Pair;
30 import org.onap.so.openstack.beans.NetworkInfo;
31 import org.onap.so.openstack.beans.NetworkStatus;
33 import com.woorea.openstack.quantum.model.Network;
34 import com.woorea.openstack.quantum.model.Segment;
36 public class NetworkInfoMapper {
38 private final Network network;
39 private final Map<String,NetworkStatus> networkStatusMap = new HashMap<>();
41 public NetworkInfoMapper(Network network) {
42 this.network = network;
43 configureNetworkStatusMap();
47 * Capture the data from a Neutron Network object.
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".
57 public NetworkInfo map() {
58 final NetworkInfo info = new NetworkInfo();
59 if (network == null) {
60 info.setStatus(NetworkStatus.NOTFOUND);
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());
70 info.setVlans(result.getValue1());
71 info.setSubnets(network.getSubnets());
76 protected NetworkStatus mapStatus(String status) {
77 return networkStatusMap.getOrDefault(status, NetworkStatus.UNKNOWN);
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());
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());
98 return Pair.with(provider, vlans);
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);