Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.incubator.net.l2monitoring.cfm; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | import com.google.common.collect.Lists; |
| 27 | |
| 28 | /** |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 29 | * The default implementation of {@link MaintenanceDomain}. |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 30 | */ |
| 31 | public final class DefaultMaintenanceDomain implements MaintenanceDomain { |
| 32 | |
| 33 | private final MdId mdId; |
| 34 | private final MdLevel mdLevel; |
| 35 | private final Collection<MaintenanceAssociation> maintenanceAssociationList; |
| 36 | private final short mdNumericId; |
| 37 | |
| 38 | private DefaultMaintenanceDomain(DefaultMdBuilder builder) throws CfmConfigException { |
| 39 | this.mdId = builder.mdId; |
| 40 | this.mdLevel = builder.mdLevel; |
| 41 | this.maintenanceAssociationList = builder.maList; |
| 42 | this.mdNumericId = builder.mdNumericId; |
| 43 | } |
| 44 | |
| 45 | private DefaultMaintenanceDomain(DefaultMaintenanceDomain md, Collection<MaintenanceAssociation> maList) { |
| 46 | this.mdId = md.mdId; |
| 47 | this.mdLevel = md.mdLevel; |
| 48 | this.maintenanceAssociationList = maList; |
| 49 | this.mdNumericId = md.mdNumericId; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public MdId mdId() { |
| 54 | return mdId; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public MdLevel mdLevel() { |
| 59 | return mdLevel; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<MaintenanceAssociation> maintenanceAssociationList() { |
| 64 | if (maintenanceAssociationList != null) { |
| 65 | return Lists.newArrayList(maintenanceAssociationList); |
| 66 | } |
| 67 | return Lists.newArrayList(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public MaintenanceDomain withMaintenanceAssociationList( |
| 72 | Collection<MaintenanceAssociation> maintenanceAssociationList) { |
| 73 | return new DefaultMaintenanceDomain(this, maintenanceAssociationList); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public short mdNumericId() { |
| 78 | return mdNumericId; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int hashCode() { |
| 83 | final int prime = 31; |
| 84 | int result = 1; |
| 85 | result = prime * result + ((mdId == null) ? 0 : mdId.hashCode()); |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (this == obj) { |
| 92 | return true; |
| 93 | } |
| 94 | if (obj == null) { |
| 95 | return false; |
| 96 | } |
| 97 | if (getClass() != obj.getClass()) { |
| 98 | return false; |
| 99 | } |
| 100 | DefaultMaintenanceDomain other = (DefaultMaintenanceDomain) obj; |
| 101 | if (mdId == null) { |
| 102 | if (other.mdId != null) { |
| 103 | return false; |
| 104 | } |
| 105 | } else if (!mdId.equals(other.mdId)) { |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(getClass()) |
| 114 | .add("mdId", mdId.toString()) |
| 115 | .add("level", mdLevel).toString(); |
| 116 | } |
| 117 | |
| 118 | public static MdBuilder builder(MdId mdId) |
| 119 | throws CfmConfigException { |
| 120 | return new DefaultMdBuilder(mdId); |
| 121 | } |
| 122 | |
| 123 | //A method for creating a builder from an existing MD |
| 124 | public static MdBuilder builder(MaintenanceDomain md) |
| 125 | throws CfmConfigException { |
| 126 | return new DefaultMdBuilder(md); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | private static final class DefaultMdBuilder implements MaintenanceDomain.MdBuilder { |
| 131 | private final MdId mdId; |
| 132 | private MdLevel mdLevel = MdLevel.LEVEL0; |
| 133 | private Collection<MaintenanceAssociation> maList; |
| 134 | private short mdNumericId; |
| 135 | |
| 136 | private DefaultMdBuilder(MdId mdId) |
| 137 | throws CfmConfigException, NumberFormatException { |
| 138 | this.mdId = mdId; |
| 139 | this.maList = new ArrayList<>(); |
| 140 | } |
| 141 | |
| 142 | private DefaultMdBuilder(MaintenanceDomain md) |
| 143 | throws CfmConfigException, NumberFormatException { |
| 144 | this.mdId = md.mdId(); |
| 145 | this.mdNumericId = md.mdNumericId(); |
| 146 | this.mdLevel = md.mdLevel(); |
| 147 | this.maList = new ArrayList<>(); |
| 148 | md.maintenanceAssociationList().forEach(ma -> this.maList.add(ma)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public MdBuilder mdLevel(MdLevel mdLevel) { |
| 153 | this.mdLevel = mdLevel; |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public MdBuilder addToMaList(MaintenanceAssociation ma) { |
| 159 | this.maList.add(ma); |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public MdBuilder deleteFromMaList(MaIdShort maName) { |
| 165 | this.maList.removeIf(ma -> ma.maId().equals(maName)); |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean checkMaExists(MaIdShort maName) { |
| 171 | return this.maList.stream().anyMatch(ma -> ma.maId().equals(maName)); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public MdBuilder mdNumericId(short mdNumericId) { |
| 176 | if (mdNumericId <= 0) { |
| 177 | throw new IllegalArgumentException( |
| 178 | "numericId must be > 0. Rejecting " + mdNumericId); |
| 179 | } |
| 180 | this.mdNumericId = mdNumericId; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public MaintenanceDomain build() throws CfmConfigException { |
| 186 | return new DefaultMaintenanceDomain(this); |
| 187 | } |
| 188 | } |
| 189 | } |