blob: 49023d4c8c62fff0fbe209dab88547160fe0963c [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.incubator.net.l2monitoring.cfm.service;
17
Sean Condon96b896d2017-12-11 12:44:29 -080018import com.google.common.base.MoreObjects;
19import org.onlab.util.Tools;
Sean Condon0e89bda2017-03-21 14:23:19 +000020import org.onosproject.event.AbstractEvent;
Sean Condon96b896d2017-12-11 12:44:29 -080021import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
22import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
23import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
Sean Condon0e89bda2017-03-21 14:23:19 +000024import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
25
Sean Condon96b896d2017-12-11 12:44:29 -080026import java.util.Optional;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29
Sean Condon0e89bda2017-03-21 14:23:19 +000030/**
31 * Event related to the maintenance of CFM MDs.
32 */
33public class MdEvent extends AbstractEvent<MdEvent.Type, MdId> {
34
Sean Condon96b896d2017-12-11 12:44:29 -080035 private MaIdShort maId;
36 private MaintenanceDomain oldMd;
Sean Condon0e89bda2017-03-21 14:23:19 +000037 /**
38 * MD Event types supported.
39 */
40 public enum Type {
41 MD_ADDED,
42 MD_REMOVED,
Sean Condon96b896d2017-12-11 12:44:29 -080043 MD_UPDATED,
44 MA_ADDED,
45 MA_REMOVED
Sean Condon0e89bda2017-03-21 14:23:19 +000046 }
47
48 public MdEvent(Type type, MdId mdId) {
49 super(type, mdId);
50 }
Sean Condon96b896d2017-12-11 12:44:29 -080051
52 /**
53 * Constructor that allows the MD to be held in the event.
54 * This is useful if the MD had been deleted - it will be the only way of
55 * retrieving some of its attributes
56 * @param type The type of the event
57 * @param mdId The ID of the MD
58 * @param md The whole MD
59 * @throws CfmConfigException if there's a problem copying MD
60 */
61 public MdEvent(Type type, MdId mdId, MaintenanceDomain md) throws CfmConfigException {
62 super(type, mdId);
63 this.oldMd = DefaultMaintenanceDomain.builder(md).build();
64 }
65
66 public MdEvent(Type type, MdId mdId, MaintenanceDomain md, MaIdShort maId)
67 throws CfmConfigException {
68 super(type, mdId);
69 this.maId = maId;
70 this.oldMd = DefaultMaintenanceDomain.builder(md).build();
71 }
72
73 public Optional<MaIdShort> maId() {
74 return maId == null ? Optional.empty() : Optional.of(maId);
75 }
76
77 public Optional<MaintenanceDomain> md() {
78 return oldMd == null ? Optional.empty() : Optional.of(oldMd);
79 }
80
81 @Override
82 public String toString() {
83 MoreObjects.ToStringHelper helper = toStringHelper(this)
84 .add("time", Tools.defaultOffsetDataTime(time()))
85 .add("type", type())
86 .add("subject", subject());
87 if (maId != null) {
88 helper = helper.add("subject2", maId);
89 }
90 return helper.toString();
91 }
Sean Condon0e89bda2017-03-21 14:23:19 +000092}