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.MepId; |
| 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 | /** |
| 29 | * The default implementation of {@link org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation}. |
| 30 | */ |
| 31 | public final class DefaultMaintenanceAssociation implements MaintenanceAssociation { |
| 32 | |
| 33 | private MaIdShort maId; |
| 34 | private final CcmInterval ccmInterval; |
| 35 | private final Collection<Component> componentList; |
| 36 | private final Collection<MepId> remoteMepIdList; |
| 37 | private final short maNumericId; |
| 38 | |
| 39 | private DefaultMaintenanceAssociation(DefaultMaBuilder builder) |
| 40 | throws CfmConfigException { |
| 41 | this.maId = builder.maId; |
| 42 | this.ccmInterval = builder.ccmInterval; |
| 43 | this.componentList = builder.componentList; |
| 44 | this.remoteMepIdList = builder.remoteMepIdList; |
| 45 | this.maNumericId = builder.maNumericId; |
| 46 | } |
| 47 | |
| 48 | private DefaultMaintenanceAssociation(MaintenanceAssociation ma, |
| 49 | AttributeName attrName, Object attrValue) { |
| 50 | this.maId = ma.maId(); |
| 51 | this.ccmInterval = ma.ccmInterval(); |
| 52 | |
| 53 | if (attrName == AttributeName.COMPONENTLIST) { |
| 54 | this.componentList = (Collection<Component>) attrValue; |
| 55 | } else { |
| 56 | this.componentList = ma.componentList(); |
| 57 | } |
| 58 | |
| 59 | if (attrName == AttributeName.REMOTEMEPIDLIST) { |
| 60 | this.remoteMepIdList = (Collection<MepId>) attrValue; |
| 61 | } else { |
| 62 | this.remoteMepIdList = ma.remoteMepIdList(); |
| 63 | } |
| 64 | |
| 65 | this.maNumericId = ma.maNumericId(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public MaIdShort maId() { |
| 70 | return maId; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CcmInterval ccmInterval() { |
| 75 | return ccmInterval; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Collection<Component> componentList() { |
| 80 | if (componentList != null) { |
| 81 | return Lists.newArrayList(componentList); |
| 82 | } |
| 83 | return Lists.newArrayList(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public MaintenanceAssociation withComponentList(Collection<Component> componentList) { |
| 88 | return new DefaultMaintenanceAssociation(this, AttributeName.COMPONENTLIST, componentList); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Collection<MepId> remoteMepIdList() { |
| 93 | if (remoteMepIdList != null) { |
| 94 | return Lists.newArrayList(remoteMepIdList); |
| 95 | } |
| 96 | return Lists.newArrayList(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public MaintenanceAssociation withRemoteMepIdList(Collection<MepId> remoteMepIdList) { |
| 101 | return new DefaultMaintenanceAssociation(this, AttributeName.REMOTEMEPIDLIST, remoteMepIdList); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public short maNumericId() { |
| 106 | return maNumericId; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int hashCode() { |
| 111 | final int prime = 31; |
| 112 | int result = 1; |
| 113 | result = prime * result |
| 114 | + ((ccmInterval == null) ? 0 : ccmInterval.hashCode()); |
| 115 | result = prime * result |
| 116 | + ((componentList == null) ? 0 : componentList.hashCode()); |
| 117 | result = prime * result + ((maId == null) ? 0 : maId.hashCode()); |
| 118 | result = prime * result |
| 119 | + ((remoteMepIdList == null) ? 0 : remoteMepIdList.hashCode()); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean equals(Object obj) { |
| 125 | if (this == obj) { |
| 126 | return true; |
| 127 | } |
| 128 | if (obj == null) { |
| 129 | return false; |
| 130 | } |
| 131 | if (getClass() != obj.getClass()) { |
| 132 | return false; |
| 133 | } |
| 134 | DefaultMaintenanceAssociation other = (DefaultMaintenanceAssociation) obj; |
| 135 | if (ccmInterval != other.ccmInterval) { |
| 136 | return false; |
| 137 | } |
| 138 | if (componentList == null) { |
| 139 | if (other.componentList != null) { |
| 140 | return false; |
| 141 | } |
| 142 | } else if (!componentList.equals(other.componentList)) { |
| 143 | return false; |
| 144 | } |
| 145 | if (!maId.equals(other.maId)) { |
| 146 | return false; |
| 147 | } |
| 148 | if (remoteMepIdList == null) { |
| 149 | if (other.remoteMepIdList != null) { |
| 150 | return false; |
| 151 | } |
| 152 | } else if (!remoteMepIdList.equals(other.remoteMepIdList)) { |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public String toString() { |
| 160 | return MoreObjects.toStringHelper(getClass()).add("maId", maId).toString(); |
| 161 | } |
| 162 | |
| 163 | public static MaBuilder builder(MaIdShort maId, int mdNameAndTypeLen) |
| 164 | throws CfmConfigException { |
| 165 | return new DefaultMaBuilder(maId, mdNameAndTypeLen); |
| 166 | } |
| 167 | |
| 168 | public static MaBuilder builder(MaintenanceAssociation ma) |
| 169 | throws CfmConfigException { |
| 170 | return new DefaultMaBuilder(ma); |
| 171 | } |
| 172 | |
| 173 | private static final class DefaultMaBuilder implements MaintenanceAssociation.MaBuilder { |
| 174 | |
| 175 | private final MaIdShort maId; |
| 176 | private CcmInterval ccmInterval; |
| 177 | private Collection<Component> componentList; |
| 178 | private Collection<MepId> remoteMepIdList; |
| 179 | private short maNumericId; |
| 180 | |
| 181 | public DefaultMaBuilder(MaIdShort maId, int mdNameAndTypeLen) |
| 182 | throws CfmConfigException { |
| 183 | if (maId.getNameLength() + mdNameAndTypeLen > 48) { |
| 184 | throw new CfmConfigException("Combined length of MD name and " |
| 185 | + "MA name exceeds 48. Cannot create"); |
| 186 | } |
| 187 | this.maId = maId; |
| 188 | this.componentList = new ArrayList<>(); |
| 189 | this.remoteMepIdList = new ArrayList<>(); |
| 190 | } |
| 191 | |
| 192 | public DefaultMaBuilder(MaintenanceAssociation ma) |
| 193 | throws CfmConfigException { |
| 194 | |
| 195 | this.maId = ma.maId(); |
| 196 | this.maNumericId = ma.maNumericId(); |
| 197 | this.componentList = new ArrayList<>(); |
| 198 | ma.componentList().forEach(comp -> this.componentList.add(comp)); |
| 199 | this.remoteMepIdList = new ArrayList<>(); |
| 200 | ma.remoteMepIdList().forEach((rmep -> this.remoteMepIdList.add(rmep))); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public MaBuilder addToComponentList(Component component) { |
| 205 | this.componentList.add(component); |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public MaBuilder addToRemoteMepIdList(MepId remoteMep) { |
| 211 | this.remoteMepIdList.add(remoteMep); |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public MaBuilder ccmInterval(CcmInterval ccmInterval) { |
| 217 | this.ccmInterval = ccmInterval; |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public MaBuilder maNumericId(short maNumericId) { |
| 223 | if (maNumericId <= 0) { |
| 224 | throw new IllegalArgumentException( |
| 225 | "numericId must be > 0. Rejecting " + maNumericId); |
| 226 | } |
| 227 | this.maNumericId = maNumericId; |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public MaintenanceAssociation build() throws CfmConfigException { |
| 233 | return new DefaultMaintenanceAssociation(this); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private enum AttributeName { |
| 238 | REMOTEMEPIDLIST, |
| 239 | COMPONENTLIST |
| 240 | } |
| 241 | |
| 242 | } |