Add nullcheck to HeatbridgeUtils.extractPciIdsFromVserver
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / heatbridge / utils / HeatBridgeUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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 /*
22  * Copyright (C) 2018 Bell Canada. All rights reserved.
23  *
24  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
25  * the License. You may obtain a copy of the License at
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
30  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
31  * specific language governing permissions and limitations under the License.
32  */
33
34 package org.onap.so.heatbridge.utils;
35
36 import com.google.common.base.Preconditions;
37 import com.google.common.base.Strings;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42 import javax.annotation.Nonnull;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.onap.aai.domain.yang.SriovVf;
45 import org.onap.aai.domain.yang.Vserver;
46
47 public final class HeatBridgeUtils {
48
49     /**
50      * IaaS naming convention for compute/p-interface to openstack/physical-network name mapping
51      */
52     private static final String OS_SIDE_SHARED_SRIOV_PREFIX = "shared-";
53     private static final String OS_SIDE_DEDICATED_SRIOV_PREFIX = "dedicated-";
54     private static final String COMPUTE_SIDE_SHARED_SRIOV_PREFIX = "sriov-s-";
55     private static final String COMPUTE_SIDE_DEDICATED_SRIOV_PREFIX = "sriov-d-";
56
57     private HeatBridgeUtils() {
58         throw new IllegalStateException("Trying to instantiate a utility class.");
59     }
60
61     public static Optional<String> getMatchingPserverPifName(@Nonnull final String physicalNetworkName) {
62         Preconditions.checkState(!Strings.isNullOrEmpty(physicalNetworkName),
63                 "Physical network name is null or empty!");
64         if (physicalNetworkName.contains(OS_SIDE_DEDICATED_SRIOV_PREFIX)) {
65             return Optional.of(
66                     physicalNetworkName.replace(OS_SIDE_DEDICATED_SRIOV_PREFIX, COMPUTE_SIDE_DEDICATED_SRIOV_PREFIX));
67         } else if (physicalNetworkName.contains(OS_SIDE_SHARED_SRIOV_PREFIX)) {
68             return Optional
69                     .of(physicalNetworkName.replace(OS_SIDE_SHARED_SRIOV_PREFIX, COMPUTE_SIDE_SHARED_SRIOV_PREFIX));
70         }
71         return Optional.of(physicalNetworkName);
72     }
73
74     public static List<String> extractPciIdsFromVServer(Vserver vserver) {
75         if (vserver.getLInterfaces() == null) {
76             return Collections.emptyList();
77         }
78         return vserver.getLInterfaces().getLInterface().stream()
79                 .filter(lInterface -> lInterface.getSriovVfs() != null
80                         && CollectionUtils.isNotEmpty(lInterface.getSriovVfs().getSriovVf()))
81                 .flatMap(lInterface -> lInterface.getSriovVfs().getSriovVf().stream()).map(SriovVf::getPciId)
82                 .collect(Collectors.toList());
83     }
84
85 }