Add missing license headers to packetcable-driver messages
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / messages / impl / COPSDecisionMsgEX.java
1 /*
2  * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.pcmm.messages.impl;
10
11 import org.umu.cops.stack.*;
12 import org.umu.cops.stack.COPSHeader.OPCode;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.net.Socket;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
20
21 /**
22  * COPS Decision Message
23  *
24  *
25  */
26
27 public class COPSDecisionMsgEX extends COPSMsg {
28
29     /* COPSHeader coming from base class */
30     private final COPSHandle _clientHandle;
31     private final COPSError _error;
32     private final COPSIntegrity _integrity;
33 //    private COPSContext _decContext;
34     private final COPSClientSI clientSI;
35     private final Map<COPSContext, List<COPSDecision>> _decisions;
36
37     public COPSDecisionMsgEX(final short clientType, final COPSHandle _clientHandle, final COPSError _error,
38                              final COPSIntegrity _integrity, final COPSClientSI clientSI,
39                              final Map<COPSContext, List<COPSDecision>> _decisions) {
40         super(new COPSHeader(OPCode.DEC, clientType));
41         this._clientHandle = _clientHandle;
42         this._error = _error;
43         this._integrity = _integrity;
44         this.clientSI = clientSI;
45         this._decisions = new ConcurrentHashMap<>(_decisions);
46     }
47
48     @Override
49     protected int getDataLength() {
50         int out = 0;
51         if (_clientHandle != null) out += _clientHandle.getDataLength();
52         if (_error != null) out += _error.getDataLength();
53
54         // Display decisions
55         // Display any local decisions
56         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
57             out += entry.getKey().getDataLength();
58             for (final COPSDecision decision : entry.getValue()) {
59                 out += decision.getDataLength();
60             }
61         }
62         if (clientSI != null) out += clientSI.getDataLength();
63         if (_integrity != null) out += _integrity.getDataLength();
64         return out;
65     }
66
67     @Override
68     protected void writeBody(final Socket socket) throws IOException {
69         if (_clientHandle != null)
70             _clientHandle.writeData(socket);
71         if (_error != null)
72             _error.writeData(socket);
73
74         // Display decisions
75         // Display any local decisions
76         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
77
78             final COPSContext context = entry.getKey();
79             final List<COPSDecision> decisions = entry.getValue();
80             context.writeData(socket);
81
82             for (final COPSDecision decision : decisions) {
83                 decision.writeData(socket);
84             }
85         }
86         if (clientSI != null)
87             clientSI.writeData(socket);
88         if (_integrity != null)
89             _integrity.writeData(socket);
90     }
91
92     /**
93      * Method getClientHandle
94      *
95      * @return a COPSHandle
96      *
97      */
98     public COPSHandle getClientHandle() {
99         return _clientHandle;
100     }
101
102     public COPSClientSI getClientSI() {
103         return clientSI;
104     }
105
106     @Override
107     protected void dumpBody(final OutputStream os) throws IOException {
108         if (_clientHandle != null)
109             _clientHandle.dump(os);
110         if (_error != null)
111             _error.dump(os);
112
113         // Display any local decisions
114         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
115             final COPSContext context = entry.getKey();
116             final List<COPSDecision> v = entry.getValue();
117             context.dump(os);
118
119             for (final COPSDecision decision : v) {
120                 decision.dump(os);
121             }
122         }
123         if (clientSI != null)
124             clientSI.dump(os);
125         if (_integrity != null) {
126             _integrity.dump(os);
127         }
128     }
129 }